标签:
#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;
}
标签:
原文地址:http://www.cnblogs.com/lxk2010012997/p/4374896.html