-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1263.推箱子.cpp
73 lines (67 loc) · 2.48 KB
/
1263.推箱子.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "s.h"
/*
* @lc app=leetcode.cn id=1263 lang=cpp
*
* [1263] 推箱子
*/
// @lc code=start
class Solution {
public:
int minPushBox(vector<vector<char>>& grid) {
int m = grid.size(), n = grid[0].size();
int sx, sy, bx, by; // 玩家、箱子的初始位置
for (int x = 0; x < m; x++) {
for (int y = 0; y < n; y++) {
if (grid[x][y] == 'S') {
sx = x;
sy = y;
} else if (grid[x][y] == 'B') {
bx = x;
by = y;
}
}
}
auto ok = [&](int x, int y) -> bool { // 不越界且不在墙上
return x >= 0 && x < m && y >= 0 && y < n && grid[x][y] != '#';
};
vector<int> d = {0, -1, 0, 1, 0};
vector<vector<int>> dp(m * n, vector<int>(m * n, INT_MAX));
queue<pair<int, int>> q;
dp[sx * n + sy][bx * n + by] = 0; // 初始状态的推动次数为 0
q.push({sx * n + sy, bx * n + by});
while (!q.empty()) {
queue<pair<int, int>> q1;
while (!q.empty()) {
auto [s1, b1] = q.front();
q.pop();
int sx1 = s1 / n, sy1 = s1 % n, bx1 = b1 / n, by1 = b1 % n;
if (grid[bx1][by1] == 'T') { // 箱子已被推到目标处
return dp[s1][b1];
}
for (int i = 0; i < 4; i++) { // 玩家向四个方向移动到另一个状态
int sx2 = sx1 + d[i], sy2 = sy1 + d[i + 1], s2 = sx2*n+sy2;
if (!ok(sx2, sy2)) { // 玩家位置不合法
continue;
}
if (bx1 == sx2 && by1 == sy2) { // 推动箱子
int bx2 = bx1 + d[i], by2 = by1 + d[i + 1], b2 = bx2*n+by2;
if (!ok(bx2, by2) || dp[s2][b2] <= dp[s1][b1] + 1) { // 箱子位置不合法 或 状态已访问
continue;
}
dp[s2][b2] = dp[s1][b1] + 1;
q1.push({s2, b2});
} else {
if (dp[s2][b1] <= dp[s1][b1]) { // 状态已访问
continue;
}
dp[s2][b1] = dp[s1][b1];
q.push({s2, b1});
}
}
}
q.swap(q1);
}
return -1;
}
};
// @lc code=end