-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain17144_미세먼지안녕.java
128 lines (102 loc) · 2.78 KB
/
Main17144_미세먼지안녕.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
123
124
125
126
127
128
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;
//2019-06-20
//BOJ :: 17144 미세먼지안녕!
public class Main17144_미세먼지안녕 {
private static int R, C, T;
private static int[][] map, nmap;
static int[] dy = { -1, 1, 0, 0 };
static int[] dx = { 0, 0, -1, 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());
C = Integer.parseInt(st.nextToken());
T = Integer.parseInt(st.nextToken());
map = new int[R][C];
for (int i = 0; i < R; i++) {
st = new StringTokenizer(br.readLine(), " ");
for (int j = 0; j < C; j++) {
map[i][j] = Integer.parseInt(st.nextToken());
}
} //// input
for (int t = 0; t < T; t++) {
spreadDust();
}
int ans=0;
for(int i=0;i<R;i++) {
for(int j=0;j<C;j++) {
if(map[i][j]>0) {
ans+=map[i][j];
}
}
}
System.out.println(ans);
}
public static void spreadDust() {
ArrayList<pair> airCleanr = new ArrayList<>();
nmap = new int[R][C];
for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++) {
if (map[i][j] > 0) {// 먼지인 경우
int cnt = 0;
for (int d = 0; d < 4; d++) {
int ny = i + dy[d];
int nx = j + dx[d];
if (ny < 0 || ny > R - 1 || nx < 0 || nx > C - 1 || map[ny][nx] == -1)
continue;
nmap[ny][nx] += map[i][j] / 5;
cnt++;
}
nmap[i][j] += map[i][j] - (map[i][j] / 5) * cnt;
} else if (map[i][j] == -1) {// 공기청정기 위치.
nmap[i][j] = -1;
airCleanr.add(new pair(i, j));
}
}
}
for (int i = 0; i < R; i++) {
map[i] = Arrays.copyOf(nmap[i], C);
}
cleanAir(airCleanr.get(0), airCleanr.get(1));
}
public static void cleanAir(pair up, pair down) {
// up : 반시계방향
// down : 시계방향
for (int i = 0; i < C - 1; i++) {
if (nmap[up.y][i] == -1 || nmap[down.y][i] == -1) {
map[up.y][i + 1] = 0;
map[down.y][i + 1] = 0;
} else {
map[up.y][i + 1] = nmap[up.y][i];
map[down.y][i + 1] = nmap[down.y][i];
}
map[0][i] = nmap[0][i + 1];
map[R - 1][i] = nmap[R - 1][i + 1];
}
for (int j = 0; j < R - 1; j++) {
if (j < up.y) {
map[j + 1][0] = nmap[j][0]; // 위로
map[j][C - 1] = nmap[j + 1][C - 1]; // 아래로
} else if (j >= down.y) {
map[j + 1][C - 1] = nmap[j][C - 1]; // 아래
map[j][0] = nmap[j + 1][0]; // 위로
}
}
map[up.y][up.x] = -1;
map[down.y][down.x] = -1;
//System.out.println();
}
static class pair {
int y;
int x;
public pair(int y, int x) {
this.y = y;
this.x = x;
}
}
}