diff options
-rw-r--r-- | snakes_ladders.c | 82 | ||||
-rw-r--r-- | snakes_ladders.py | 65 |
2 files changed, 147 insertions, 0 deletions
diff --git a/snakes_ladders.c b/snakes_ladders.c new file mode 100644 index 0000000..b7128db --- /dev/null +++ b/snakes_ladders.c @@ -0,0 +1,82 @@ +// 909. Snakes and Ladders +#include "leetcode.h" + +/* + * you are given an 'n x n' integer matrix 'board' where the cells are all + * labeled from 1 to 'n^2' in 'boustropheden style' starting from bottom left of + * the board, and alternating direction each row. a board square on row 'r' and + * column 'c' has a snake of a ladder if 'board[r][c] != -1'. the destination of + * that snake or ladder is 'board[r][c]'. squares 1 and 'n^2' are not the + * starting points of any snake or ladder. note that you only take a snake or a + * ladder at most once per dice roll. if the destination to a snake or ladder is + * the start of another snake or ladder, you do not follow the subsequent snake + * or ladder. + */ + +struct queue { + int num; + int moves; +}; + +void coordinates(int num, int n, int *row, int *col) { + int r = (num - 1) / n, c = (num - 1) % n; + if (r % 2 == 1) + c = n - 1 - c; + *row = n - 1 - r; + *col = c; +} + +int snakesAndLadders(int **board, int boardSize, int *boardColSize) { + int n = boardSize * boardSize, front = 0, rear = 0; + bool *vis = (bool *)calloc(n + 1, sizeof(bool)); + struct queue *q = (struct queue *)malloc(n * sizeof(struct queue)); + q[rear].num = 1; + q[rear++].moves = 0; + vis[1] = true; + while (front < rear) { + struct queue curr = q[front++]; + if (curr.num == n) { + free(vis); + free(q); + return curr.moves; + } + for (int i = 1; i <= 6; i++) { + int next = curr.num + 1; + if (next > n) + continue; + int row, col; + coordinates(next, boardSize, &row, &col); + if (board[row][col] != -1) + next = board[row][col]; + if (next == n) { + free(vis); + free(q); + return curr.moves + 1; + } + if (!vis[next]) { + vis[next] = true; + q[rear].num = next; + q[rear++].moves = curr.moves + 1; + } + } + } + free(vis); + free(q); + return -1; +} + +int main() { + int b1i[6][6] = {{-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}}; + int b2i[2][2] = {{-1, -1}, {-1, 3}}; + struct two_d_arr b1, b2; + two_d_arr_init(&b1, ARRAY_SIZE(b1i), ARRAY_SIZE(b1i[0]), b1i); + two_d_arr_init(&b2, ARRAY_SIZE(b2i), ARRAY_SIZE(b2i[0]), b2i); + printf("%d\n", + snakesAndLadders(b1.arr, b1.row_size, b1.col_size)); // expect: 4 + printf("%d\n", + snakesAndLadders(b2.arr, b2.row_size, b2.col_size)); // expect: 1 + two_d_arr_free(&b1); + two_d_arr_free(&b2); +} diff --git a/snakes_ladders.py b/snakes_ladders.py new file mode 100644 index 0000000..5541d87 --- /dev/null +++ b/snakes_ladders.py @@ -0,0 +1,65 @@ +# 909. Snakes and Ladders + +""" +you are given an 'n x n' integer matrix 'board' where the cells are all +labeled from 1 to 'n^2' in 'boustropheden style' starting from bottom left of +the board, and alternating direction each row. a board square on row 'r' and +column 'c' has a snake of a ladder if 'board[r][c] != -1'. the destination of +that snake or ladder is 'board[r][c]'. squares 1 and 'n^2' are not the +starting points of any snake or ladder. note that you only take a snake or a +ladder at most once per dice roll. if the destination to a snake or ladder is +the start of another snake or ladder, you do not follow the subsequent snake +or ladder. +""" + + +class Solution(object): + def snakesAndLadders(self, board): + """ + :type board: List[List[int]] + :rtype: int + """ + n = len(board) + + def coordinates(num): + row, col = (num - 1) // n, (num - 1) % n + col = n - 1 if row % 2 == 1 else col + row = n - 1 - row + return row, col + + vis, queue = set(), [(1, 0)] + vis.add(1) + while queue: + curr, moves = queue.pop(0) + if curr == n**2: + return moves + for i in range(1, 7): + next = curr + i + if next > n**2: + continue + row, col = coordinates(next) + if board[row][col] != -1: + next = board[row][col] + if next == n**2: + return moves + 1 + if next not in vis: + vis.add(next) + queue.append((next, moves + 1)) + return -1 + + +if __name__ == "__main__": + obj = Solution() + print( + obj.snakesAndLadders( + board=[ + [-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], + ] + ) + ) + print(obj.snakesAndLadders(board=[[-1, -1], [-1, 3]])) |