- 简单的BFS 题目 ,就是条件之间的逻辑要理清
- 根据题目,转弯次数不能超过要求
其中 的 吧 VIS 条件 独立出来写,是 为了让所有点扩展出来,而非把VIS的点 当作墙。
?
为了方便以后复习这种模板题。。我们需要的是
-
- 地图矩阵 2.标记数组 3. 位移数组 4.以及 结构体 5.队列(先进先出)
#include<iostream>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
char map[105][105];
int vis[105][105];
int step[4][2] = { { 1, 0 }, { 0, 1 }, { -1, 0 }, { 0, -1 } };
int m, n,k;
struct Node
{
int x, y, time;
}Start,End;
int bfs()
{
Start.time = -1;
Node now, next;
queue<Node>q;
q.push(Start);
while (!q.empty())
{
now = q.front(); q.pop();
if (now.time >= k) continue;
for (int i = 0; i < 4; i++)
{
next = now;
next.time++;
while (1)
{
next.x = next.x + step[i][0];
next.y = next.y + step[i][1];
if (next.x >= 0 && next.x < m && next.y >= 0 && next.y < n && map[next.x][next.y] == '.' && next.time <= k)
{
if (next.x == End.x && next.y == End.y)
{
return 1;
}
if (!vis[next.x][next.y]) //该条件 不能放在 上面的IF里面
{
q.push(next);
vis[next.x][next.y] = 1;
}
}
else break;
}
}
}
return 0;
}
int main()
{
//freopen("debug//in.txt", "r", stdin);
//freopen("debug//out.txt", "w", stdout);
int t;
cin >> t;
while (t--)
{
cin >> m >> n;
for (int i = 0; i < m;i++)
for (int j = 0; j < n; j++)
{
cin >> map[i][j];
}
cin >> k >> Start.y >> Start.x >> End.y >> End.x;
Start.x--, Start.y--, End.x--, End.y -- ;
memset(vis, 0, sizeof(vis));
vis[Start.x][Start.y] = 1;
if (bfs())
cout << "yes" << endl;
else cout << "no" << endl;
}
//fclose(stdin);
//system("pause");
//fclose(stdout);
return 0;
}