The thief ran.

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.