poj3984(广搜走迷宫输出路径)
来源:互联网 发布:招聘node 编辑:程序博客网 时间:2024/06/06 20:24
#include <iostream>#include <queue>#include <cstdio>using namespace std;int main(){ int A[7][7]; int u; int fa[7][7]={{0}}; int record[7][7]={{0}}; queue<int> q; int i,j; for (i=0;i<=4;i++) for (j=0;j<=4;j++) cin >> A[i][j]; u=0; q.push(u); record[0][0]=1; while (q.front()!=24 && !q.empty()) { int x=q.front()/5; int y=q.front()%5; if (!record[x+1][y] && x+1<=4 && A[x+1][y]!=1) { u=(x+1)*5+y; fa[x+1][y]=q.front(); q.push(u); record[x+1][y]=1; } if (!record[x-1][y] && x-1>=0 && A[x-1][y]!=1) { u=(x-1)*5+y; fa[x-1][y]=q.front(); q.push(u); record[x-1][y]=1; } if (!record[x][y+1] && y+1<=4 && A[x][y+1]!=1) { u=x*5+y+1; fa[x][y+1]=q.front(); q.push(u); record[x][y+1]=1; } if (!record[x][y-1] && y-1>=0 && A[x][y-1]!=1) { u=x*5+y-1; fa[x][y-1]=q.front(); q.push(u); record[x][y-1]=1; } q.pop(); } int x=4; int y=4; int path[100]; int counter=0; while (1) { path[counter++]=x*5+y; int tempx=x,tempy=y; if (fa[x][y]==0) break; x=fa[tempx][tempy]/5; y=fa[tempx][tempy]%5; } printf("(0, 0)\n"); for (i=counter-1;i>=0;i--) { printf("(%d, %d)\n",path[i]/5,path[i]%5); } return 0;}