-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path17144.java
154 lines (118 loc) · 3.57 KB
/
17144.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
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
import java.io.*;
import java.util.StringTokenizer;
public class Main {
static int r;
static int c;
static int t;
static int cleaner_x = -1;
static int cleaner_y = -1;
static int[] dx = {1, 0, -1, 0};
static int[] dy = {0, 1, 0, -1};
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st = new StringTokenizer(br.readLine());
r = Integer.parseInt(st.nextToken());
c = Integer.parseInt(st.nextToken());
t = Integer.parseInt(st.nextToken());
int[][] map = new int[r][c];
for(int i=0; i<r; i++) {
st = new StringTokenizer(br.readLine());
for(int j=0; j<c; j++) {
int input = Integer.parseInt(st.nextToken());
map[i][j] = input;
if(input == -1 && cleaner_x == -1) {
cleaner_x = i;
cleaner_y = j;
}
}
}
// t초만큼 진행
for(int i=0; i<t; i++) {
map = spread(map);
rolling(map);
}
// 합 구하기
int answer = 0;
for(int i=0; i<r; i++) {
for(int j=0; j<c; j++) {
answer += map[i][j];
}
}
System.out.println(answer+2); // -1 2개 처리
}
public static int[][] spread(int[][] before) {
int[][] after = new int[r][c];
for(int i=0; i<r; i++) {
for(int j=0; j<c; j++) {
// 확산할 필요가 없는 칸이면 skip
if(before[i][j] == 0) {
continue;
}
if(before[i][j] == -1) {
after[i][j] = -1;
continue;
}
int val = before[i][j] / 5;
int cnt = 0;
// 상하좌우에 확산될 값 더해줌
for(int a=0; a<4; a++) {
int x = i+dx[a];
int y = j+dy[a];
if(x<0 || x>=r || y<0 || y>=c || before[x][y] == -1)
continue;
after[x][y] += val;
cnt++;
}
// 현재 칸에 남아있는 미세먼지 더해줌
after[i][j] += before[i][j] - (val*cnt);
}
}
return after;
}
public static void rolling(int[][] map) {
int x,y;
// 위쪽영역 반시계방향
x = cleaner_x;
y = cleaner_y;
while(x-1 >= 0) {
map[x][y] = map[x-1][y];
x--;
}
map[cleaner_x][cleaner_y] = -1;
while(y+1 < c) {
map[x][y] = map[x][y+1];
y++;
}
while(x+1 <= cleaner_x) {
map[x][y] = map[x+1][y];
x++;
}
while(y-1 >= 0) {
map[x][y] = map[x][y-1];
y--;
}
map[cleaner_x][cleaner_y+1] = 0;
// 아래쪽영역 시계방향
x = cleaner_x+1;
y = cleaner_y;
while(x+1 < r) {
map[x][y] = map[x+1][y];
x++;
}
map[cleaner_x+1][cleaner_y] = -1;
while(y+1 < c) {
map[x][y] = map[x][y+1];
y++;
}
while(x-1 >= cleaner_x+1) {
map[x][y] = map[x-1][y];
x--;
}
while(y-1 >= 0) {
map[x][y] = map[x][y-1];
y--;
}
map[cleaner_x+1][cleaner_y+1] = 0;
}
}