2 5 5 ...** *.**. ..... ..... *.... 1 1 1 1 3 5 5 ...** *.**. ..... ..... *.... 2 1 1 1 3
no yes
一个广搜,刚学的循环队列,感觉很好用。这一题和hdu1175类似,但是昨天把那道题代码改着写的提交就wrong,今天重写了一次,AC了,bug真的不好找。。。
题意:一个人想要从一个点到达另一个点,不能穿过障碍,且拐弯的次数有限制,问是否能到达。。
思路:每个点可能经过多次,用一个数组记录经过该点时这条路线的最少转弯次数,转弯次数大于标记值就不能入队。。。
#include"stdio.h"
#include"string.h"
#include"queue"
#include"vector"
using namespace std;
#define N 105
#define M 1000000
char g[N][N];
int mark[N][N];
int n,m,x1,x2,y1,y2,q;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
struct node
{
int x,y,d,mov;
}p[M];
int judge(int x,int y)
{
if(x>=0&&x<n&&y>=0&&y<m&&g[x][y]=='.')
return 1;
return 0;
}
int bfs()
{
int i,l,r,x,y;
l=r=0;
for(i=0;i<4;i++)
{
p[r].x=x1;
p[r].y=y1;
p[r].d=i;
p[r++].mov=0;
}
while(l!=r)
{
for(i=0;i<4;i++)
{
x=p[r].x=dx[i]+p[l].x;
y=p[r].y=dy[i]+p[l].y;
if(judge(x,y)==0)
continue;
p[r].d=i;
p[r].mov=p[l].mov;
if(p[l].d!=i)
p[r].mov++;
if(x==x2&&y==y2&&p[r].mov<=q)
return 1;
if(p[r].mov>mark[x][y])
continue;
mark[x][y]=p[r].mov;
r++;
if(r>=M)
r=0;
}
l++;
if(l>=M)
l=0;
}
return 0;
}
int main()
{
int T,i,j;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(i=0;i<n;i++)
scanf("%s",g[i]);
scanf("%d%d%d%d%d",&q,&y1,&x1,&y2,&x2);
x1--;x2--;y1--;y2--;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
mark[i][j]=q+1;
}
}
if(bfs())
printf("yes\n");
else
printf("no\n");
}
return 0;
}
hdu 1728 逃离迷宫 (bfs+循环队列),布布扣,bubuko.com
原文:http://blog.csdn.net/u011721440/article/details/38434759