-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnakes_And_Ladders.cpp
More file actions
32 lines (32 loc) · 973 Bytes
/
Copy pathSnakes_And_Ladders.cpp
File metadata and controls
32 lines (32 loc) · 973 Bytes
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
class Solution {
public:
int get(int s, int n){
int quot = (s-1)/n;
int rem = (s-1)%n;
int row = n-1-quot;
int col = row%2 != n%2 ? rem: n-1-rem;
return row*n + col;
}
int snakesAndLadders(vector<vector<int>>& board) {
int N = board.size();
unordered_map<int,int> distance;
distance[1] = 0;
queue<int> q;
q.push(1);
while(!q.empty()){
auto curr = q.front();q.pop();
if(curr == N*N)
return distance[curr];
for(int nxt = curr+1; nxt<=min(curr+6, N*N); ++nxt){
int rc = get(nxt,N);
int r = rc/N, c = rc%N;
int nxt_final = board[r][c]==-1 ? nxt: board[r][c];
if(!distance.count(nxt_final)){
distance[nxt_final] = distance[curr] + 1;
q.push(nxt_final);
}
}
}
return -1;
}
};