求在转弯的范围内 是否能到目标点,用广搜 这和以往的不同不是把遍历过的坐标标记
不再使用, 而是 从不同的方向到莫个点的最优解放进队列;用一个数组记录从不同方向到每个点的
转弯的次数,然后比较从莫个点到一个点的转弯数和以前在这个点的最优解比较,
如果小就放入队列;大就放弃;
这题主要是遍历的条件;
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int inf = 1000000;
char map[110][110];//建图
int vis[110][110];//记录点的最优解;
int k, x1, x2, y1, y2, flag, m, n;
int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
struct node
{
int x, y;
int num;//次数
int dir;//这个点由哪个方向来的;
};
bool cheak(int x, int y)//判断是否满足范围;
{
if(x >= 0 && x < m && y >= 0 && y < n && map[x][y] != '*')
return true;
return false;
}
void bfs()
{
queue<node>q;
node temp, type;
temp.x = x1;
temp.y = y1;
temp.num = -1;
temp.dir = -1;
q.push(temp);
while(!q.empty())
{
temp = q.front();
q.pop();
if(temp.x == x2 && temp.y == y2 && temp.num <= k)
{
flag = 1;
return ;
}
for(int i = 0; i < 4; i++)
{
int fx = temp.x + dir[i][0];
int fy = temp.y + dir[i][1];
if(cheak(fx, fy))
{
if(temp.dir == -1)//kaishi
{
type.dir = i;
type.num = 0;
type.x = fx;
type.y = fy;
if(vis[fx][fy] >= type.num)
{
vis[fx][fy] = temp.num;
q.push(type);
}
}
else
{
if(i == temp.dir)//新的坐标和它的上个比较,如果相同就不转弯;
{
type.dir = i;
type.num = temp.num;
type.x = fx;
type.y = fy;
if(type.num <= vis[fx][fy])
{
vis[fx][fy] = type.num;
q.push(type);
}
}
else
{
type.dir = i;
type.num = temp.num + 1;
type.x = fx;
type.y = fy;
if(type.num <= vis[fx][fy])
{
vis[fx][fy] = type.num;
q.push(type);
}
}
}
}
}
}
return ;
}
int main()
{
int t;
cin >> t;
while(t--)
{
cin >> m >> n;
for(int i = 0; i < m; i++)
cin >> map[i];
for(int i = 0; i < m; i++)
for(int j = 0; j < n; j++)
vis[i][j] = inf;
cin >> k >> y1 >> x1 >> y2 >> x2;
x1 -= 1;//因为 图是按0开始的
x2 -= 1;
y1 -= 1;
y2 -= 1;
flag = -1;
if(x1 == x2 && y1 == y2)
{
cout << "yes" << endl;
continue;
}
bfs();
if(flag == -1)
cout << "no" <<endl;
else
cout << "yes" << endl;
}
return 0;
}