-
Notifications
You must be signed in to change notification settings - Fork 6
/
130. Surrounded Regions.js
143 lines (133 loc) · 2.86 KB
/
130. Surrounded Regions.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/**
* @param {character[][]} board
* @return {void} Do not return anything, modify board in-place instead.
*/
function checkTop(board, i, j) {
if (board[i - 1] !== undefined) {
if (board[i - 1][j] === 'O') {
return checkTop(board, i - 1, j);
}
return true;
}
return false;
}
function checkBottom(board, i, j) {
if (board[i + 1] !== undefined) {
if (board[i + 1][j] === 'O') {
return checkBottom(board, i + 1, j);
}
return true;
}
return false;
}
function checkLeft(board, i, j) {
if (board[i][j - 1] === undefined) {
return false;
}
if (board[i][j - 1] === 'O') {
return checkLeft(board, i, j - 1);
}
return true;
}
function checkRight(board, i, j) {
if (board[i][j + 1] === undefined) {
return false;
}
if (board[i][j + 1] === 'O') {
return checkRight(board, i, j + 1);
}
return true;
}
function checkTopY(board, i, j) {
if (board[i - 1][j] === 'X') {
return true;
}
if (board[i - 1][j] === 'O') {
return false;
}
if (board[i - 1][j] === 'Y') {
return checkTopY(board, i - 1, j);
}
}
function checkBottomY(board, i, j) {
if (board[i + 1][j] === 'X') {
return true;
}
if (board[i + 1][j] === 'O') {
return false;
}
if (board[i + 1][j] === 'Y') {
return checkBottomY(board, i + 1, j);
}
}
function checkLeftY(board, i, j) {
if (board[i][j - 1] === 'X') {
return true;
}
if (board[i][j - 1] === 'O') {
return false;
}
if (board[i][j - 1] === 'Y') {
return checkLeftY(board, i, j - 1);
}
}
function checkRightY(board, i, j) {
if (board[i][j + 1] === 'X') {
return true;
}
if (board[i][j + 1] === 'O') {
return false;
}
if (board[i][j + 1] === 'Y') {
return checkRightY(board, i, j + 1);
}
}
var solve = function(board) {
let first = [];
for (let i = 0; i < board.length; i++) {
for (let j = 0; j < board[0].length; j++) {
if (board[i][j] === 'O') {
if (checkTop(board, i, j) && checkBottom(board, i, j) && checkLeft(board, i, j) && checkRight(board, i, j)) {
board[i][j] = 'Y';
first.push({
i,
j
});
}
}
}
}
let temp = true;
while (temp) {
temp = false;
for (let k = 0; k < first.length; k++) {
const {i, j} = first[k];
if (!checkTopY(board, i, j) || !checkBottomY(board, i, j) || !checkLeftY(board, i, j) || !checkRightY(board, i, j)) {
board[i][j] = 'O';
temp = true;
first = first.slice(0, k).concat(first.slice(k + 1));
} else {
board[i][j] = 'X';
}
}
}
console.log(board);
};
solve([
["X","X","X","X"],
["X","O","O","X"],
["X","X","O","X"],
["X","O","X","X"]
]);
solve(["O"]);
solve([
["O","O"],
["O","O"]
]);
solve([
["O","X","X","O","X"],
["X","O","O","X","O"],
["X","O","X","O","X"],
["O","X","O","O","O"],
["X","X","O","X","O"]
]);