-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnakesandLadders.cpp
More file actions
40 lines (38 loc) · 1.08 KB
/
snakesandLadders.cpp
File metadata and controls
40 lines (38 loc) · 1.08 KB
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
// Source: https://leetcode.com/problems/snakes-and-ladders/
// Author: Miao Zhang
// Date: 2021-03-23
class Solution {
public:
int snakesAndLadders(vector<vector<int>>& board) {
int n = board.size();
int steps = 0;
vector<int> seen(n * n + 1, 0);
queue<int> q;
q.push(1);
while (!q.empty()) {
int size = q.size();
steps++;
while (size--) {
int s = q.front();
q.pop();
for (int s1 = s + 1; s1 <= min(s + 6, n * n); s1++) {
int r, c;
get(s1, n, &r, &c);
int x = board[r][c] == -1 ? s1 : board[r][c];
if (seen[x]) continue;
if (x == n * n) return steps;
q.push(x);
seen[x] = 1;
}
}
}
return -1;
}
private:
void get(int s, int n, int* r, int* c) {
int x = (s - 1) / n;
int y = (s - 1) % n;
*r = n - 1 - x;
*c = (x % 2) ? n - 1 - y : y;
}
};