U238758 广度优先搜索(BFS)
题目背景
## 思想
$1、$将起点入队,标记。
$2、$四个方向搜索邻居。
$3、$将**能走到**的邻居(们)入队,并标记。
$4、$重复直到走通或者走不通。
---
### 特殊点:需要用到队列进行辅助存储
---
## 代码
```cpp
#include
#include
using namespace std;
const int N = 50;
char a[N][N];
int b[N][N]; // 入队标记
int n,m;
int g[4][2] = {1,0,0,1,-1,0,0,-1};
struct node {
int x,y,d; //到(x,y)的最短步数是d
};
int bfs(int sx,int sy){
queue q;
q.push({sx,sy,0});//存储一个点
b[sx][sy] = 1;
while (!q.empty()){
node t = q.front();
q.pop();
if (t.x == n && t.y == m) return t.d; //返回最短步数
for (int i = 0;i < 4;i++){
int tx = t.x + g[i][0];
int ty = t.y + g[i][1];
if (tx >= 1 && tx = 1 && ty > n >> m;
for (int i = 1;i a[i][j];
}
}
cout
题目描述
无
输入格式
无
输出格式
无