走迷宫

走迷宫

时限:1000ms 内存限制:10000K  总时限:3000ms

描述
判断是否能从迷宫的入口到达出口
 
输入
先输入两个整数表示迷宫的行数m和列数n,再输入口和出口的坐标,最后分m行输入迷宫,其中1表示墙,0表示空格每个数字之间都有空格。
 
输出
若能到达,则输出"Yes",否则输出"No",结果占一行。
 
输入样例
3 3
0 0
2 2
0 0 0
1 1 0
0 1 0
 
输出样例
Yes
 #include <iostream>

 using namespace std;
 typedef struct postion
 {
     int posx;
     int posy;
 } postion;
 static int map[25][25];
 static int m,n;
 static postion startpos,endpos;
 static bool flag,stop;                                                          
                                                                                 
 
 void SearchPath(int fromdirect,int curposx,int curposy)
 {
     //cout<<"hello!! "<<curposx<<" "<<curposy<<endl;
     if(curposx==endpos.posx&&curposy==endpos.posy)
     {
         flag = true;
         stop = true;                                                            
         //cout<<"have reached!!"<<endl;                                         
         return ;
     }
     if((curposy+1<=n-1)&&map[curposx][curposy+1]==0&&fromdirect!=3&&stop==false)
     {
         SearchPath(1,curposx,curposy+1);
     }
     if((curposx+1<=m-1)&&map[curposx+1][curposy]==0&&fromdirect!=4&&stop==false)
     {
         SearchPath(2,curposx+1,curposy);
     }
     if((curposy-1>=0)&&map[curposx][curposy-1]==0&&fromdirect!=1&&stop==false)  
     {
         SearchPath(3,curposx,curposy-1);
     }
     if((curposx-1>=0)&&map[curposx-1][curposy]==0&&fromdirect!=2&&stop==false)  
     {
         SearchPath(4,curposx-1,curposy);
     }
 }
 
 int main()
 {
     cin>>m>>n;
     cin>>startpos.posx>>startpos.posy;
     cin>>endpos.posx>>endpos.posy;
     for(int i = 0;i < m; i++)
         for(int j = 0;j < n; j++)
             cin>>map[i][j];
     flag = false,stop = false;                                                  
 
     /*cout<<endl;
     for(int i = 0;i < m; i++)
     {
         for(int j = 0;j < n; j++)
             cout<<map[i][j]<<" ";
         cout<<endl;
     }
     cout<<endl;*/
     int fromdirect = 0;
     int curposx = startpos.posx;
     int curposy = startpos.posy;
     SearchPath(fromdirect,curposx,curposy);    //用sourcedirect来标记源方向。1表示右方,2表示下方,3表示左方,4表示上方
     if(flag==false)
         cout<<"No"<<endl;
     else
         cout<<"Yes"<<endl;
     return 0;
 }

  

态度决定高度,细节决定成败,
原文地址:https://www.cnblogs.com/lxk2010012997/p/4374896.html