时间轴

2025-11-05

init


题目:

蛇梯棋是:只要你“走到”的格子上存在梯子或蛇(board[i][j] != -1),就必须立刻跳到指定位置,不存在选择是否跳或不跳。因此,BFS中模拟投掷时,如果碰到梯子直接跳到梯子对应的位置。

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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <vector>
#include <unordered_map>
#include <utility>
#include <algorithm>
#include <queue>

using std::unordered_map;
using std::vector;
using std::pair;
using std::queue;

class Solution {
public:
vector<vector<int> > get_index_vec(int n)
{
int i = n - 1, j = 0, total = 1;
bool left = false;
vector<vector<int> > index_vec =
vector<vector<int> >(n, vector<int>(n));

while (total <= n * n) {
index_vec[i][j] = total;
total++;
if (j == n - 1 && left == false) {
left = true;
i--;
continue;
}
if (j == 0 && left == true) {
left = false;
i--;
continue;
}
if (left) {
j--;
} else {
j++;
}
}
return index_vec;
}
int snakesAndLadders(vector<vector<int> > &board)
{
int i, j, n = board.size();
vector<vector<int> > index = get_index_vec(n);
int res = 0;
int last = 0;
unordered_map<int, int> ladders_or_snakes;

for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (board[i][j] != -1) {
ladders_or_snakes[index[i][j]] =
board[i][j];
}
}
}
queue<pair<int, int> > que; // {位置, 步数}
unordered_set<int> visited;
int next;
que.push({ 1, 0 });
visited.insert(1);

while (!que.empty()) {
auto [curr, step] = que.front();
que.pop();

if (curr == n * n)
return step;
// 模拟每一步
for (int dice = 1; dice <= 6; dice++) {
next = curr + dice;
if (next > n * n)
break;

if (ladders_or_snakes.count(next)) {
next = ladders_or_snakes[next];
}

if (!visited.count(next)) {
visited.insert(next);
que.push({ next, step + 1 });
}
}
}
return -1;
}
};

// [-1,-1,-1,-1,-1,-1]
// [-1,-1,-1,-1,-1,-1]
// [-1,-1,-1,-1,-1,-1]
// [-1,35,-1,-1,13,-1]
// [-1,-1,-1,-1,-1,-1]
// [-1,15,-1,-1,-1,-1]]