-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRepaint.java
More file actions
102 lines (72 loc) · 2.36 KB
/
Copy pathRepaint.java
File metadata and controls
102 lines (72 loc) · 2.36 KB
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
import java.util.*;
public class Repaint
{
static final int INF = 1000000010; // even
static int[] dx = {1, 0, -1, 0, 1, -1, -1, 1};
static int[] dy = {0, 1, 0, -1, 1, 1, -1, -1};
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int H = sc.nextInt();
int W = sc.nextInt();
char[][] a = new char[H][W];
for (int i = 0; i < H; i++) {
a[i] = sc.next().toCharArray();
}
char[][] b = new char[H][W];
for (int i = 0; i < H; i++)
Arrays.fill(b[i], '.');
// Perform ONE repaint
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (a[i][j] == '#') {
for (int d = 0; d < 8; d++) {
int ni = i + dx[d];
int nj = j + dy[d];
if (0 <= ni && ni < H && 0 <= nj && nj < W
&& a[ni][nj] == '.') {
b[ni][nj] = '#';
}
}
}
}
}
int[][] dist = new int[H][W];
for (int i = 0; i < H; i++)
Arrays.fill(dist[i], INF);
Queue<int[]> q = new ArrayDeque<>();
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (b[i][j] == '#') {
dist[i][j] = 0;
q.offer(new int[]{i, j});
}
}
}
while (!q.isEmpty()) {
int[] cur = q.poll();
int x = cur[0];
int y = cur[1];
for (int d = 0; d < 8; d++) {
int nx = x + dx[d];
int ny = y + dy[d];
if (0 <= nx && nx < H && 0 <= ny && ny < W
&& dist[nx][ny] == INF) {
dist[nx][ny] = dist[x][y] + 1;
q.offer(new int[]{nx, ny});
}
}
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < H; i++) {
sb.setLength(0);
for (int j = 0; j < W; j++) {
if (dist[i][j] % 2 == 0)
sb.append('.');
else
sb.append('#');
}
System.out.println(sb);
}
sc.close();
}
}