1 #include<iostream> 2 #include<algorithm> 3 #include<cstdio> 4 #include<cstring> 5 using namespace std; 6 7 int Map[6][6],vis[6][6],pre[110]; 8 //pre[]记录每个状态的前一个状态 9 struct Cam 10 { 11 int x,y; 12 }List[110]; 13 14 int Dire[4][2] = {-1,0,1,0,0,-1,0,1}; 15 16 int Go(int x,int y) //判断是否可走 17 { 18 if(x >= 0 && x < 5 && y >= 0 && y < 5 && Map[x][y] == 0) 19 return 1; 20 return 0; 21 } 22 23 void Print(int x) 24 { 25 int t; 26 t = pre[x]; 27 if(t == 0) 28 { 29 printf("(0, 0) "); 30 printf("(%d, %d) ",List[x].x,List[x].y); 31 return ; 32 } 33 else 34 Print(t); 35 printf("(%d, %d) ",List[x].x,List[x].y); 36 } 37 38 void BFS() 39 { 40 memset(vis,0,sizeof(vis)); 41 int Head = 0,Tail = 1; 42 List[0].x = 0; 43 List[0].y = 0; 44 pre[0] = -1; 45 while(Head < Tail) //队列 46 { 47 int x = List[Head].x; 48 int y = List[Head].y; 49 if(x == 4 && y == 4) 50 { 51 Print(Head); 52 return ; 53 } 54 for(int i = 0; i < 4; ++i) 55 { 56 int xx = x + Dire[i][0]; 57 int yy = y + Dire[i][1]; 58 if( !vis[xx][yy] && Go(xx,yy) ) 59 { 60 vis[xx][yy] = 1; 61 List[Tail].x = xx; 62 List[Tail].y = yy; 63 pre[Tail] = Head; 64 Tail++; 65 } 66 } 67 Head++; 68 } 69 return ; 70 } 71 72 int main() 73 { 74 75 for(int i = 0; i < 5; ++i) 76 for(int j = 0; j < 5; ++j) 77 scanf("%d",&Map[i][j]); 78 BFS(); 79 80 return 0; 81 }