/*
很纠结的题目,自己一开始写的代码就是wrong,一直改不出来,
#include<iostream>
#include<queue>
#include<cstdio>
using namespace std;
int x1, y1, x2, y2;
int n, m;
int map[1002][1002];
int hash[1002][1002];
//bool hush[1002][1002];
int dir[4][2] = { {1, 0}, {-1, 0}, {0 , 1}, {0, -1} };
struct node
{
int x;
int y;
int st; //标记转弯的次数
int dir;//标记方向
};
int bfs()
{
queue<node> q;
node N, P;
int i;
int j;
for(i = 1; i <= n; i++)
for(j = 1; j <= m; j++)
{
//scanf("%d", &map[i][j]);
hash[i][j] = 100;
//hush[i][j] = false;
}
N.x = x1;
N.y = y1;
N.st = 0;
N.dir = 0;
q.push(N);
//N = q.front();
//q.pop();
for(i = 0; i < 4; i++)
{
P.x = N.x + dir[i][0];
P.y = N.y + dir[i][1];
P.st = 0;
P.dir = i;
if(P.x < 1 || P.x > n || P.y < 1 || P.y > m)
continue;
if(!map[P.x][P.y] || map[P.x][P.y] == map[x2][y2])
{
hash[P.x][P.y] = 0;
q.push(P);
//hush[P.x][P.y] = true;
}
}
q.pop();
while(!q.empty())
{
N = q.front();
q.pop();
for(i = 0; i < 4; i++)
{
P.x = N.x + dir[i][0];
P.y = N.y + dir[i][1];
P.dir = i;
P.st = N.st;
if(P.x < 1 || P.x > n || P.y < 1 || P.y > m)
continue;
if(P.dir != N.dir)
{
P.st = N.st + 1;
}
if((!map[P.x][P.y] || map[P.x][P.y] == map[x2][y2]) && P.st <= 2 && P.st <= hash[P.x][P.y])
{
q.push(P);
hash[P.x][P.y] = P.st;
//hush[P.x][P.y] = true;
}
}
if(hash[x2][y2] <= 2)
{
printf("YES/n");
return 0;
}
}
//else
//{
printf("NO/n");
return 0;
//}
}
int main()
{
int i, j, t;
while(scanf("%d%d", &n, &m) != EOF)
{
if(n == 0 && m == 0)
continue;
for(i = 1; i <= n; i++)
for(j = 1; j <= m; j++)
{
scanf("%d", &map[i][j]);
//hash[i][j] = 10;
}
scanf("%d", &t);
while(t--)
{
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
if(map[x1][y1] == map[x2][y2] && map[x1][y1] && map[x2][y2] && !(x1 == x2 && y1 == y2))
{
bfs();
}
else
{
printf("NO/n");
}
}
}
}
后来看了下论坛,找参考以下的方法
*/
#include<iostream>//2425744 2010-05-06 23:07:59 Accepted 1175 156MS 6260K 1634 B G++ 悔惜晟
#include<queue>
#include<cstdio>
using namespace std;
int x1, y1, x2, y2;
int n, m;
int map[1002][1002];
int hash[1002][1002];
int dir[4][2] = { {1, 0}, {-1, 0}, {0 , 1}, {0, -1} };
struct node
{
int x;
int y;
int dir;//标记方向
};
int bfs()
{
queue<node> q;
node N, P;
int i;
int sum;
N.x = x1;
N.y = y1;
N.dir = -1;
q.push(N);
memset(hash, 0, sizeof(hash));
while(!q.empty())
{
N = q.front();
q.pop();
for(i = 0; i < 4; i++)
{
P.x = N.x + dir[i][0];
P.y = N.y + dir[i][1];
P.dir = i;
if(N.dir != P.dir)
{
sum = hash[N.x][N.y] + 1;
}
else
{
sum = hash[N.x][N.y];
}
if(P.x >= 1 && P.x <= n && P.y >= 1 && P.y <= m && (sum <= hash[P.x][P.y]|| hash[P.x][P.y] == 0))//写了P.y <= n 找了下才找出来
{
if(P.x == x2 && P.y == y2 && map[P.x][P.y] == map[x2][y2])
{
hash[P.x][P.y] = sum;
if(hash[P.x][P.y] <= 3)
{
puts("YES");
return 1;
}
}
if(map[P.x][P.y] == 0)
{
hash[P.x][P.y] = sum;
if(hash[P.x][P.y] == 4)
continue;
q.push(P);
}
}
}
}
puts("NO");
return 1;
}
int main()
{
int i, j, t;
while(scanf("%d%d", &n, &m) != EOF)
{
if(n == 0 && m == 0)
continue;
for(i = 1; i <= n; i++)
for(j = 1; j <= m; j++)
{
scanf("%d", &map[i][j]);
}
scanf("%d", &t);
while(t--)
{
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
if(map[x1][y1] == map[x2][y2] && map[x1][y1] && map[x2][y2] && !(x1 == x2 && y1 == y2))
{
bfs();
}
else
{
printf("NO/n");
}
}
}
}