#include<bits/stdc++.h>
using namespace std;
int n,m,f;
int cnt;
int last;
int a[1005][1005];
bool b[1005][1005];
bool c[1005][1005];
int dir[][2]={{1,0},{-1,0},{0,1},{0,-1}};
bool check(int x,int y)
{
if(x>=1&&x<=n&&y>=1&&y<=m)
return true;
else return false ;
}
void dfs(int x,int y,int u,int v)
{
if(cnt>3||f)//判断状态,此状态结束或目标状态结束
return ;
for(int i=0;i<4;++i)
{
int t=last;
int nx=x+dir[i][0];
int ny=y+dir[i][1];
if(check(nx,ny)&&c[nx][ny])
{
if(last!=i)
{
cnt++;
last=i;
}
if(nx==u&&ny==v&&cnt<=3)
{
f=1;
return ;
}
c[nx][ny]=0;
dfs(nx,ny,u,v);
c[nx][ny]=1;//深搜还原防止其他路径也会由此经过
if(t!=last)
{
last=t;
cnt--;
}//还原转弯
if(f)
return;
}
}
return ;
}
int main ()
{
while(cin>>n>>m&&n&&m)
{
memset(b,false ,sizeof(b));
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j)
{
cin>>a[i][j];
if(a[i][j]==0)
b[i][j]=1;
}
int k;
cin>>k;
for(int i1=0;i1<k;++i1)
{
int x,y,u,v;
cin>>x>>y>>u>>v;
if((a[x][y]!=a[u][v])||a[x][y]==0||a[u][v]==0||!check(x,y)||!check(u,v))//ifelse 不要交叉出错
{
cout<<"NO"<<endl;
}
else {
f=0;
cnt=0;
last=-1;
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j)
c[i][j]=b[i][j];
c[x][y]=0;
c[u][v]=1;//目标点初始化
dfs(x,y,u,v);
if(f)
cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
}
}
return 0;
}
02-13
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交