-
Notifications
You must be signed in to change notification settings - Fork 0
/
0130. Surrounded Regions.js
78 lines (69 loc) · 2.06 KB
/
0130. 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
// Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'.
// A region is captured by flipping all 'O's into 'X's in that surrounded region.
//
// Example:
//
// X X X X
// X O O X
// X X O X
// X O X X
//
// After running your function, the board should be:
//
// X X X X
// X X X X
// X X X X
// X O X X
//
// Explanation:
//
// Surrounded regions shouldn’t be on the border, which means that any 'O' on the border of the board are not flipped to 'X'. Any 'O' that is not on the border and it is not connected to an 'O' on the border will be flipped to 'X'. Two cells are connected if they are adjacent cells connected horizontally or vertically.
/**
* @param {character[][]} board
* @return {void} Do not return anything, modify board in-place instead.
*/
// Idea
// 1) Check four borders. If it is O, change it and all its neighbor to temporary #
// 2) Change all O to X
// 3) Change all # to O
//
// Example
// X X X X X X X X X X X X
// X X O X -> X X O X -> X X X X
// X O X X X # X X X O X X
// X O X X X # X X X O X X
const solve = (board) => {
if (board == null || board.length === 0) return;
const h = board.length;
const w = board[0].length;
const dirs = [[-1, 0], [0, 1], [1, 0], [0, -1]];
const go = (x ,y) => {
if (board[x][y] !== 'O') return;
board[x][y] = '#';
for (const [dx, dy] of dirs) {
const i = x + dx;
const j = y + dy;
if (i >= 0 && i < h && j >= 0 && j < w) {
go(i, j);
}
}
};
// change every square connected to left and right borders from O to temporary #
for (let i = 0; i < h; i++) {
go(i, 0);
go(i, w - 1);
}
// change every square connected to top and bottom borders from O to temporary #
for (let i = 1; i < w - 1; i++) {
go(0, i);
go(h - 1, i);
}
for (let i = 0; i < h; i++) {
for (let j = 0; j < w; j++) {
// change the rest of O to X
if (board[i][j] === 'O') board[i][j] = 'X';
// change temporary # back to O
if (board[i][j] === '#') board[i][j] = 'O';
}
}
};