forked from tony9402/baekjoon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
99 lines (90 loc) · 2.35 KB
/
main.cpp
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
// Authored by : tony9402
// Co-authored by : -
// Link : http://boj.kr/8c3a1dd56b2b4bb6ba312c9dbd6eff0d
#include<bits/stdc++.h>
using namespace std;
int N, M, T;
const int dy[] = {-1,1,0,0}; // UDLR
const int dx[] = {0,0,-1,1};
const int UP[] = {0,3,1,2};
const int DOWN[] = {1,3,0,2};
vector<vector<int>> Map;
vector<int> Y;
bool inrange(int y, int x) { return 0 <= y && y < N && 0 <= x && x < M; }
void input(){
cin >> N >> M >> T;
Map = vector<vector<int>>(N, vector<int>(M));
for(int i=0;i<N;i++) {
for(int j=0;j<M;j++){
cin >> Map[i][j];
if(Map[i][j] == -1) {
Map[i][j] = 0;
Y.push_back(i);
}
}
}
}
void spread() {
vector<vector<int>> tmp = Map;
for(int i=0;i<N;i++) {
for(int j=0;j<M;j++) {
if(tmp[i][j] == 0) continue;
int div = tmp[i][j] / 5;
for(int k=0;k<4;k++) {
int qy = i + dy[k];
int qx = j + dx[k];
if((qy == Y[0] || qy == Y[1]) && qx == 0) continue;
if(inrange(qy, qx)) {
Map[i][j] -= div;
Map[qy][qx] += div;
}
}
}
}
}
void air() {
vector<vector<int>> used(N, vector<int>(M));
int y = Y[0];
int x = 0;
int dir = 0;
while(dir < 4) {
Map[Y[0]][0] = 0;
int nexty = y + dy[UP[dir]];
int nextx = x + dx[UP[dir]];
if(0 <= nexty && nexty <= Y[0] && 0 <= nextx && nextx < M && used[nexty][nextx] == 0) {
used[nexty][nextx] = 1;
Map[y][x] = Map[nexty][nextx];
y = nexty;
x = nextx;
}
else dir ++;
}
y = Y[1];
x = 0;
dir = 0;
while(dir < 4) {
Map[Y[1]][0] = 0;
int nexty = y + dy[DOWN[dir]];
int nextx = x + dx[DOWN[dir]];
if(Y[1] <= nexty && nexty < N && 0 <= nextx && nextx < M && used[nexty][nextx] == 0) {
used[nexty][nextx] = 1;
Map[y][x] = Map[nexty][nextx];
y = nexty;
x = nextx;
}
else dir ++;
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
input();
while(T--){
spread();
air();
}
int answer = 0;
for(int i=0;i<N;i++) for(int j=0;j<M;j++) answer += Map[i][j];
cout << answer;
return 0;
}