정직한 시뮬레이션
결과값 2 때문에 bfs도 함께 구현했다.
소요시간 : 1시간 10분
#include<iostream>
#include<math.h>
#include<queue>
using namespace std;
int arr[64][64];
int temp[64][64];
int N;
int lim;//N에 대한 사이즈
void rot_ker(int si, int sj,int sz) {
//temp 전체에 오리지널을 복사
for (int i = 0; i < sz; i++) {
for (int j = 0; j < sz; j++) {
arr[si+j][sj + sz - 1 - i] = temp[si+i][sj+j];
}
}
//cout << si << "," << sj << "," << sz << endl;
//print_arr(-1);
}
void rot(int L) {
int sz = pow(2, L);
copy(&arr[0][0], &arr[0][0] + 64 * 64, &temp[0][0]);
for(int si = 0;si<lim;si+=sz){
for (int sj = 0; sj < lim; sj += sz) {
rot_ker(si, sj,sz);
}
}
}
bool in_box(int i, int j) {
return i >= 0 && i < lim&& j >= 0 && j < lim;
}
int di[] = { -1,1,0,0 };
int dj[] = { 0,0,-1,1 };
void down_ice() {
copy(&arr[0][0], &arr[0][0] + 64 * 64, &temp[0][0]);
for (int i = 0; i < lim; i++) {
for (int j = 0; j < lim; j++) {
//target = arr[i][j]
int sum_ice = 0;
for (int k = 0; k < 4; k++) {
int fi = i + di[k], fj = j + dj[k];
if (in_box(fi, fj)&&temp[fi][fj]>0) {
sum_ice++;
}
}
if (sum_ice < 3) {
arr[i][j] = max(temp[i][j] - 1, 0);
}
}
}
}
bool visited[64][64] = { false, };
struct pos {
int i, j;
pos(int ii, int jj) {
i = ii; j = jj;
}
};
pos get_seed() {
for (int i = 0; i < lim; i++) {
for (int j = 0; j < lim; j++) {
if (visited[i][j] == false&&arr[i][j]>0) {
return pos(i, j);
}
}
}
return pos(-1, -1);
}
int main() {
int Q;
int queries[1000];
cin >> N>>Q;
lim = pow(2, N);
for (int i = 0; i < lim; i++) {
for (int j = 0; j < lim; j++) {
cin >> arr[i][j];
}
}
for (int ctr = 0; ctr < Q; ctr++) {
cin >> queries[ctr];
rot(queries[ctr]);
down_ice();
}
//bfs
pos seed = get_seed();
int max_con = 0;
while (seed.i != -1) {
int cur_con = 0;
queue<pos> q;
q.push(seed);
visited[seed.i][seed.j] = true;
cur_con++;
while (!q.empty()) {
pos cur = q.front();
q.pop();
for (int k = 0; k < 4; k++) {
pos child = pos(cur.i + di[k], cur.j + dj[k]);
if (in_box(child.i,child.j)&&!visited[child.i][child.j]&&arr[child.i][child.j]>0) {
q.push(child);
visited[child.i][child.j] = true;
cur_con++;
}
}
}
max_con = max(max_con, cur_con);
seed = get_seed();
}
int sum_ice = 0;
for (int i = 0; i < lim; i++) {
for (int j = 0; j < lim; j++) {
sum_ice+=arr[i][j];
}
}
cout << sum_ice << endl;
cout << max_con << endl;
}