-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJava_23289.java
More file actions
179 lines (164 loc) · 5.86 KB
/
Java_23289.java
File metadata and controls
179 lines (164 loc) · 5.86 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class Java_23289 {
static int R, C, K;
static int[][] map;
static final int WALL = -1;
static int[] dx = {0, 0, -1, 1};
static int[] dy = {1, -1, 0, 0};
static int[] spreadD = {-2, 0, 2};
static int[][] wallSpreadDx = {{-1, -2}, {0}, {1, 2}};
static int[][] wallSpreadDy = {{0, 1}, {1}, {0, 1}};
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
R = Integer.parseInt(st.nextToken()) * 2 - 1;
C = Integer.parseInt(st.nextToken()) * 2 - 1;
K = Integer.parseInt(st.nextToken());
map = new int[R][C];
ArrayList<Heater> heaterList = new ArrayList<>();
ArrayList<int[]> checkList = new ArrayList<>();
for (int i = 0; i < R; i += 2) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < C; j += 2) {
int type = Integer.parseInt(st.nextToken());
if (type == 0) continue;
if (type < 5) heaterList.add(new Heater(i, j, type-1));
else checkList.add(new int[]{i, j});
}
}
int wallCnt = Integer.parseInt(br.readLine());
for (int i = 0; i < wallCnt; i++) {
st = new StringTokenizer(br.readLine());
int x = (Integer.parseInt(st.nextToken()) - 1) * 2;
int y = (Integer.parseInt(st.nextToken()) - 1) * 2;
int type = Integer.parseInt(st.nextToken());
if (type == 0) x -= 1;
else y += 1;
map[x][y] = WALL;
}
int chocolate = 0;
while (!isHigherThanK(checkList)) {
if (chocolate > 100) {
chocolate = 101;
break;
}
for (Heater h : heaterList) {
turnOnHeater(h);
}
adjust();
reduceEdge();
chocolate++;
}
System.out.println(chocolate);
}
static void turnOnHeater(Heater heater) {
Queue<Room> que = new LinkedList<>();
boolean[][] isVisited = new boolean[R][C];
int dir = heater.dir;
int k = 5;
int startX = heater.x + dx[dir] * 2;
int startY = heater.y + dy[dir] * 2;
que.add(new Room(startX, startY));
isVisited[startX][startY] = true;
map[startX][startY] += k;
while (!que.isEmpty()) {
int size = que.size();
if (k == 0) break;
k--;
for (int i = 0; i < size; i++) {
Room r = que.poll();
spreadRoom:
for (int d = 0; d < 3; d++) {
int tx = r.x, ty = r.y;
if (dir < 2) {
tx += spreadD[d];
ty += (dir == 1 ? -2 : 2);
} else {
tx += (dir == 2 ? -2 : 2);
ty += spreadD[d];
}
if (isValidate(tx, ty) && !isVisited[tx][ty]) {
for (int w = 0; w < wallSpreadDx[d].length; w++) {
int wx, wy;
if (dir < 2) {
wx = r.x + wallSpreadDx[d][w];
wy = r.y + wallSpreadDy[d][w] * (dir == 1 ? -1 : 1);
} else {
wx = r.x + wallSpreadDy[d][w] * (dir == 2 ? -1 : 1);
wy = r.y + wallSpreadDx[d][w];
}
if (map[wx][wy] == WALL) continue spreadRoom;
}
que.add(new Room(tx, ty));
map[tx][ty] += k;
isVisited[tx][ty] = true;
}
}
}
}
}
static void adjust() {
int[][] originMap = new int[R][C];
for (int i = 0; i < R; i++) {
System.arraycopy(map[i], 0, originMap[i], 0, C);
}
for (int i = 0; i < R; i += 2) {
for (int j = 0; j < C; j += 2) {
for (int d = 0; d < 4; d++) {
int tx = i + dx[d];
int ty = j + dy[d];
if (isValidate(tx, ty) && map[tx][ty] != WALL) {
tx += dx[d];
ty += dy[d];
if (originMap[i][j] > originMap[tx][ty]) {
int dif = (originMap[i][j] - originMap[tx][ty]) / 4;
map[i][j] -= dif;
map[tx][ty] += dif;
}
}
}
}
}
}
static void reduceEdge() {
for (int i = 0; i < R; i += R - 1) {
for (int j = 0; j < C; j++) {
if (map[i][j] > 0) map[i][j] -= 1;
}
}
for (int j = 0; j < C; j += C - 1) {
for (int i = 1; i < R-1; i++) {
if (map[i][j] > 0) map[i][j] -= 1;
}
}
}
static boolean isHigherThanK(ArrayList<int[]> list) {
for (int[] room : list) {
if (map[room[0]][room[1]] < K) return false;
}
return true;
}
static boolean isValidate(int x, int y) {
return x >= 0 && x < R && y >= 0 && y < C;
}
static class Heater {
int x, y, dir;
public Heater(int x, int y, int dir) {
this.x = x;
this.y = y;
this.dir = dir;
}
}
static class Room {
int x, y;
public Room(int x, int y) {
this.x = x;
this.y = y;
}
}
}