https://www.acmicpc.net/problem/20058
#include <iostream>
#include <cmath>
#include <cstring>
const int MAX = 65;
int a[MAX][MAX];
int visited[MAX][MAX];
int n, q, l;
int temp_num = 0;
struct Ice { int x, y; };
Ice ic[4] = { { -1,0 },{ 1,0 },{ 0,1 },{ 0,-1 } };
int temp[MAX][MAX];
int pow_n;
using namespace std;
void DivideMap() {
int pow_l = pow(2, l);
int count = pow_n / pow_l;
int count2 = count;
int index_i = 1;
while (count > 0) {
int range_i = index_i + pow_l;
int index_j = 1;
while (count2 > 0)
{
int ii = index_i;
int range_j = index_j + pow_l;
int jj = range_j - 1;
for (; index_i < range_i; index_i++) {
for (; index_j < range_j; index_j++) {
temp[ii][jj] = a[index_i][index_j];
ii += 1;
}
jj -= 1;
ii -= pow_l;
index_j -= pow_l;
}
index_j += pow_l;
count2--;
index_i -= pow_l;
}
index_i += pow_l;
count--;
count2 = pow_n / pow_l;
}
for (int i = 1; i <= pow_n; i++) {
for (int j = 1; j <= pow_n; j++) {
a[i][j] = temp[i][j];
}
}
}
void ReduceIce() {
int movei, movej;
for (int i = 1; i <= pow_n; i++) {
for (int j = 1; j <= pow_n; j++) {
int count = 0;
if (temp[i][j] > 0) {
for (int k = 0; k < 4; k++) {
movei = i + ic[k].x;
movej = j + ic[k].y;
if (movei > 0 && movej > 0 && movei <= pow_n && movej <= pow_n) {
if (temp[movei][movej] > 0)
count++;
}
}
if (count < 3)
a[i][j] -= 1;
}
}
}
}
void dfs(int i, int j) {
visited[i][j] = 1;
for (int k = 0; k < 4; k++) {
int movei = i + ic[k].x;
int movej = j + ic[k].y;
if (movei > 0 && movej > 0 && movei <= pow_n && movej <= pow_n && visited[movei][movej] != 1 && a[movei][movej] >0) {
temp_num++;
dfs(movei, movej);
}
}
}
int Find_Ice() {
memset(visited, -1, sizeof(visited));
int answer = 0;
int count = 0;
for (int i = 1; i <= pow_n; i++) {
for (int j = 1; j <= pow_n; j++) {
count += a[i][j];
if (a[i][j] > 0 && visited[i][j] != 1) {
temp_num = 1;
dfs(i, j);
if (temp_num > answer)
answer = temp_num;
}
}
}
cout << count << endl;
return answer;
}
int main(void) {
cin >> n >> q;
pow_n = pow(2, n);
for (int i = 1; i <= pow_n; i++) {
for (int j = 1; j <= pow_n; j++) {
cin >> a[i][j];
}
}
for (int i = 0; i < q; i++) {
cin >> l;
DivideMap();
ReduceIce();
}
cout << Find_Ice() << endl;
}
'백준 알고리즘 > DFS' 카테고리의 다른 글
마법사 상어와 파이어스톰 (0) | 2021.04.01 |
---|---|
청소년 상어 C++ (0) | 2021.03.08 |
마법사 상어와 파이어스톰 C++ (0) | 2021.02.28 |
원판 돌리기 C++ (0) | 2021.02.28 |
백준 감시 C++ (0) | 2019.10.19 |