Recently, thieves in the east six dormitory are very smart. They are always able to find the path to escape after theft. To catch them, we need to know the path of their escape, please help us find their escape route (to simplify the problem, we can ensure that there is only one escape path at most, and the start point is (0, 0), and the end point is (4, 4, you cannot escape by diagonal lines. If someone intercepts the terminal, the escape fails ). Input
A 5*5 matrix separated by spaces. 0 indicates a walking path. 1 indicates an obstacle. The escape start point is top left and the end point is bottom right.
Output
Escape line. See the output example (note the X and Y axes). If there is no escape path, output: "No way! "(Excluding colons and quotation marks)
Sample Input
0 1 1 1 10 1 1 1 10 1 1 1 10 1 1 1 10 0 0 0 0
Sample output
(0,0)(0,1)(0,2)(0,3)(0,4)(1,4)(2,4)(3,4)(4,4)
#include <vector>#include <iostream>using namespace std;const int maze_size=5; int maze[maze_size+2][maze_size+2]; int state[maze_size+2][maze_size+2]; bool get; int num=2;struct node{ int x;int y;node *last;};vector<node> outlet; node finish; node start; void maze_creat(){ int i,j; for(i=0;i<=maze_size+1;i++) for(j=0;j<=maze_size+1;j++) if((i==0)||(i==maze_size+1)||(j==0)||(j==maze_size+1)) { maze[i][j]=1;state[i][j]=0; } for(i=1;i<=maze_size;++i) for(j=1;j<=maze_size;++j){ cin>>maze[j][i];state[j][i]=0;} start.x=1; start.y=1; finish.x=5; finish.y=5; get=false; return;}void seek(node now){ if(now.x==finish.x&&now.y==finish.y){ get=true; outlet.push_back(now);return; } node next; state[now.x][now.y]=1; if(maze[now.x][now.y+1]==0&&state[now.x][now.y+1]==0&&get==false){ next.x=now.x;next.y=now.y+1;next.last=&now;seek(next);} if(maze[now.x][now.y-1]==0&&state[now.x][now.y-1]==0&&get==false){ next.x=now.x;next.y=now.y-1;next.last=&now;seek(next);} if(maze[now.x+1][now.y]==0&&state[now.x+1][now.y]==0&&get==false){ next.x=now.x+1;next.y=now.y;next.last=&now;seek(next);} if(maze[now.x-1][now.y]==0&&state[now.x-1][now.y]==0&&get==false){ next.x=now.x-1;next.y=now.y;next.last=&now;seek(next);} if(get==true)outlet.push_back(now); return;}void display(){ vector<node>::size_type end_entry=outlet.size()-1; while(!outlet.empty()){ cout<<"("<<outlet[end_entry].x-1<<","<<outlet[end_entry].y-1<<")"<<endl; --end_entry; outlet.pop_back(); } return;}int main(){ maze_creat(); seek(start); if(get==false)cout<<"No Way!"<<endl; display(); return 0;}
The thief ran.