반응형
https://www.acmicpc.net/problem/2573
녹고, 지역 나누고
따로 수행하기!
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string.h>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
int n, m;
int map[301][301];
int temp[301][301];
int visited[301][301];
int year;
int dy[] = { 1,0,-1,0 };
int dx[] = { 0,1,0,-1 };
int areaCount;
int qcount;
int flag;
queue<pair<int, pair<int, int>>> q;
void copyMap() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
map[i][j] = temp[i][j];
}
}
qcount = q.size();
}
void melting() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int cnt = 0;
if (map[i][j] > 0) {
for (int d = 0; d < 4; d++) {
if (map[i + dx[d]][j + dy[d]] == 0) {
cnt++;
}
}
temp[i][j] = map[i][j] - cnt;
if (temp[i][j] < 0) temp[i][j] = 0;
}
}
}
copyMap();
}
void divideArea() {
while (!q.empty()) {
int val = q.front().first;
int cx = q.front().second.first;
int cy = q.front().second.second;
q.pop();
for (int i = 0; i < 4; i++) {
int nx = cx + dx[i];
int ny = cy + dy[i];
if (map[nx][ny] != 0 && visited[nx][ny] == 0 && nx >= 0 && ny >= 0 && nx < n && ny < m) {
visited[nx][ny] = 1;
q.push(make_pair(val+1, make_pair(nx, ny)));
}
}
}
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
scanf("%d", &map[i][j]);
}
}
year = 0;
while (-1) {
while (!q.empty()) q.pop();
memset(visited, 0, sizeof(visited));
memset(temp, 0, sizeof(temp));
areaCount = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (visited[i][j] == 0 && map[i][j] != 0) {
areaCount++;
if (areaCount >= 2) break;
q.push(make_pair(areaCount, make_pair(i, j)));
divideArea();
}
}
}
if (areaCount >= 2) break;
if (areaCount == 0) break;
melting();
year++;
}
if (areaCount == 0) {
printf("%d", 0);
}else {
printf("%d", year);
}
}
반응형