象与马
题意
在 个格点上,马走日、象飞田,双方轮流且可连续走,求两者相遇(一方吃掉另一方)的最少总步数,否则输出 。
思路
分别 BFS 求马和象到所有格点的最短步数,枚举每个格点,若双方步数均 ≥1,则用步数之和更新答案,取最小值。
代码
#include <bits/stdc++.h>
using namespace std;
const int N = 1e4+5;
struct P {
int x, y, step;
};
int n, hx, hy, ex, ey;
bool vis[N][N], vis2[N][N];
int res[N][N], res2[N][N];
int d[8][2]={ {-2, -1}, {-2, 1}, {-1, -2}, {-1, 2}, {1, -2}, {1, 2}, {2, -1}, {2, 1} };
int f[4][2]={ {-2, -2}, {-2, 2}, {2, -2}, {2, 2} };
int mp[N][N];
void bfs () {
queue<P> q;
q.push (P{hx, hy, 0});
vis[hx][hy]=1;
while (q.size()) {
P now=q.front();
q.pop();
res[now.x][now.y]=now.step;
for (int i=0; i<8; i++) {
int nx = now.x+d[i][0], ny = now.y+d[i][1];
if (nx<0 or nx>n or ny<0 or ny>n) continue;
if (vis[nx][ny]) continue;
q.push (P{nx, ny, now.step+1});
vis[nx][ny]=1;
}
}
}
void bfs2 () {
queue<P> q2;
q2.push (P{ex, ey, 0});
vis2[ex][ey]=1;
while (q2.size()) {
P now2=q2.front();
q2.pop();
res2[now2.x][now2.y]=now2.step;
for (int i=0; i<4; i++) {
int nx = now2.x+f[i][0], ny = now2.y+f[i][1];
if (nx<0 or nx>n or ny<0 or ny>n) continue;
if (vis2[nx][ny]) continue;
q2.push (P{nx, ny, now2.step+1});
vis2[nx][ny]=1;
}
}
}
int main() {
cin >> n >> hx >> hy >> ex >> ey;
bfs ();
bfs2 ();
int minn=INT_MAX;
for (int i=0; i<=n; i++) {
for (int j=0; j<=n; j++) {
if (vis[i][j]!=0 and vis2[i][j]!=0) {
minn=min (minn, res[i][j]+res2[i][j]);
} else {
res[i][j]=0;
res2[i][j]=0;
}
}
}
if (minn==INT_MAX) {
cout << -1;
} else cout << minn;
return 0;
}总结
本质是双源 BFS 求相遇最小步数和,需排除初始位置相遇(双方至少走一步)。
好奇怪的游戏
题意
黑白两马从给定起点出发,可走“日”或“田”字步,不能走到坐标 ≤0 的位置,求各自到 (1,1) 的最少步数。
思路
对每个起点分别 BFS,步数方向数组包含 8 个“日”字和 4 个“田”字移动,边界设为 1~30 保证能搜到 (1,1)。
代码
#include <bits/stdc++.h>
using namespace std;
const int N = 1e4+5;
struct P {
int x, y, step;
};
int n, hx, hy, ex, ey;
bool vis[N][N], vis2[N][N];
int res[N][N], res2[N][N];
int d[12][2]={ {-2, -1}, {-2, 1}, {-1, -2}, {-1, 2}, {1, -2}, {1, 2}, {2, -1}, {2, 1}, {-2, -2}, {-2, 2}, {2, -2}, {2, 2} };
int mp[N][N];
void bfs () {
queue<P> q;
q.push (P{hx, hy, 0});
vis[hx][hy]=1;
while (q.size()) {
P now=q.front();
q.pop();
res[now.x][now.y]=now.step;
for (int i=0; i<12; i++) {
int nx = now.x+d[i][0], ny = now.y+d[i][1];
if (nx<1 or nx>30 or ny<1 or ny>30) continue;
if (vis[nx][ny]) continue;
q.push (P{nx, ny, now.step+1});
vis[nx][ny]=1;
}
}
cout << res[1][1] << '\n';
}
void bfs2 () {
queue<P> q2;
q2.push (P{ex, ey, 0});
vis2[ex][ey]=1;
while (q2.size()) {
P now2=q2.front();
q2.pop();
res2[now2.x][now2.y]=now2.step;
for (int i=0; i<12; i++) {
int nx = now2.x+d[i][0], ny = now2.y+d[i][1];
if (nx<1 or nx>30 or ny<1 or ny>30) continue;
if (vis2[nx][ny]) continue;
q2.push (P{nx, ny, now2.step+1});
vis2[nx][ny]=1;
}
}
cout << res2[1][1];
}
int main() {
cin >> hx >> hy >> ex >> ey;
bfs ();
bfs2 ();
return 0;
}总结
双源 BFS 分别搜索,扩展规则为马步加象步,输出各自到达 (1,1) 的最短步数。
血色先锋队
题意
多源点同时向外四方向扩散,求每个询问点被感染的最短时间。
思路
将所有感染源作为起点一次性放入队列,BFS 逐层扩散,记录每个格子首次被访问时的步数。
代码
#include <bits/stdc++.h>
using namespace std;
const int N = 1e4+5;
struct P {
int x, y, step;
};
int n, m, a, b;
bool vis[N][N];
int res[N][N];
int d[4][2]={ {0, -1}, {-1, 0}, {1, 0}, {0, 1} };
int mp[N][N];
void bfs () {
queue<P> q;
for (int i=1; i<=a; i++) {
int X, Y;
cin >> X >> Y;
q.push (P{X, Y, 0});
vis[X][Y]=1;
}
while (q.size()) {
P now=q.front();
q.pop();
res[now.x][now.y]=now.step;
for (int i=0; i<4; i++) {
int nx = now.x+d[i][0], ny = now.y+d[i][1];
if (nx<1 or nx>n or ny<1 or ny>m) continue;
if (vis[nx][ny]) continue;
q.push (P{nx, ny, now.step+1});
vis[nx][ny]=1;
}
}
}
int main() {
cin >> n >> m >> a >> b;
bfs();
for (int i=1; i<=b; i++) {
int c, d;
cin >> c >> d;
cout << res[c][d] << '\n';
}
return 0;
}总结
多源 BFS 一次性计算所有点的最短感染时间,离线查询输出。
Slikar
题意
在 网格上,水和人同时移动,水每分四方向漫延且不能进岩石和巢穴 D,人不能进水或岩石,也不能在将被淹的格子停留,求人从 S 到 D 的最短时间,否则输出 KAKTUS。
思路
先 BFS 预计算每个格子被水淹没的时间(水从所有 ’*’ 同时出发),再 BFS 人移动,仅当人到达某格的时间严格小于该格被水淹没的时间时才可扩展。
代码
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3+5;
struct P {
int x, y, step;
};
int n, m, cx, cy;
int res[N][N];
bool vis[N][N];
int d[4][2]={ {1, 0}, {0, 1}, {-1, 0}, {0, -1} };
char mp[N][N];
void bfs () {
queue<P> q;
memset (res, 0x3f, sizeof res);
for (int i=1; i<=n; i++) {
for (int j=1; j<=m; j++) {
if (mp[i][j]=='*') {
q.push (P{i, j, 0});
vis[i][j]=1;
}
}
}
while (q.size()) {
P now=q.front();
q.pop();
res[now.x][now.y]=now.step;
for (int i=0; i<4; i++) {
int nx=now.x+d[i][0], ny=now.y+d[i][1];
if (nx<1 or nx>n or ny<1 or ny>m) continue;
if (vis[nx][ny] or mp[nx][ny]=='D' or mp[nx][ny]=='X') continue;
q.push (P{nx, ny, now.step+1});
vis[nx][ny]=1;
}
}
}
void bfs2 () {
memset (vis, 0, sizeof vis);
queue<P> q;
int xx, yy;
for (int i=1; i<=n; i++) {
for (int j=1; j<=m; j++) {
if (mp[i][j]=='S') {
q.push (P{i, j, 0});
vis[i][j]=1;
}
}
}
while (q.size()) {
P now=q.front();
q.pop();
if (mp[now.x][now.y]=='D') {
cout << now.step;
return;
}
for (int i=0; i<4; i++) {
int nx=now.x+d[i][0], ny=now.y+d[i][1];
if (nx<1 or nx>n or ny<1 or ny>m) continue;
if (vis[nx][ny] or mp[nx][ny]=='X' or now.step+1>=res[nx][ny]) continue;
q.push (P{nx, ny, now.step+1});
vis[nx][ny]=1;
}
}
cout << "KAKTUS";
}
int main() {
cin >> n >> m;
for (int i=1; i<=n; i++) {
for (int j=1; j<=m; j++) {
cin >> mp[i][j];
}
}
bfs ();
bfs2 ();
return 0;
}总结
双阶段 BFS:水时预处理约束人路径,人 BFS 时加时间比较,保证不被水同时或提前淹到。
题意
在 的 01 迷宫中,从给定入口走到出口,只能走上下左右且不经过 1,输出唯一的最短路径坐标序列,无解输出 no way。
思路
BFS 从入口开始扩展,用 记录每个点的前驱坐标,到达出口后递归回溯输出路径。
代码
#include <bits/stdc++.h>
using namespace std;
const int N = 1e4+5;
struct P {
int x, y, step;
};
P l[N][N];
int n, m, sx, sy, ex,ey;
bool vis[N][N];
int res[N][N];
int d[4][2]={ {1, 0}, {0, 1}, {-1, 0}, {0, -1} };
char mp[N][N];
void bfs () {
queue<P> q;
q.push (P{sx, sy, 0});
mp[1][1]=1;
while (q.size()) {
P now=q.front();
q.pop();
res[now.x][now.y]=now.step;
for (int i=0; i<4; i++) {
int nx = now.x+d[i][0], ny = now.y+d[i][1];
if (nx < 1 or nx > n or ny < 1 or ny > m) continue;
if (vis[nx][ny] or mp[nx][ny] == '1') continue;
q.push (P{nx, ny, now.step+1});
vis[nx][ny]=1;
l[nx][ny]=now;
}
}
}
void show (int x, int y) {
if (x==sx and y==sy) {
cout << "(" << x << "," << y << ")";
return;
}
show (l[x][y].x, l[x][y].y);
cout << "->(" << x << "," << y << ")";
}
int main() {
cin >> n >> m;
for (int i=1; i<=n; i++) {
for (int j=1; j<=m; j++) cin >> mp[i][j];
}
cin >> sx >> sy >> ex >> ey;
bfs();
if (vis[ex][ey]==0) {
cout << "no way";
} else show (ex, ey);
return 0;
}总结
BFS 保证最短路径,前驱数组记录路径,递归输出格式符合要求。