题目链接:~( ̄▽ ̄~)(~ ̄▽ ̄)~
思路:用bfs一次把这个方向上能到的点入队
code:
#include <stdio.h>
int m = 0, n = 0, front = 0, rear = 0, map[1002][1002], used[1002][1002];
int X1 = 0, Y1 = 0, X2 = 0, Y2 = 0;
int dir[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
typedef struct
{
int x, y, count;
}node;
node step, quene[1000*1000+1];
int bfs()
{
int i = 0, x = 0, y = 0, fx = 0, fy = 0, count = 0;
quene[rear++] = step;
while(front<rear)
{
x = quene[front].x; y = quene[front].y; count = quene[front++].count;
for(i = 0; i<4; i++)
{
fx = x+dir[i][0]; fy = y+dir[i][1];
while(!map[fx][fy] && count<3 && fx>0 && fx<=n && fy>0 &&fy<=m && !used[fx][fy])//这方向上能到的点入队
{
used[fx][fy] = 1;
step.x = fx; step.y = fy; step.count = count+1;
quene[rear++] = step;
fx += dir[i][0]; fy += dir[i][1];
}
if(fx == X2 && fy == Y2 && count<3)
return 1;
}
}
return 0;
}
int main()
{
int i = 0, j= 0, q = 0;
while(scanf("%d %d",&n, &m) , n && m)
{
for(i = 1; i<=n; i++)
for(j = 1; j<=m; j++)
scanf("%d",&map[i][j]);
scanf("%d",&q);
while(q--)
{
for(i = 1; i<=n; i++)
for(j = 1; j<=m; j++)
used[i][j] = 0;
scanf("%d %d %d %d",&X1, &Y1, &X2, &Y2);
if((map[X1][Y1] != map[X2][Y2] || !map[X1][Y1] || !map[X2][Y2]) || (X1==X2 && Y2 == Y1))
printf("NO\n");
else
{
front = rear = 0;
step.x = X1; step.y = Y1;step.count = 0;
if(bfs())
printf("YES\n");
else
printf("NO\n");
}
}
}
return 0;
}