-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathByeByeDirt.java
More file actions
197 lines (172 loc) · 5.93 KB
/
Copy pathByeByeDirt.java
File metadata and controls
197 lines (172 loc) · 5.93 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package algorithm.baek.bfs;
import algorithm.TestCase;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.text.ParseException;
import java.util.Arrays;
import java.util.StringTokenizer;
/**
* https://www.acmicpc.net/problem/17144
* 미세먼지 안녕!
* BFS
*/
public class ByeByeDirt implements TestCase {
static int R, C, T;
static Room room;
static int[][] dirs = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
static BufferedReader br;
static BufferedWriter bw;
static StringTokenizer st;
static AirCleaner airCleaner;
@Override
public void test() throws ParseException, IOException {
initMap();
for (int i = 0; i < T; i++) {
room.spread();
airCleaner.cleaning(room);
}
int res = room.getDustAmount();
bw.write(String.valueOf(res));
bw.close();
}
private void initMap() throws IOException {
br = new BufferedReader(new java.io.InputStreamReader(System.in));
bw = new BufferedWriter(new java.io.OutputStreamWriter(System.out));
st = new StringTokenizer(br.readLine());
R = Integer.parseInt(st.nextToken());
C = Integer.parseInt(st.nextToken());
T = Integer.parseInt(st.nextToken());
int[][] tmpMap = new int[R][C];
for (int i = 0; i < R; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < C; j++) {
int value = Integer.parseInt(st.nextToken());
tmpMap[i][j] = value;
if (value == -1) {
airCleaner = new AirCleaner(i, j);
}
}
}
room = new Room(tmpMap);
}
class Room {
private int[][] map;
public Room(int[][] map) {
this.map = map;
}
public void spread() {
int[][] tmpMap = new int[R][C];
for (int i = 0; i < R; i++) {
tmpMap[i] = Arrays.copyOf(map[i], C);
}
for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++) {
int wayCount = 0;
for (int[] dir : dirs) {
int nx = i + dir[0];
int ny = j + dir[1];
if (nx < 0 || nx >= R || ny < 0 || ny >= C) continue;
if (tmpMap[nx][ny] == -1) continue;
wayCount++;
}
int dividedDust = map[i][j] / 5;
int remainDust = dividedDust * wayCount;
for (int[] dir : dirs) {
int nx = i + dir[0];
int ny = j + dir[1];
if (nx < 0 || nx >= R || ny < 0 || ny >= C) continue;
if (tmpMap[nx][ny] == -1) continue;
tmpMap[nx][ny] += dividedDust;
}
tmpMap[i][j] -= remainDust;
}
}
this.map = tmpMap;
}
public int getDustAmount() {
int dustAmount = 0;
for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++) {
dustAmount += map[i][j];
}
}
return dustAmount + 2;
}
public void setMap(int[][] map) {
this.map = map;
}
public int[][] getMap() {
return this.map;
}
public void printMap() {
for (int[] ints : map) {
for (int anInt : ints) {
System.out.print(anInt + " ");
}
System.out.println();
}
}
}
class AirCleaner {
int x;
int y;
public AirCleaner(int y, int x) {
this.x = x;
this.y = y;
}
public int[][] cleaningUp(Room room) {
int[][] tmpMap = room.getMap();
int curX = this.x;
int curY = this.y - 1;
// 1. 위로
for (int nextY = curY; nextY > 0; nextY--) {
tmpMap[nextY][curX] = tmpMap[nextY - 1][curX];
}
tmpMap[curY][curX] = -1;
// 2. 오른쪽으로
for (int nextX = curX; nextX < C - 1; nextX++) {
tmpMap[0][nextX] = tmpMap[0][nextX + 1];
}
// 3. 아래로
for (int nextY = 0; nextY < curY; nextY++) {
tmpMap[nextY][C-1] = tmpMap[nextY + 1][C-1];
}
// 4. 왼쪽으로
for (int nextX = C - 1; nextX > 1; nextX--) {
tmpMap[curY][nextX] = tmpMap[curY][nextX - 1];
}
tmpMap[curY][1] = 0;
tmpMap[curY][curX] = -1;
return tmpMap;
}
public int[][] cleaningDown(Room room) {
int[][] tmpMap = room.getMap();
int x = this.x;
int y = this.y;
// 1. 아래로
for (int i = y; i < R - 1; i++) {
tmpMap[i][x] = tmpMap[i + 1][x];
}
// 2. 오른쪽으로
for (int i = x; i < C - 1; i++) {
tmpMap[R - 1][i] = tmpMap[R - 1][i + 1];
}
// 3. 위로
for (int i = R - 1; i > y; i--) {
tmpMap[i][C - 1] = tmpMap[i - 1][C - 1];
}
// 4. 왼쪽으로
for (int i = C - 1; i > 1; i--) {
tmpMap[y][i] = tmpMap[y][i - 1];
}
tmpMap[y][x] = -1;
tmpMap[y][1] = 0;
return tmpMap;
}
public void cleaning(Room room) {
room.setMap(cleaningUp(room));
room.setMap(cleaningDown(room));
}
}
}