标签:
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1175
和之前的1728类似。就是判断转弯数,建立一个用于记录转弯数的数组。。
还有就是对于特殊情况要进行考虑,比如起点与终点相同的情况,对于本题来说是不可以消去的应该输出NO。还有就是起点或终点是零这也是不行的,因为0代表没有棋子。。。
还有在判断能不能走的时候要小心,对于判断条件一定要小心,不要图赶快写。。
错误的地方都写在注释中了。。
代码:
// hdu 1175 bfs 转弯数
//1、起点和终点重合的时候要特殊考虑,这是不能消掉的。。
//2、如果起点或终点之中有一个0,就不能消掉。
//3、 对于turn1数组每次查询之前都要进行初始化。。
#include<cstdio>
#include<queue>
#include<cstring>
#define INF 100000000;
using namespace std;
int n,m;
int sx,sy,ex,ey;
int ok;
int d1[4] = {0,0,1,-1};
int d2[4] = {1,-1,0,0};
int map[1010][1010];
int turn1[1010][1010];
struct state
{
int x,y;
int turn;
int dir;
}cur,next1;
void bfs(state temp)
{
temp.turn = 0;
temp.dir = -1;
turn1[temp.x][temp.y] = 0;
queue<state> q;
q.push(temp);
while(!q.empty())
{
cur = q.front();
q.pop();
if(cur.x == ex&&cur.y == ey)
{
if(cur.turn <= 2)
{
ok = 1;
printf("YES\n");
return;
}
}
if(cur.turn > 2) continue;
for(int i = 0;i < 4;i++)
{
next1.x = cur.x + d1[i];
next1.y = cur.y + d2[i];
next1.dir = i;
if(next1.x>0&&next1.x<=n&&next1.y>0&&next1.y<=m&&(map[next1.x][next1.y]==0||(next1.x==ex&&next1.y==ey)))
{ //这里要小心 一开始写成(map[next1.x][next1.y]==0||(map[next1.x][next1.y]==map[sx][sy]))
if(cur.dir!=i && cur.dir!=-1) //如果两个棋子是被相同数字的棋子挡住就会出现错误的判断
next1.turn = cur.turn+1;
else
next1.turn = cur.turn;
if(next1.turn <= turn1[next1.x][next1.y])
{
turn1[next1.x][next1.y] =next1.turn;
q.push(next1);
}
}
}
}
}
int main ()
{
while(scanf("%d%d",&n,&m)==2 && (n||m))
{
for(int i = 1;i <= n;i++)
for(int j = 1;j <= m;j++)
{
scanf("%d",&map[i][j]);
turn1[i][j] = INF;
}
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d%d",&sx,&sy,&ex,&ey);
for(int i = 1;i <= n;i++)
for(int j = 1;j <= m;j++)
turn1[i][j] = INF;//此处要记得初始化。。不然WA妥妥的,因为在之前的查询之中已经被破坏
ok = 0;
if(map[sx][sy]==map[ex][ey]&&map[sx][sy]*map[ex][ey]!=0&&(sx!=ex||sy!=ey))
{
cur.x = sx;
cur.y = sy;
bfs(cur);
if(!ok) printf("NO\n");
}
else
printf("NO\n");
}
}
return 0;
} 标签:
原文地址:http://blog.csdn.net/liujc_/article/details/44517581