hdu 1728 逃离迷宫 bfs

蛋疼,犯了2个错误

1.不能每遇到个点都把它四个方向的每个点都走一个(每个点入一次队列),这样要mle

2.不能把已经走过的点标记成‘*’,这样要wa

View Code
#include <stdio.h>
#include <queue>
using namespace std;

struct node{
int x,y,cnt;
}a;

int n,m,ex,ey,k;
char map[105][105];

int bfs()
{
int i,row,col,dir[4][2]={0,1,1,0,-1,0,0,-1};
queue<node> q;
q.push(a);
while (!q.empty())
{
a=q.front();q.pop();
if(a.cnt==k+1)return 0;
for (i=0;i<4;i++)
{
row=dir[i][0]+a.x;
col=dir[i][1]+a.y;
while (row>=0&&col>=0&&row<n&&col<m&&map[row][col]!='*')
{
if(row==ex-1&&col==ey-1)return 1;
if(map[row][col]=='.')
{
map[row][col]='@';
node b;
b.x=row,b.y=col,b.cnt=a.cnt+1;
q.push(b);
}
row+=dir[i][0];
col+=dir[i][1];
}
}
}
return 0;
}

int main()
{
int T,i,sx,sy;
scanf("%d",&T);
while (T--)
{
scanf("%d%d",&n,&m);
for (i=0;i<n;i++)scanf("%s",map[i]);
scanf("%d%d%d%d%d",&k,&sy,&sx,&ey,&ex);
if(sy==ey&&sx==ex)
{
printf("yes\n");
continue;
}
map[sx-1][sy-1]='*';
a.x=sx-1,a.y=sy-1,a.cnt=0;
if(bfs())printf("yes\n");
else printf("no\n");
}
return 0;
}
/*
5 5
.....
.....
....*
.....
.....
1 1 1 5 5
*/



posted @ 2011-12-22 17:59  104_gogo  阅读(174)  评论(0编辑  收藏  举报