原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1728
#include <iostream>
#include <queue>
using namespace std;
int r,c,k,visit[101][101];
char map[101][101];
int dir_x[4] = {0,1,0,-1},dir_y[4] = {1,0,-1,0};
typedef struct
{
int x;
int y;
int turn;//转弯次数
}node;
node s,e;
void bfs()
{
queue<node> que;
node first,next;
if(s.x == e.x && s.y == e.y)//特殊情况,起点和终点相同
{
cout << "yes" << endl;
return;
}
s.turn = -1;//初始化起点的方向为-1,即起点到四周的转弯次数为0
que.push(s);
while(!que.empty())
{
first = que.front();
que.pop();
if(first.x == e.x && first.y == e.y && first.turn <= k)
{
cout << "yes" << endl;
return;
}
int i;
for(i = 0;i < 4;i++)
{
next.x = first.x + dir_x[i];
next.y = first.y + dir_y[i];
while(next.x >= 1 && next.x <= r && next.y >= 1 && next.y <= c && map[next.x][next.y] == '.')
{
if(visit[next.x][next.y] == 0)//没被访问过
{
next.turn = first.turn + 1;
visit[next.x][next.y] = 1;//标记已经访问过了
que.push(next);
}
next.x += dir_x[i];//确定一个方向,一直朝这个方向走,直到不能走位置才改变方向
next.y += dir_y[i];
}
}
}
cout << "no" << endl;
return;
}
int main(int argc, char *argv[])
{
int t,i,j,x1,y1,x2,y2;
cin >> t;
while(t--)
{
memset(visit,0,sizeof(visit));
cin >> r >> c;
for(i = 1;i <= r;i++)
for(j = 1;j <= c;j++)
cin >> map[i][j];
cin >> k >> x1 >> y1 >> x2 >> y2;
s.x = y1;
s.y = x1;
e.x = y2;
e.y = x2;
bfs();
}
return 0;
}