#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
const int MAX = 1001;
int r, c, t;
int air[MAX][MAX];
int copyair[MAX][MAX];
int copys[MAX][MAX];
vector<pair<int, int>> cleanmachine;
struct Move {
int x, y;
};
Move mv[4] = { {1,0}, {0,1}, {0,-1}, {-1,0} };
void dfs(int i, int j) {
int spread = int(copyair[i][j] / 5);
int count = 0;
for (int k = 0; k < 4; k++) {
int movei = i + mv[k].x;
int movej = j + mv[k].y;
if (movei >= 0 && movei < r && movej >= 0 && movej < c) {
if (air[movei][movej] != -1) {
air[movei][movej] += spread;
count++;
}
}
}
air[i][j] = air[i][j] - spread * count;
}
void cleanair() {
int r1 = cleanmachine[0].first;
int c1 = cleanmachine[0].second;
int r2 = cleanmachine[1].first;
int c2 = cleanmachine[1].second;
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++)
copys[i][j] = air[i][j];
}
for (int i = 2; i < c; i++) {
air[r1][i] = copys[r1][i - 1];
}
air[r1][1] = 0;
for (int i = r1 - 1; i >= 0; i--) {
air[i][c - 1] = copys[i + 1][c - 1];
}
for (int i = c - 2; i >= 0; i--) {
air[0][i] = copys[0][i + 1];
}
for (int i = 1; i < r1; i++) {
air[i][0] = copys[i - 1][0];
}
for (int i = 2; i < c; i++) {
air[r2][i] = copys[r2][i - 1];
}
air[r2][1] = 0;
for (int i = r2 + 1; i < r; i++) {
air[i][c - 1] = copys[i - 1][c - 1];
}
for (int i = c - 2; i >= 0; i--) {
air[r - 1][i] = copys[r - 1][i + 1];
}
for (int i = r - 2; i > r2; i--) {
air[i][0] = copys[i + 1][0];
}
return;
}
void clearvisited() {
memset(copyair, 0, sizeof(copyair));
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
copyair[i][j] = air[i][j];
}
}
}
int main(void) {
cin >> r >> c >> t;
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
cin >> air[i][j];
if (air[i][j] == -1) {
cleanmachine.push_back(make_pair(i, j));
}
}
}
for (int k = 0; k < t; k++) {
clearvisited();
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if (copyair[i][j] != 0 && copyair[i][j] != -1)
dfs(i, j);
}
}
cleanair();
}
int answer = 0;
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
answer += air[i][j];
}
}
cout << answer+2 << endl;
}
'백준 알고리즘 > DFS' 카테고리의 다른 글
백준 감시 C++ (0) | 2019.10.19 |
---|---|
백준 사다리 조작 C++ (0) | 2019.10.19 |
백준 15683번 C++ (0) | 2019.09.19 |
백준 12100번 C++ (0) | 2019.09.19 |
백준 2468번 C++ (0) | 2019.02.25 |