https://www.acmicpc.net/problem/17144
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
const int MAX = 55;
int r, c, t;
int map[MAX][MAX];
vector<pair<int, int>> loc;
struct MOVE { int x, y; };
MOVE mv[4] = { {1,0}, {-1,0}, {0,1}, {0,-1} };
void printmap_tmp(int tmp_m[MAX][MAX]) {
cout << endl;
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
cout << tmp_m[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
void MoveDust() {
int tmp_m[MAX][MAX];
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
tmp_m[i][j] = map[i][j];
}
}
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if (tmp_m[i][j] > 0) {
int count = 0;
int v = int(tmp_m[i][j] / 5);
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 (find(loc.begin(), loc.end(), make_pair(movei, movej)) == loc.end()) {
count++;
map[movei][movej] += v;
}
}
}
map[i][j] = map[i][j] - v * count;
}
}
}
}
void Wind() {
int r1 = loc[0].first;
int c1 = loc[0].second;
int r2 = loc[1].first;
int c2 = loc[1].second;
int tmp_m[MAX][MAX];
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
tmp_m[i][j] = map[i][j];
}
}
for (int j = 2; j < c; j++) {
map[r1][j] = tmp_m[r1][j - 1];
}
map[r1][1] = 0;
for (int i = r1 - 1; i >= 0; i--) {
map[i][c - 1] = tmp_m[i + 1][c - 1];
}
for (int j = c - 2; j >= 0; j--) {
map[0][j] = tmp_m[0][j + 1];
}
for (int i = 1; i < r1; i++) {
map[i][0] = tmp_m[i - 1][0];
}
for (int j = 2; j < c; j++) {
map[r2][j] = tmp_m[r2][j - 1];
}
map[r2][1] = 0;
for (int i = r2 + 1; i < r; i++) {
map[i][c - 1] = tmp_m[i - 1][c - 1];
}
for (int j = c - 2; j >= 0; j--) {
map[r - 1][j] = tmp_m[r - 1][j + 1];
}
for (int i = r - 2; i > r2; i--) {
map[i][0] = tmp_m[i + 1][0];
}
map[loc[0].first][loc[0].second] = 0;
map[loc[1].first][loc[1].second] = 0;
}
void printmap() {
cout << endl;
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
cout << map[i][j] << " ";
}
cout << endl;
}
}
int main(void) {
cin >> r >> c >> t;
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
cin >> map[i][j];
if (map[i][j] == -1) {
loc.push_back({ i,j });
}
}
}
for (int testcase = 0; testcase < t; testcase++) {
MoveDust();
Wind();
}
int answer = 0;
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if (map[i][j] > 0) {
answer += map[i][j];
}
}
}
cout << answer << endl;
}
'백준 알고리즘 > BFS' 카테고리의 다른 글
퍼즐 조각 채우기 C++ (0) | 2022.02.06 |
---|---|
온풍기 안녕! C++ (0) | 2022.01.22 |
연구소 3 C++ (0) | 2021.10.17 |
스타트 택시 C++ (0) | 2021.10.13 |
연구소 3 C++ (0) | 2021.04.22 |