-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1034. Coloring A Border
36 lines (34 loc) · 990 Bytes
/
1034. Coloring A Border
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
class Solution {
private int[][] grid;
private int color;
private int m;
private int n;
private boolean[][] vis;
public int[][] colorBorder(int[][] grid, int row, int col, int color) {
this.grid = grid;
this.color = color;
m = grid.length;
n = grid[0].length;
vis = new boolean[m][n];
dfs(row, col, grid[row][col]);
return grid;
}
private void dfs(int i, int j, int c) {
vis[i][j] = true;
int[] dirs = {-1, 0, 1, 0, -1};
for (int k = 0; k < 4; ++k) {
int x = i + dirs[k], y = j + dirs[k + 1];
if (x >= 0 && x < m && y >= 0 && y < n) {
if (!vis[x][y]) {
if (grid[x][y] == c) {
dfs(x, y, c);
} else {
grid[i][j] = color;
}
}
} else {
grid[i][j] = color;
}
}
}
}