迷宫问题(ybt1255
#include <bits/stdc++.h>
using namespace std;
const int N=100;
int g[N][N];
typedef pair<int,int> PII;
PII q[N*N],pre[N][N];
void bfs(int x,int y) {
memset(pre,-1,sizeof pre);
int dx[4]={-1,0,1,0};
int dy[4]={0,1,0,-1};
int hh=0,tt=0;
q[0]={x,y};
while(hh<=tt) {
PII t=q[hh++];
for(int i=0;i<4;i++) {
int a=t.first+dx[i];
int b=t.second+dy[i];
if(a>=0&&a<5&&b>=0&&b<5&&g[a][b]==0&&pre[a][b].first==-1) {
q[++tt]={a,b};
pre[a][b]=t;
}
}
}
}
int main()
{
//int n;
//cin>>n;
for(int i=0;i<5;i++) {
for(int j=0;j<5;j++) {
cin>>g[i][j];
}
}
bfs(4,4);
PII end(0,0);
while(true) {
cout<<"("<<end.first<<", "<<end.second<<")"<<endl;
if(end.first==4 && end.second==4) break;
end=pre[end.first][end.second];//pre记录路径
}
return 0;
}