-
Notifications
You must be signed in to change notification settings - Fork 41
/
solution.js
54 lines (51 loc) · 1.64 KB
/
solution.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
/**
* @param {number[][]} grid
* @param {number} r0
* @param {number} c0
* @param {number} color
* @return {number[][]}
*/
// 矩阵 DFS
var colorBorder = function (grid, r0, c0, color) {
const maxRow = grid.length - 1;
const maxCol = grid[0].length - 1;
const componentColor = grid[r0][c0];
function isBorder (x, y) {
if (x === 0 || y === 0 || x === maxRow || y === maxCol) {
return true;
}
if (grid[x - 1][y] !== componentColor || grid[x][y - 1] !== componentColor || grid[x + 1][y] !== componentColor || grid[x][y + 1] !== componentColor) {
return true;
}
return false;
}
const visited = {};
visited[`${r0},${c0}`] = true;
const border = [];
function dfs (x, y) {
if (isBorder(x, y)) {
border.push(x, y);
}
if (x > 0 && !visited[`${x - 1},${y}`] && grid[x - 1][y] === componentColor) {
visited[`${x - 1},${y}`] = true;
dfs(x - 1, y);
}
if (y > 0 && !visited[`${x},${y - 1}`] && grid[x][y - 1] === componentColor) {
visited[`${x},${y - 1}`] = true;
dfs(x, y - 1);
}
if (x < maxRow && !visited[`${x + 1},${y}`] && grid[x + 1][y] === componentColor) {
visited[`${x + 1},${y}`] = true;
dfs(x + 1, y);
}
if (y < maxCol && !visited[`${x},${y + 1}`] && grid[x][y + 1] === componentColor) {
visited[`${x},${y + 1}`] = true;
dfs(x, y + 1);
}
}
dfs(r0, c0);
for (let i = 0; i < border.length; i += 2) {
grid[border[i]][border[i + 1]] = color;
}
return grid;
};