标签:
2 5 5 ...** *.**. ..... ..... *.... 1 1 1 1 3 5 5 ...** *.**. ..... ..... *.... 2 1 1 1 3
no yes
#include<cstdio>
#include<cstring>
#include<queue>
#include<cstdlib>
#include<algorithm>
using namespace std;
int m,n,K,x1,x2,y1,y2;
char map[105][105];
int step[105][105];
int fx[4]={1,0,-1,0};
int fy[4]={0,1,0,-1};
struct node
{
int x,y;
};
int valid(node p)
{
if(p.x>=1&&p.x<=m&&p.y>=1&&p.y<=n)
return 1;
else
return 0;
}
queue<node> q;
void BFS()
{
node p1,p2;
p1.x=x1;
p1.y=y1;
while(!q.empty())
q.pop();
q.push(p1);
while(!q.empty())
{
p1=q.front();
for(int i=0;i<4;i++)
{
p2.x=p1.x+fx[i];
p2.y=p1.y+fy[i];
while(valid(p2)&&map[p2.x][p2.y]!='*')//这个循环实在是太重要了,节省了很多时间啊
{
if(step[p2.x][p2.y]==-1)
{
q.push(p2);
step[p2.x][p2.y]=step[p1.x][p1.y]+1;
if(p2.x==x2&&p2.y==y2)
{
if(step[p2.x][p2.y]<=K)
printf("yes\n");
else
printf("no\n");
return ;
}
}
p2.x=p2.x+fx[i];
p2.y=p2.y+fy[i];
}
}
q.pop();
}
printf("no\n");
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&m,&n);
for(int i=1;i<=m;i++)
scanf("%s",map[i]+1);
scanf("%d%d%d%d%d",&K,&y1,&x1,&y2,&x2);
memset(step,-1,sizeof(step));
if(x1==x2&&y1==y2)
{
printf("yes\n");
continue;
}
BFS();
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/lh__huahuan/article/details/47340713