/
boj17144.java
122 lines (109 loc) · 3.45 KB
/
boj17144.java
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
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class Main {
private static int r, c, t;
private static int dy[] = {0, -1, 0, 1};
private static int dx[] = {1, 0, -1, 0};
private static int map[][];
private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
private static StringTokenizer st;
private static ArrayList<Integer> cleaner;
public static void main(String[] args) throws Exception {
init();
while (true) {
t--;
spreadDust();
activateAirCleaner();
if (t == 0) {
System.out.println(countDustVolumn());
break;
}
}
}
private static int countDustVolumn() {
int ret = 0;
for (int y=0; y<r; y++) {
for (int x=0; x<c; x++) {
if (map[y][x] != 0 && map[y][x] != -1) ret += map[y][x];
}
}
return ret;
}
private static void copyMap(int src[][], int dest[][]) {
for (int y=0; y<r; y++) {
for (int x=0; x<c; x++) {
dest[y][x] = src[y][x];
}
}
}
private static void activateAirCleaner() {
int upY = cleaner.get(0);
int downY = cleaner.get(1);
// À§ÂÊ
for (int y=upY-1; y>0; y--) {
map[y][0] = map[y-1][0];
}
for (int x=0; x<c-1; x++) {
map[0][x] = map[0][x+1];
}
for (int y=0; y<upY; y++) {
map[y][c-1] = map[y+1][c-1];
}
for (int x=c-1; x>0; x--) {
map[upY][x] = map[upY][x-1];
}
map[upY][1] = 0;
// ¾Æ·¡ÂÊ
for (int y=downY+1; y<r-1; y++) {
map[y][0] = map[y+1][0];
}
for (int x=0; x<c-1; x++) {
map[r-1][x] = map[r-1][x+1];
}
for (int y=r-1; y>=downY; y--) {
map[y][c-1] = map[y-1][c-1];
}
for (int x=c-1; x>1; x--) {
map[downY][x] = map[downY][x-1];
}
map[downY][1] = 0;
}
private static void spreadDust() {
int tempMap[][] = new int[r][c];
copyMap(map, tempMap);
for (int y=0; y<r; y++) {
for (int x=0; x<c; x++) {
if (map[y][x] != 0 && map[y][x] != -1) {
int count = 0;
int dust = map[y][x] / 5;
for (int i=0; i<4; i++) {
int ny = y+dy[i];
int nx = x+dx[i];
if (ny<0 || nx<0 || ny>=r || nx>=c || map[ny][nx] == -1) continue;
tempMap[ny][nx] += dust;
count++;
}
tempMap[y][x] -= (count * dust);
}
}
}
copyMap(tempMap, map);
}
private static void init() throws Exception {
st = new StringTokenizer(br.readLine());
r = Integer.parseInt(st.nextToken());
c = Integer.parseInt(st.nextToken());
t = Integer.parseInt(st.nextToken());
map = new int[r][c];
cleaner = new ArrayList<>();
for (int y=0; y<r; y++) {
st = new StringTokenizer(br.readLine(), " ");
for (int x=0; x<c; x++) {
map[y][x] = Integer.parseInt(st.nextToken());
if (map[y][x] == -1) cleaner.add(y);
}
}
}
}