简单深搜,主要记录转弯次数。
题目http://acm.hdu.edu.cn/showproblem.php?pid=1175
#include<stdio.h>
#include<string.h>
int mov[4][2] = {1,0,0,1,-1,0,0,-1};
int map[1001][1001];
int book[1001][1001];
int n,m,ex,ey;
int dfs(int sx,int sy,int turn,int dir)
{
if(turn==3)
return 0;
for(int i = 0;i<4;i++)
{
if(turn==2 && dir!=i)
continue;
int next_x = sx+mov[i][0];
int next_y = sy+mov[i][1];
if(next_x==ex && next_y==ey)
return 1;
if(next_x<0 || next_x>=n || next_y<0 || next_y>=m || book[next_x][next_y])
continue;
if(map[next_x][next_y]==0)
{
int t;
t = (dir==i?turn:turn+1);
book[next_x][next_y] = 1;
if(dfs(next_x,next_y,t,i)==1)
{
return 1;
}
book[next_x][next_y] = 0;
}
}
return 0;
}
int main()
{
while(scanf("%d%d",&n,&m) && n+m)
{
for(int i = 0;i<n;i++)
for(int j = 0;j<m;j++)
scanf("%d",&map[i][j]);
int num;
scanf("%d",&num);
int sx,sy;
while(num--)
{
memset(book,0,sizeof(book));
scanf("%d%d%d%d",&sx,&sy,&ex,&ey);
sx = sx - 1;
sy = sy - 1;
ex = ex - 1;
ey = ey - 1;
if(map[sx][sy]!=map[ex][ey] || map[sx][sy]==0)
{
printf("NO\n");
}
else
{
printf("%s\n",dfs(sx,sy,-1,-1)==0?"NO":"YES");
}
}
}
return 0;
}