Skip to content

Commit

Permalink
Create snakes-and-ladders.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
kamyu104 authored Sep 23, 2018
1 parent 9b5b029 commit a027817
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions C++/snakes-and-ladders.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Time: O(n^2)
// Space: O(n^2)

class Solution {
public:
int snakesAndLadders(vector<vector<int>>& board) {
const int n = board.size();
unordered_map<int, int> lookup;
lookup[1] = 0;
queue<int> q({1});
while (!q.empty()) {
const auto s = q.front(); q.pop();
if (s == n * n) {
return lookup[s];
}
for (int i = s + 1; i < min(s + 6, n * n) + 1; ++i) {
int r, c;
tie(r, c) = coordinate(n, i);
int s2 = i;
if (board[r][c] != -1) {
s2 = board[r][c];
}
if (!lookup.count(s2)) {
lookup[s2] = lookup[s] + 1;
q.emplace(s2);
}
}
}
return -1;
}

private:
pair<int, int> coordinate(int n, int s) {
const int a = (s - 1) / n;
const int b = (s - 1) % n;
const int r = n - 1 - a;
const int c = (r % 2 != n % 2) ? b : n - 1 - b;
return {r, c};
}
};

0 comments on commit a027817

Please sign in to comment.