# 迷宫问题(bfs)

int maze[5][5] = {

0, 1, 0, 0, 0,

0, 1, 0, 1, 0,

0, 0, 0, 0, 0,

0, 1, 1, 1, 0,

0, 0, 0, 1, 0,

};

0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0

(0, 0)
(1, 0)
(2, 0)
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(3, 4)
(4, 4)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int mp[5][5],vis[5][5];
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
struct node{
int x,y;
};
node pre[10][10];
void BFS(){
queue<node> que;
node str;
str.x=str.y=0;
que.push(str);
vis[0][0]=1;
while(!que.empty()){
node now = que.front();
que.pop();
if(now.x == 4 && now.y == 4)
return;
for(int i = 0;i < 4;i++){
node next;
next.x = now.x + dir[i][0];
next.y = now.y + dir[i][1];
if(next.x >= 0 && next.x < 5 && next.y >= 0 && next.y < 5 && !mp[next.x][next.y] && !vis[next.x][next.y]){
vis[next.x][next.y] = 1;
que.push(next);
pre[next.x][next.y] = now;
}
}
}
}
void print(node cur){
if(cur.x == 0 && cur.y == 0){
printf("(0, 0)\n");
return;
}
print(pre[cur.x][cur.y]);   //逆序输出
printf("(%d, %d)\n",cur.x,cur.y);
}
int main(){
for(int i = 0;i < 5;i++){
for(int j = 0;j < 5;j++){
scanf("%d",&mp[i][j]);
}
}
BFS();
node ed;
ed.x = ed.y = 4;
print(ed);
return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
using namespace std;
int cross[5] = {0,0,-1,0,1};
int stra[5] = {0,1,0,-1,0};
int mapp[50][50];
bool vis[50][50];
stack <int> s1,s2;
struct node{
int x,y;
node(int a,int b){//构造方法
x = a;
y = b;
}
};
queue<node> q,q1;
void bfs(int x,int y){
q.push(node(x,y));
vis[x][y] = 1;
while(!q.empty()){
node now = q.front();
q.pop();
if(now.x == 4 && now.y == 4){
return;
}
for(int i = 1;i <= 4;i++){
int pre = now.x + cross[i];
int pry = now.y + stra[i];
if(mapp[pre][pry] != 1 && pry >=0 && pry < 5 && pre >= 0 && pre < 5){
vis[pre][pry] = 1;
q.push(node(pre,pry));
q1.push(now);
//s1.push(pre);s2.push(pry);ll
}
}
}
}
void print(){
while(!q1.empty()){
node a = q1.front();
q1.pop();
printf("(%d, %d)\n",a.x,a.y);
}
printf("(4, 4)\n");
}
int main(){
for(int i = 0;i < 5;i++){
for(int j = 0;j < 5;j++){
cin >> mapp[i][j];
}
}
memset(vis,0,sizeof(vis));
bfs(0,0);
print();
return 0;
}