每次直走一个方向
这样走到底后,换方向就相当于一次转弯了
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
#include <iostream>
#include <queue>
using namespace std;
int m,n,sx,sy,ex,ey,k,dir[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};
char map[120][120];
struct node
{
int x,y,s;
};
void BFS()
{
queue<node> q;
node f,l;
f.x = sx;
f.y = sy;
f.s = -1;
q.push(f);
while (!q.empty())
{
f = q.front();
q.pop();
map[f.x][f.y] = '*';
for (int i = 0;i < 4;i++)
{
l.x = f.x + dir[i][0];
l.y = f.y + dir[i][1];
while (l.x > 0 && l.x <= m && l.y > 0 && l.y <= n)
{
if(map[l.x][l.y] == '*')
break;
l.s = f.s + 1;
if(l.x == ex && l.y == ey && l.s <= k)
{
printf("yes\n");
return;
}
if(l.s <= k)
q.push(l);
l.x = l.x + dir[i][0];
l.y = l.y + dir[i][1];
}
}
}
printf("no\n");
}
int main()
{
int i,j,t;
scanf("%d",&t);
while (t--)
{
scanf("%d %d",&m,&n);
for (i = 1;i <= m;i++)
for(j = 1;j <= n;j++)
cin >> map[i][j];
scanf("%d %d %d %d %d",&k,&sy,&sx,&ey,&ex);
if(sx == ex && sy == ey)
printf("yes\n");
else
BFS();
}
return 0;
}