-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
139 lines (122 loc) · 4.12 KB
/
Main.java
File metadata and controls
139 lines (122 loc) · 4.12 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
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class Main {
static int R,C,T,airCleaner;
static int[][] map;
static int[] dx = {-1,1,0,0};
static int[] dy = {0,0,-1,1};
public static void main(String[] args) throws IOException {
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];
boolean cleanerFound = false;
for(int i=0; i<R; i++) {
st = new StringTokenizer(br.readLine());
for(int j=0; j<C; j++) {
int tmp = Integer.parseInt(st.nextToken());
if(tmp == -1 && !cleanerFound) {
airCleaner = i;
cleanerFound = true;
}
map[i][j] = tmp;
}
}
for(int i=0; i<T; i++) {
spreadDust();
operateCleaner();
// printTest(i);
}
printRemainDust();
br.close();
}
public static void printTest(int t) {
System.out.println("TIME: " + t);
for(int i=0; i<R; i++) {
System.out.println(Arrays.toString(map[i]));
}
System.out.println();
}
public static void printRemainDust() {
int sum = 0;
for(int i=0; i<R; i++) {
for(int j=0; j<C; j++) {
if(map[i][j] != -1) {
sum += map[i][j];
}
}
}
System.out.println(sum);
}
public static void spreadDust() {
Queue<Square> dusts = new LinkedList<>();
for(int i=0; i<R; i++) {
for(int j=0; j<C; j++) {
if(map[i][j] != -1 && map[i][j] != 0) {
dusts.add(new Square(i, j, map[i][j]));
}
}
}
while(!dusts.isEmpty()) {
Square dust = dusts.poll();
if(dust.val < 5) continue;
int spreadingDust = dust.val / 5;
int count = 0;
for(int i=0; i<4; i++) {
int nx = dust.x + dx[i];
int ny = dust.y + dy[i];
if(nx>=0 && nx<R && ny>=0 && ny<C) {
if(map[nx][ny] == -1) continue;
map[nx][ny] += spreadingDust;
count++;
}
}
map[dust.x][dust.y] -= spreadingDust*count;
}
}
public static void operateCleaner() {
int top = airCleaner;
for(int i = top-1; i>0; i--) { // 왼쪽 변, 위에서 아래로
map[i][0] = map[i-1][0];
}
for(int i=0; i<C-1; i++) { // 상단 변, 오른쪽에서 왼쪽으로
map[0][i] = map[0][i+1];
}
for(int i=0; i<top; i++) { // 오른쪽 변, 아래에서 위로
map[i][C-1] = map[i+1][C-1];
}
for(int i=C-1; i>1; i--) { // 하단 변, 왼쪽에서 오른쪽으로
map[top][i] = map[top][i-1];
}
map[top][1] = 0; // 공기청정기에서 나온 바람은 먼지 없음.
int bottom = airCleaner+1;
for(int i=bottom+1; i<R-1; i++) { // 왼쪽 변, 아래에서 위로
map[i][0] = map[i+1][0];
}
for(int i=0; i<C-1; i++) { // 하단 변, 오른쪽에서 왼쪽으로
map[R-1][i] = map[R-1][i+1];
}
for(int i=R-1; i>bottom; i--) { // 오른쪽 변, 위에서 아래로
map[i][C-1] = map[i-1][C-1];
}
for(int i=C-1; i>1; i--) { // 상단 변, 왼쪽에서 오른쪽으로
map[bottom][i] = map[bottom][i-1];
}
map[bottom][1] = 0; // 공기청정기에서 나온 바람은 먼지 없음.
}
}
class Square {
int x, y, val;
public Square(int x, int y, int val) {
this.x = x;
this.y = y;
this.val = val;
}
}