generated from threeal/project-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
solution.c
43 lines (35 loc) · 1.14 KB
/
solution.c
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
#include <stdlib.h>
int snakesAndLadders(int** board, int boardSize, int* boardColSize) {
(void)boardColSize;
const int n = boardSize;
const int end = n * n - 1;
int* positions = malloc(n * n * sizeof(int));
positions[0] = 0;
int positionsBegin = 0;
int positionsEnd = 1;
for (int moves = 1; positionsBegin < positionsEnd; ++moves) {
int newPositionsEnd = positionsEnd;
for (int i = positionsBegin; i < positionsEnd; ++i) {
const int pos = positions[i];
if (pos + 6 >= end) return moves;
for (int next = pos + 6; next > pos; --next) {
const int y = n - 1 - next / n;
const int x = (next / n) % 2 == 0 ? next % n : n - 1 - next % n;
if (board[y][x] > 0) {
const int next = board[y][x] - 1;
board[y][x] = 0;
if (next == end) return moves;
positions[newPositionsEnd] = next;
++newPositionsEnd;
} else if (board[y][x] < 0) {
board[y][x] = 0;
positions[newPositionsEnd] = next;
++newPositionsEnd;
}
}
}
positionsBegin = positionsEnd;
positionsEnd = newPositionsEnd;
}
return -1;
}