题意简化
给定一个 的地图,其中 # 代表障碍(无法通过),1 代表草地(花费代价 1),0 代表普通格子。
需要从依次经过 个检查点,行走规则:
- 车子只能沿当前朝向直行或原地转向。
- 直行:不消耗转弯次数,但可能产生格子代价。
- 转向:改变朝向(上下左右),消耗 次转弯机会,但不产生格子代价。
- 总转弯次数不能超过 。
思路
本题是一个典型的带状态约束的多阶段最短路径问题。直接使用普通 BFS 或 Dijkstra 会因状态空间过大和优先队列的 开销而超时。
由于本蒟蒻不会纯 DP 解法,所以我们采用更简单的 启发式搜索 + 01-BFS 来解决。
设 表示:在忽略转弯次数限制的情况下,从 走到第 个检查点最少需要花费多少耐久。
设 表示:已经走过了前 个检查点,现在停在 坐标,车头朝着 方向,用掉了 次转弯机会。在这个状态下,最少花费多少耐久。
1. 如何计算 数组?
如果对每个格子都跑一次 BFS,复杂度为 ,显然不可接受。
我们从每个检查点 出发,反向跑一次 BFS 最短路。由于本题中“进入某格的代价仅取决于该格本身”,正向与反向的边权是对称的,因此反向最短路等价于正向最短路。如果不可到达,则记为无穷大。
代码
void init() {
static pair<int, int> deq[10005]; //双端队列
for (int i = 1; i <= k; ++i) {
for (int x = 0; x < n; ++x) {
for (int y = 0; y < m; ++y) {
h[i][x][y] = inf; //初始化为无法到达
}
}
int head = 5000, tail = 5000;
h[i][p[i].x][p[i].y] = 0;
deq[tail++] = {p[i].x, p[i].y};
while (head < tail) {
auto [x, y] = deq[head++];
for (int d = 0; d < 4; ++d) {
int nx = x + dx[d];
int ny = y + dy[d];
if (nx < 0 || nx >= n || ny < 0 || ny >= m) continue;
if (g[nx][ny] == '#') continue;
int w = (g[x][y] == '1' ? 1 : 0);
if (h[i][x][y] + w < h[i][nx][ny]) {
h[i][nx][ny] = h[i][x][y] + w;
if (w == 0) {
deq[--head] = {nx, ny};
} else {
deq[tail++] = {nx, ny};
}
}
}
}
}
}2. 如何进行搜索?
同样使用 0-1 BFS 来实现。用 dp 数组记录已求得的最优解。
我们可以用数组来模拟双端队列,如果扩展出来的状态是在公路上,则插入队头;如果是草地(需要花费代价),则插入队尾。这样能保证每次取出的队首一定是花费最小的,避免了优先队列的巨大常数开销。
代码
void solve() {
init();
if (h[1][p[0].x][p[0].y] == inf) {
cout << -1 << "\n"; //特判:如果第一个检查点都无法到达,那么直接输出-1
return ;
}
memset(dp, 0x3f, sizeof(dp));
static state q[15000005]; //数组模拟双端队列
int head = 7500000, tail = 7500000; //从中间开始
for (int d = 0; d < 4; ++d) { //初始状态加入队列
dp[0][p[0].x][p[0].y][d][0] = 0;
q[tail++] = {0, 0, p[0].x, p[0].y, d, 0};
}
while (head < tail) { //搜索过程
state u = q[head++]; //取出头结点
if (u.cost > dp[u.s][u.x][u.y][u.dir][u.turns]) {
continue; //已经不是最优,跳过
}
if (u.s == k && u.x == p[k].x && u.y == p[k].y && u.turns <= t) {
cout << 1000000000 - u.cost << "\n"; //到达,输出
return ;
}
//1、尝试直行
int nx = u.x + dx[u.dir];
int ny = u.y + dy[u.dir];
if (nx >= 0 && nx < n && ny >= 0 && ny < m && g[nx][ny] != '#') {
int ns = u.s;
if (ns < k && nx == p[ns + 1].x && ny == p[ns + 1].y) {
ns++; //到达下一个检查点
}
bool can = true;
if (ns < k && h[ns + 1][nx][ny] == inf) {
can = false; //无法到达下一个检查点
}
if (can) {
int move_cost = (g[nx][ny] == '1' ? 1 : 0);
int ncost = u.cost + move_cost;
if (ncost < dp[ns][nx][ny][u.dir][u.turns]) { //比原来更优
dp[ns][nx][ny][u.dir][u.turns] = ncost; //更新答案
if (move_cost == 0) { //插入队列
q[--head] = {ncost, u.turns, nx, ny, u.dir, ns};
} else {
q[tail++] = {ncost, u.turns, nx, ny, u.dir, ns};
}
}
}
}
//2、尝试转弯
for (int ndir = 0; ndir < 4; ++ndir) {
if (ndir == u.dir) continue; //跳过重复
int nturns = u.turns + 1;
if (nturns > t) continue;
bool can = true;
if (u.s < k && h[u.s + 1][u.x][u.y] == inf) {
can = false; //无法到达
}
if (can) {
if (u.cost < dp[u.s][u.x][u.y][ndir][nturns]) { //插入队列
dp[u.s][u.x][u.y][ndir][nturns] = u.cost;
q[--head] = {u.cost, nturns, u.x, u.y, ndir, u.s};
}
}
}
}
cout << -1 << "\n"; //无解
}3. 如何进行剪枝?
朴素的搜索是会超时的。我们可以考虑加入剪枝来优化。
- 如果发现当前节点比 dp 数组中记录的要大,则可以直接放弃。
- 在预处理中,我们已经对不可到达的节点进行了标记。如果我们发现当前节点无法到达下一个检查点,则直接放弃,因为无论如何也到达不了终点。
4. 复杂度
预处理:从每个检查点各跑一次 01-BFS 求 数组,单次 ,共 次,故为 。
主搜索:状态为 ,故状态总数为
在 01-BFS 中每个状态至多入队、出队各一次;每次出队最多尝试 1 次直行和 3 次转向,转移代价为 ,因此主搜索的复杂度为 。
总复杂度:时间 ,空间 (dp 与 数组)。
实测可以通过本题。
5. 完整代码
AC Code
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
int n, m, k, t;
vector<string> g;
int h[15][55][55];
int dp[15][55][55][4][105];
struct point {
int x, y;
}p[15];
struct state {
int cost, turns, x, y, dir, s;
};
void init() {
static pair<int, int> deq[10005];
for (int i = 1; i <= k; ++i) {
for (int x = 0; x < n; ++x) {
for (int y = 0; y < m; ++y) {
h[i][x][y] = inf;
}
}
int head = 5000, tail = 5000;
h[i][p[i].x][p[i].y] = 0;
deq[tail++] = {p[i].x, p[i].y};
while (head < tail) {
auto [x, y] = deq[head++];
for (int d = 0; d < 4; ++d) {
int nx = x + dx[d];
int ny = y + dy[d];
if (nx < 0 || nx >= n || ny < 0 || ny >= m) continue;
if (g[nx][ny] == '#') continue;
int w = (g[x][y] == '1' ? 1 : 0);
if (h[i][x][y] + w < h[i][nx][ny]) {
h[i][nx][ny] = h[i][x][y] + w;
if (w == 0) deq[--head] = {nx, ny};
else deq[tail++] = {nx, ny};
}
}
}
}
}
void solve() {
init();
if (h[1][p[0].x][p[0].y] == inf) {
cout << -1 << "\n";
return ;
}
memset(dp, 0x3f, sizeof(dp));
static state q[15000005];
int head = 7500000, tail = 7500000;
for (int d = 0; d < 4; ++d) {
dp[0][p[0].x][p[0].y][d][0] = 0;
q[tail++] = {0, 0, p[0].x, p[0].y, d, 0};
}
while (head < tail) {
state u = q[head++];
if (u.cost > dp[u.s][u.x][u.y][u.dir][u.turns]) continue;
if (u.s == k && u.x == p[k].x && u.y == p[k].y && u.turns <= t) {
cout << 1000000000 - u.cost << "\n";
return ;
}
int nx = u.x + dx[u.dir];
int ny = u.y + dy[u.dir];
if (nx >= 0 && nx < n && ny >= 0 && ny < m && g[nx][ny] != '#') {
int ns = u.s;
if (ns < k && nx == p[ns + 1].x && ny == p[ns + 1].y) ns++;
bool can = true;
if (ns < k && h[ns + 1][nx][ny] == inf) can = false;
if (can) {
int move_cost = (g[nx][ny] == '1' ? 1 : 0);
int ncost = u.cost + move_cost;
if (ncost < dp[ns][nx][ny][u.dir][u.turns]) {
dp[ns][nx][ny][u.dir][u.turns] = ncost;
if (move_cost == 0) q[--head] = {ncost, u.turns, nx, ny, u.dir, ns};
else q[tail++] = {ncost, u.turns, nx, ny, u.dir, ns};
}
}
}
for (int ndir = 0; ndir < 4; ++ndir) {
if (ndir == u.dir) continue;
int nturns = u.turns + 1;
if (nturns > t) continue;
bool can = true;
if (u.s < k && h[u.s + 1][u.x][u.y] == inf) can = false;
if (can) {
if (u.cost < dp[u.s][u.x][u.y][ndir][nturns]) {
dp[u.s][u.x][u.y][ndir][nturns] = u.cost;
q[--head] = {u.cost, nturns, u.x, u.y, ndir, u.s};
}
}
}
}
cout << -1 << "\n";
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n >> m >> k >> t;
g.resize(n);
for (int i = 0; i < n; ++i) cin >> g[i];
for (int i = 0; i <= k; ++i) {
cin >> p[i].x >> p[i].y;
p[i].x--;
p[i].y--;
}
solve();
return 0;
}