链接: Problem - 1728
这是一道BFS模版题 。AC 代码如下:
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e3;
const int INF=1<<31-1;
char str[maxn][maxn+1];
int dx[4]={-1,0,1,0};
int dy[4]={0,1,0,-1};
int sx,sy;
int gx,gy;
int n,m;
int k;
int d[maxn][maxn];
typedef pair<int,int>p;
int check(int nx,int ny)
{
if(0<=nx&&nx<n&&0<=ny&&ny<m&&str[nx][ny]!='*')
return 1;
else return 0;
}
int bfs()
{
queue<p>que;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
d[i][j]=INF;
que.push(p(sx,sy));
d[sx][sy]=-1;
while(que.size())
{
p u=que.front();
que.pop();
if(u.first==gx&&u.second==gy&&d[gx][gy]<=k)
{
return 1;
}
for(int i=0;i<4;i++)
{
int nx=u.first+dx[i],ny=u.second+dy[i];
while(check(nx,ny))
{
if(d[nx][ny]==INF)
{
que.push(p(nx,ny));
d[nx][ny]=d[u.first][u.second]+1;
if(nx==gx&&ny==gy&&d[nx][ny]<=k)
{
return 1;
}
}
nx+=dx[i];
ny+=dy[i];
}
}
}
return 0;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
memset(str,0,sizeof(str));
for(int i=0;i<n;i++)
scanf("%s",str[i]);
scanf("%d%d%d%d%d",&k,&sy,&sx,&gy,&gx);
sy--,sx--,gy--,gx--;
int h=bfs();
if(sx==gx&&sy==gy)
printf("yes\n");
else if(h)
printf("yes\n");
else
printf("no\n");
}
return 0;
}