赞
踩
BFS(Breadth-First Search)广度优先搜索
,是一种图搜索算法,用于在图或树等数据结构中遍历或搜索节点。
BFS
从起始节点开始,逐层地访问其相邻节点,直到找到目标节点或遍历完所有节点。
BFS
的工作原理是先访问起始节点,然后按照距离起始节点的距离进行遍历。具体过程如下:
BFS
的特点是先访问距离起始节点近的节点,然后逐渐向离起始节点更远的节点进行扩展。因此,当在图中搜索最短路径或寻找最近邻节点时,BFS
是一种常用的算法。
需要注意的是,BFS使用队列来存储待访问的节点,因此它是一种先进先出(First-In-First-Out,FIFO)的算法。此外,为了避免重复访问节点,需要使用一个标记数组或哈希表来记录节点的访问状态。
一般来说当图的边权都为1时使用BFS,边权为1表示从一个节点到相邻节点的距离或代价是相等的,这意味着每个相邻节点离起始节点的距离相差一个单位。在这种情况下,BFS
可以确保首次到达目标节点时的路径长度最小。
如果图中存在边权不为1的情况,BFS可能无法得到最短路径。 对于带有不同边权的图,更适合使用Dijkstra算法
或A*算法
等其他路径搜索算法,它们可以考虑不同边权的影响,找到最优路径。
题目描述:
给定一个
n
×
m
n×m
n×m 的二维整数数组,用来表示一个迷宫,数组中只包含
0
0
0 或
1
1
1,其中
0
0
0 表示可以走的路,
1
1
1 表示不可通过的墙壁。
最初,有一个人位于左上角 ( 1 , 1 ) (1,1) (1,1) 处,已知该人每次可以向上、下、左、右任意一个方向移动一个位置。
请问,该人从左上角移动至右下角 ( n , m ) (n,m) (n,m) 处,至少需要移动多少次。
数据保证 ( 1 , 1 ) (1,1) (1,1) 处和 ( n , m ) (n,m) (n,m) 处的数字为 0 0 0,且一定至少存在一条通路。
输入格式:
第一行包含两个整数 n 和 m。
接下来 n 行,每行包含 m 个整数(0 或 1),表示完整的二维数组迷宫。
输出格式:
输出一个整数,表示从左上角移动至右下角的最少移动次数。
数据范围:
1
≤
n
,
m
≤
100
1 ≤ n,m ≤ 100
1≤n,m≤100
输入样例:
5 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
输出样例:
8
#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<queue> using namespace std; const int N = 110; int d[N][N], g[N][N], n, m; typedef pair<int, int> PII; int bfs() { queue<PII> q; memset(d, -1, sizeof(d)); d[0][0] = 0; q.push({0, 0}); int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1}; while (q.size()) { auto t = q.front(); q.pop(); for (int i = 0; i < 4; ++i) { int x = t.first + dx[i], y = t.second + dy[i]; 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; q.push({x, y}); } } } return d[n - 1][m - 1]; } int main() { cin.tie(0); ios::sync_with_stdio(false); 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; }
在BFS中添加路径回溯,在找到目标节点后打印出从起点到终点的最短路径坐标。
#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<queue> #include<vector> using namespace std; const int N = 110; int d[N][N], g[N][N], n, m, tt = -1, hh = 0, tail = -1; typedef pair<int, int> PII; PII q[N * N]; int bfs() { q[++tt] = { 0, 0 }; memset(d, -1, sizeof d); d[0][0] = 0; int dx[4]{ -1, 0, 1, 0 }, dy[4]{ 0, -1, 0, 1 }; while (hh <= tt) { auto t = q[hh++]; for (int i = 0; i < 4; i++) { int x = t.first + dx[i], y = t.second + dy[i]; if (x < n && x >= 0 && y < m && y >= 0 && d[x][y] == -1 && g[x][y] == 0) { d[x][y] = d[t.first][t.second] + 1; q[++tt] = { x, y }; } } } // 打印最短路径 vector<PII> path; int x = n - 1, y = m - 1; while (x || y) { path.push_back({ x, y }); for (int i = 0; i < 4; i++) { int nx = x + dx[i], ny = y + dy[i]; if (nx >= 0 && nx < n && ny >= 0 && ny < m && d[nx][ny] == d[x][y] - 1) { x = nx; y = ny; break; } } } path.push_back({ 0, 0 }); cout << "Shortest Path Coordinates:" << endl; for (int i = path.size() - 1; i >= 0; i--) { cout << "(" << path[i].first << ", " << path[i].second << ")" << endl; } return d[n - 1][m - 1]; } int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> n >> m; for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) cin >> g[i][j]; cout << "Shortest Path Length: " << bfs() << endl; return 0; }
用DFS实现在迷宫中寻找出路经并返回路径坐标。
#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<vector> using namespace std; const int N = 110; int g[N][N]; int n, m; int dx[4]{ 0, 1, 0, -1 }, dy[4]{ 1, 0, -1, 0 }; typedef pair<int, int> PII; vector<PII> path; bool st[N][N]; void dfs(int x, int y) { if (x == n - 1 && y == m - 1) { for (auto e : path) cout << '(' << e.first << ", " << e.second << ')' << ' '; cout << endl; } for (int i = 0; i < 4; ++i) { int a = x + dx[i], b = y + dy[i]; if (a >= 0 && a < n && b >= 0 && b < m && !st[a][b] && g[a][b] == 0) { st[a][b] = true; path.push_back({a, b}); dfs(a, b); path.pop_back(); st[a][b] = false; } } } int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> n >> m; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) cin >> g[i][j]; } path.push_back({0, 0}); st[0][0] = true; dfs(0, 0); return 0; }
单点时限: 2.0sec
内存限制: 256MB
题目描述
一天,sunny 不小心进入了一个迷宫,不仅很难寻找出路,而且有的地方还有怪物,但是 sunny 有足够的能力杀死怪物,但是需要一定的时间,但是 sunny 想早一点走出迷宫,所以请你帮助他计算出最少的时间走出迷宫,输出这个最少时间。
我们规定每走一格需要时间单位 1
,杀死怪物也需要时间 1
,如果不能走到出口,则输出 impossible
,每次走只能是上下左右 4
个方向。
输入格式:
每次首先
2
2
2 个数
n
,
m
n,m
n,m
(
0
<
n
,
m
≤
200
)
(0<n,m≤200)
(0<n,m≤200),代表迷宫的高和宽,然后
n
n
n 行,每行
m
m
m 个字符。
S
代码你现在所在的位置。
T
代表迷宫的出口。
#
代表墙,你是不能走的。
X
代表怪物。
.
代表路,可以走。
处理到文件结束。
输出格式:
输出最少的时间走出迷宫。不能走出则输出impossible
。
输入样例:
4 4
S.X.
#..#
..#.
X..T
4 4
S.X.
#..#
..#.
.#.T
输出样例:
6
impossible
(BFS + 优先队列)
题意:走迷宫,求最短路径,上下左右走一格花费 1
,走到有怪的格子花费 2
。
将每一点的坐标和由起点到该点的距离存入结构体.
由起点开始,将该点存入优先队列,以到起点的距离 dis
为优先级,每次取出 dis
最小的,向外扩散。
相当于第一轮得出所有到起点距离为1的点,第二轮得出所有到起点距离为2的点。
注意:对普通的最短路问题,由于每个各自的花费相同,因此每次存入的点优先级都相同。故不需要使用优先队列,但本题存在有无怪物的区别,每次存入的格子的优先级可能不同,故使用优先队列。
#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<queue> using namespace std; const int N = 110; struct Node { int x, y, dis; bool const operator<(const Node& b) const { return dis > b.dis; } }; char g[N][N]; int sx, sy, tx, ty; int dx[4]{1, 0, -1, 0}, dy[4]{0, 1, 0, -1}; int n, m; void bfs() { priority_queue<Node> q; Node st{ sx, sy, 0 }; g[sx][sy] = '#'; q.push(st); while (q.size()) { Node u = q.top(); q.pop(); if (u.x == tx && u.y == ty) { cout << u.dis << endl; return; } for (int i = 0; i < 4; ++i) { int x = u.x + dx[i]; int y = u.y + dy[i]; Node t = { x, y, u.dis }; if (x >= 0 && x < n && y >= 0 && y < m && g[x][y] != '#') { if (g[x][y] == 'X') t.dis += 2; else t.dis += 1; g[x][y] = '#'; q.push(t); } } } cout << "impossible" << endl; } int main() { cin.tie(0); ios::sync_with_stdio(false); while (cin >> n >> m) // 持续输入,Ctrl + z退出 { for (int i = 0; i < n; ++i) { cin >> g[i]; for (int j = 0; j < m; ++j) { if (g[i][j] == 'S') sx = i, sy = j; if (g[i][j] == 'T') tx = i, ty = j; } } bfs(); } return 0; }
在BFS中添加路径回溯,在找到目标节点后打印出从起点到终点的最短路径坐标。
#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<queue> #include<vector> using namespace std; const int N = 110; struct Node { int x, y, dis; bool const operator<(const Node& b) const { return dis > b.dis; } }; char g[N][N]; int sx, sy, tx, ty; int dx[4]{ 1, 0, -1, 0 }, dy[4]{ 0, 1, 0, -1 }; int n, m; void bfs() { priority_queue<Node> q; pair<int, int> path[N][N]; Node st{ sx, sy, 0}; g[sx][sy] = '#'; q.push(st); while (q.size()) { Node u = q.top(); q.pop(); if (u.x == tx && u.y == ty) { cout << u.dis << endl; vector<pair<int, int>> shortest_path; shortest_path.push_back({ tx, ty }); int i = tx, j = ty; while (i != sx || j != sy) { int x = path[i][j].first, y = path[i][j].second; i = x, j = y; shortest_path.push_back({ i, j }); } for (int k = shortest_path.size() - 1; k >= 0; k--) cout << '(' << shortest_path[k].first << ", " << shortest_path[k].second << ')' << endl; return; } for (int i = 0; i < 4; ++i) { int x = u.x + dx[i]; int y = u.y + dy[i]; Node t = { x, y, u.dis }; if (x >= 0 && x < n && y >= 0 && y < m && g[x][y] != '#') { path[x][y] = { u.x, u.y }; if (g[x][y] == 'X') t.dis += 2; else t.dis += 1; g[x][y] = '#'; q.push(t); } } } cout << "impossible" << endl; } int main() { cin.tie(0); ios::sync_with_stdio(false); while (cin >> n >> m) { for (int i = 0; i < n; ++i) { cin >> g[i]; for (int j = 0; j < m; ++j) { if (g[i][j] == 'S') sx = i, sy = j; if (g[i][j] == 'T') tx = i, ty = j; } } bfs(); } return 0; }
题目描述:
在一个 3×3
的网格中,1∼8
这 8
个数字和一个 x
恰好不重不漏地分布在这 3×3
的网格中。
例如:
1 2 3
x 4 6
7 5 8
在游戏过程中,可以把 x
与其上、下、左、右四个方向之一的数字交换(如果存在)。
我们的目的是通过交换,使得网格变为如下排列(称为正确排列):
1 2 3
4 5 6
7 8 x
例如,示例中图形就可以通过让 x
先后与右、下、右三个方向的数字交换成功得到正确排列。
交换过程如下:
1 2 3 1 2 3 1 2 3 1 2 3
x 4 6 4 x 6 4 5 6 4 5 6
7 5 8 7 5 8 7 x 8 7 8 x
现在,给你一个初始网格,请你求出得到正确排列至少需要进行多少次交换。
输入格式:
输入占一行,将 3×3
的初始网格描绘出来。
例如,如果初始网格如下所示:
1 2 3
x 4 6
7 5 8
则输入为:1 2 3 x 4 6 7 5 8
输出格式:
输出占一行,包含一个整数,表示最少交换次数。
如果不存在解决方案,则输出 −1
。
输入样例:
2 3 4 1 5 x 7 6 8
输出样例:
19
#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<queue> #include <unordered_map> using namespace std; int bfs(string start) { queue<string> q; unordered_map<string, int> d; q.push(start); d[start] = 0; int dx[4]{1, 0, -1, 0}, dy[4]{0, 1, 0, -1}; string end = "12345678x"; while (q.size()) { auto t = q.front(); q.pop(); if (t == end) return d[t]; int distance = d[t]; int k = t.find('x'); int x = k / 3, y = k % 3; for (int i = 0; i < 4; i++) { int a = x + dx[i], b = y + dy[i]; if (a >= 0 && a < 3 && b >= 0 && b < 3) { swap(t[a * 3 + b], t[k]); if (!d.count(t)) // count(key)用于检查键key是否存在于映射中 { d[t] = distance + 1; // 记录转换次数 q.push(t); } swap(t[a * 3 + b], t[k]); // 还原状态遍历其他转换 } } } return -1; } int main() { string start; char c; for (int i = 0; i < 9; ++i) { cin >> c; start += c; } cout << bfs(start) << endl; return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。