-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
57 lines (49 loc) · 1.32 KB
/
index.js
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
const DIRS = [
[-1, 0],
[0, -1],
[0, 1],
[1, 0],
];
export function solve(board) {
let visited = {};
let conquered = {};
let valid = true;
function conquer(board, i, j) {
conquered[`${i}-${j}`] = true;
board[i][j] = 'X';
for (let d of DIRS) {
const x = i + d[0];
const y = j + d[1];
if (board[x] && board[x][y] === 'O') {
if (!conquered[`${x}-${y}`]) {
conquer(board, x, y);
}
}
}
}
function sail(board, i, j) {
visited[`${i}-${j}`] = true;
for (let dd of DIRS) {
let _x = i + dd[0];
let _y = j + dd[1];
if (!board[_x] || !board[_x][_y]) valid = false;
}
for (let d of DIRS) {
const x = i + d[0];
const y = j + d[1];
if (board[x] && board[x][y] === 'O' && !visited[`${x}-${y}`]) {
sail(board, x, y);
}
}
}
for (let i = 0; i < board.length; i++) {
for (let j = 0; j < board[i].length; j++) {
if (board[i][j] === 'O' && !visited[`${i}-${j}`]) {
sail(board, i, j);
if (valid) conquer(board, i, j);
valid = true;
}
}
}
return board;
}