POJ 3984 ,迷宫问题(bfs)

来源:互联网 发布:淘宝换手机号了怎么改 编辑:程序博客网 时间:2024/06/05 15:11
#include <iostream>#include <queue>#include <cstdio>using namespace std;const int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};int map[5][5];struct node{int x;int y;int f;int id;}step[50];void pre(int temp){if (step[temp].f != -1) {pre(step[temp].f);printf("(%d, %d)\n",step[temp].x,step[temp].y);}}int main() {for (int i = 0; i < 5; i++) {for (int j = 0; j < 5; j++) {scanf("%d",&map[i][j]);}}int temp = 0,flag = 1,ans;queue<node> q;step[0].x = 0;step[0].y = 0;step[0].f = -1;step[0].id = 0;q.push(step[0]);map[0][0] = 1;while (!q.empty() && flag) {for (int i = 0; i < 4; i++) {int tx = q.front().x + dir[i][0];int ty = q.front().y + dir[i][1];ans = q.front().id;if (tx < 0 || ty < 0 || tx > 5 || ty > 5 || map[tx][ty] == 1) {continue;}if (tx == 4 && ty == 4) {flag = 0;break;} map[tx][ty] = 1;temp++;step[temp].x = tx;step[temp].y = ty;step[temp].f = q.front().id;//到达当前的前一个点step[temp].id = temp;q.push(step[temp]); }q.pop();}printf("(0, 0)\n");pre(ans);printf("(4, 4)\n");return 0;}

0 0
原创粉丝点击