Skip to content

Commit dccacca

Browse files
committed
이진희-1034
1 parent 4bf4491 commit dccacca

1 file changed

Lines changed: 69 additions & 0 deletions

File tree

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
3+
1. 아이디어 : 2차원 격자배열에서 raw,col좌표를 기준으로 같은색으로 연결된 칸을 전부 color로 바꾼다. 이때 경계값만 색칠해야 한다.
4+
5+
2. 시간복잡도 : O(N*M)
6+
7+
3. 자료구조/알고리즘 : bfs
8+
9+
*/
10+
11+
class Solution {
12+
private int[] dy = {0,1,0,-1};
13+
private int[] dx = {-1,0,1,0};
14+
private boolean[][] visited;
15+
private int n,m;
16+
17+
public int[][] colorBorder(int[][] grid, int row, int col, int color) {
18+
19+
n = grid.length;
20+
m = grid[0].length;
21+
visited = new boolean[n][m];
22+
23+
return bfs(row, col, color, grid);
24+
}
25+
26+
private int[][] bfs (int startY, int startX, int c, int[][] grid) {
27+
Deque<int[]> dq = new ArrayDeque<>();
28+
List<int[]> border = new ArrayList<>();
29+
30+
dq.add(new int[]{startY, startX});
31+
visited[startY][startX] = true;
32+
int ori = grid[startY][startX];
33+
34+
while(!dq.isEmpty()) {
35+
int[] curr = dq.poll();
36+
int y = curr[0];
37+
int x = curr[1];
38+
39+
// 경계면만 바꾸기
40+
boolean check = false;
41+
42+
for(int dir=0; dir<4; dir++) {
43+
int ny = y + dy[dir];
44+
int nx = x + dx[dir];
45+
46+
if(ny<0 || ny>=n || nx<0 || nx>=m) {
47+
check = true;
48+
continue;
49+
}
50+
if(visited[ny][nx]) continue;
51+
if(grid[ny][nx] != ori) {
52+
check = true;
53+
continue;
54+
}
55+
56+
dq.add(new int[]{ny, nx});
57+
visited[ny][nx] = true;
58+
}
59+
60+
if(check) border.add(new int[]{y,x});
61+
}
62+
63+
for(int[] b : border) {
64+
grid[b[0]][b[1]] = c;
65+
}
66+
67+
return grid;
68+
}
69+
}

0 commit comments

Comments
 (0)