#include <iostream>
#include <vector>
#include <cmath>
#include <memory.h>
#include <queue>
#define MAX_N 64
using namespace std;
int board[MAX_N][MAX_N];
int nboard[MAX_N][MAX_N];
bool visited[MAX_N][MAX_N];
int n;
int dx[4] = { -1, 1, 0, 0 };
int dy[4] = { 0, 0, -1, 1 };
void RotateSubBoard(int sx, int sy, int len)
{
if (len < 2)
{
return;
}
for (int dx = 0; dx < len; dx++)
{
nboard[sx + dx][sy + len - 1] = board[sx][sy + dx]; // 윗줄
nboard[sx + dx][sy] = board[sx + len - 1][sy + dx]; // 아랫줄
nboard[sx + len - 1][sy + dx] = board[sx + len - 1 - dx][sy + len - 1]; // 오른쪽줄
nboard[sx][sy + dx] = board[sx + len - 1 - dx][sy]; // 왼쪽줄
}
RotateSubBoard(sx + 1, sy + 1, len - 2);
}
int MeltIce()
{
int ret = 0;
memcpy(nboard, board, sizeof(board));
for (int r = 0; r < n; r++)
{
for (int c = 0; c < n; c++)
{
if (board[r][c] == 0) continue;
int count = 0;
for (int dir = 0; dir < 4; dir++)
{
int nx = r + dx[dir];
int ny = c + dy[dir];
if (nx < 0 || nx >= n || ny < 0 || ny >= n) continue;
if (board[nx][ny] > 0) count++;
}
if (count < 3)
{
nboard[r][c] -= 1;
}
ret += nboard[r][c];
}
}
memcpy(board, nboard, sizeof(nboard));
return ret;
}
int bfs(int sx, int sy)
{
queue<pair<int, int>> q;
q.push(make_pair(sx, sy));
visited[sx][sy] = true;
int ret = 1;
while (!q.empty())
{
pair<int, int> cpos = q.front();
q.pop();
for (int dir = 0; dir < 4; dir++)
{
int nx = cpos.first + dx[dir];
int ny = cpos.second + dy[dir];
if (nx < 0 || nx >= n || ny < 0 || ny >= n) continue;
if (visited[nx][ny]) continue;
if (board[nx][ny] == 0) continue;
visited[nx][ny] = true;
q.push(make_pair(nx, ny));
ret++;
}
}
return ret;
}
int GetLargestArea()
{
memset(visited, false, sizeof(visited));
int ret = 0;
for (int r = 0; r < n; r++)
{
for (int c = 0; c < n; c++)
{
if (visited[r][c]) continue;
if (board[r][c] == 0) continue;
ret = max(ret, bfs(r, c));
}
}
return ret;
}
int main()
{
cin.tie(NULL);
ios_base::sync_with_stdio(false);
int N, Q;
cin >> N >> Q;
n = (int)pow(2.f, (float)N);
for (int r = 0; r < n; r++)
{
for (int c = 0; c < n; c++)
{
cin >> board[r][c];
}
}
int L;
int remain = 0;
for (int i = 0; i < Q; i++)
{
cin >> L;
int subLen = (int)pow(2.f, (float)L);
memset(nboard, 0, sizeof(nboard));
for (int r = 0; r < n; r += subLen)
{
for (int c = 0; c < n; c += subLen)
{
RotateSubBoard(r, c, subLen);
}
}
if(subLen > 1) memcpy(board, nboard, sizeof(board));
remain = MeltIce();
}
int maxNum = GetLargestArea();
cout << remain << "\n" << maxNum << endl;
}
Leave a comment