BFS
董晓的宽搜模板
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 100010;
int n, m, a, b;
vector<int> e[N];
int vis[N];
queue<int> q;//1.创建队列
void bfs(int s) {
vis[s] = 1; q.push(s);//2.压入起点
while (q.size()) {//3.只要队列不空,从对头取出这个点
int x = q.front(); q.pop();
// printf("%d出队\n",x);
for (auto y : e[x]) {//4.然后打探照灯
if (vis[y]) continue;
vis[y] = 1; q.push(y);
// printf(" %d入队\n",y);
}
}
}
int main() {
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> a >> b;
//建立邻接点的关系
e[a].push_back(b);
e[b].push_back(a);
}
bfs(1);
return 0;
}
董晓的走迷宫
我认为优于y总的
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std;
typedef pair<int, int> PII;
const int N = 110;
int n;
int g[N][N]; //g[x][y]==0这个点是空地,可以走;g[x][y]==1这个点是障碍物或已经走过的点,就不能走
int dx[4] = { -1, 1, 0, 0 };
int dy[4] = { 0, 0, -1, 1 };
PII pre[N][N];//打印路径使用,记录前一个点是哪个(前驱点)
void bfs(int x,int y) {
queue<PII> q;//1.创建队列
q.push({ x, y });//2.压入起点
g[x][y] = 1;
while (q.size()) {//3.只要队列不空,从头取出这个点
auto u = q.front();
q.pop();
for (int i = 0; i < 4; ++i) {//4.然后打探照灯
int a = u.first + dx[i], b = u.second + dy[i];//A.获取这个点
if (a < 0 || a >= n || b < 0 || b >= n)continue;//B.判越界
if (g[a][b]) continue;//C.判重
g[a][b] = 1;//打标记
pre[a][b] = u;//记录路径点(x,y)的上一步是点t
q.push({ a, b });
}
}
}
void print(int x, int y) {
//从后往前打印,打印到0返回
if (x == 0 && y == 0)return;
PII p = pre[x][y];
print(p.first, p.second);
printf("%d %d\n", x, y);
}
int main() {
cin >> n ;
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
cin >> g[i][j];
bfs(0, 0);
puts("0 0");
print(n - 1, n - 1);
return 0;
}
/*
测试数据
输入
5
0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0
输出
0 0
1 0
2 0
2 1
2 2
2 3
2 4
3 4
4 4
*/
acwing走迷宫
pair模拟队列
/**
* BFS模板套路
* 1. 将初始状态加入队列queue
* 2. while queue不空
* 3. {
* 3.1 t <- 队头(每次将队头拿出来)
* 3.2 扩展队头
* }
*/
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstring>
#include <stack>
using namespace std;
typedef pair<int, int> PII;
const int N = 110;
int m, n;
int g[N][N], d[N][N];
int dx[4] = { -1, 1, 0, 0 };
int dy[4] = { 0, 0, -1, 1 };
//PII Prev[N][N];//打印路径使用,记录前一个点是哪个
//stack<PII> path;//打印路径使用,用于顺序输出路径
int bfs() {
memset(d, -1, sizeof(d));
queue<PII> q;//1.创建队列
d[0][0] = 0;
q.push({ 0, 0 });//2.压入起点
while (!q.empty()) {//3.只要队列不空,从头取出这个点
auto t = q.front();
q.pop();
for (int i = 0; i < 4; ++i) {//4.然后打探照灯
int x = t.first + dx[i], y = t.second + dy[i];//A.获取这个点
if (x < 0 || x >= n || y < 0 || y >= m)continue;//B.判越界
if (g[x][y]) continue;//C.判重
g[x][y] = 1;//打标记
prev[x][y] = t;//记录路径点(x,y)的上一步是点t
q.push({ x, y });
d[x][y] = d[t.first][t.second] + 1;
}
}
// // 打印路径使用
// int x = n - 1, y = m - 1;
// while (x || y) {
// path.push({x, y});
// auto t = Prev[x][y];
// x = t.first, y = t.second;
// }
// path.push({x, y});
// while (!path.empty()) {
// auto node = path.top();
// cout << node.first << ' '<< node.second << endl;
// path.pop();
// }
return d[n - 1][m - 1];
}
int main() {
cin >> n >> m;
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
cin >> g[i][j];
cout << bfs() << endl;
return 0;
}
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
typedef pair<int, int>PII;
const int N = 110;
int n, m;//n行,每行m个正数(0或1)表示地图 g[n][m]
int g[N][N];
int d[N][N];
PII q[N * N], prev1[N][N];//记录点(N,N)前面的点是哪个PII;
int bfs() {
int hh = 0, tt = 0;
q[0] = { 0,0 };
//d存的是距离,同时如果d为-1说明没走过
memset(d, -1, sizeof(d));
d[0][0] = 0;
//dx,dy的0要错开
int dx[4] = { -1,1,0,0 }, dy[4] = { 0,0,-1,1 };
while (hh <= tt)
{
auto t = q[hh++];
for (int i = 0; i < n; i++)
{
int x = t.first + dx[i], y = t.second + dy[i];
//g[x][y]==0(这个点是空地,可以走;如果为1就不能走)
//d[x][y]==-1(这个点没走过,可以走;如果为0就不能走)
if (x >= 0 && x < n && y >= 0 && y < m && g[x][y] == 0 && d[x][y] == -1)
{
d[x][y] = d[t.first][t.second] + 1;
prev1[x][y] = t;//点(x,y)从点t过来
q[++tt] = { x,y };
}
}
}
//返回右下角点的距离
return d[n - 1][m - 1];
}
//输出路径
void print() {
int x = n - 1, y = m - 1;
while (x || y)
{
cout << x << ' ' << y << endl;
auto t = prev1[x][y];
x = t.first, y = t.second;
}
}
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
cin >> g[i][j];
cout << bfs() << endl;
return 0;
}