#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
const int MAX = 21;
int answer = 1;
int n;
int board[MAX][MAX];
void moveblock(int k) {
switch (k) {
case 0:
for (int i = 0; i < n; i++) {
queue<int> q;
for (int j = n - 1; j >= 0; j--) {
if (board[j][i] != 0)
q.push(board[j][i]);
board[j][i] = 0;
}
int index = n - 1;
while (!q.empty()) {
int value = q.front();
q.pop();
if (board[index][i] == 0)
board[index][i] = value;
else if (board[index][i] == value) {
board[index][i] *= 2;
index--;
}
else {
index--;
board[index][i] = value;
}
}
}
break;
case 1:
for (int i = 0; i < n; i++) {
queue<int> q;
for (int j = 0; j < n; j++) {
if (board[j][i] != 0)
q.push(board[j][i]);
board[j][i] = 0;
}
int index = 0;
while (!q.empty()) {
int value = q.front();
q.pop();
if (board[index][i] == 0)
board[index][i] = value;
else if (board[index][i] == value) {
board[index][i] *= 2;
index++;
}
else {
index++;
board[index][i] = value;
}
}
}
break;
case 2:
for (int i = 0; i < n; i++) {
queue<int> q;
for (int j = n - 1; j >= 0; j--) {
if (board[i][j] != 0)
q.push(board[i][j]);
board[i][j] = 0;
}
int index = n - 1;
while (!q.empty()) {
int value = q.front();
q.pop();
if (board[i][index] == 0)
board[i][index] = value;
else if (board[i][index] == value) {
board[i][index] *= 2;
index--;
}
else {
index--;
board[i][index] = value;
}
}
}
break;
case 3:
for (int i = 0; i < n; i++) {
queue<int> q;
for (int j = 0; j < n; j++) {
if (board[i][j] != 0)
q.push(board[i][j]);
board[i][j] = 0;
}
int index = 0;
while (!q.empty()) {
int value = q.front();
q.pop();
if (board[i][index] == 0)
board[i][index] = value;
else if (board[i][index] == value) {
board[i][index] *= 2;
index++;
}
else {
index++;
board[i][index] = value;
}
}
}
break;
}
}
void dfs(int count) {
if (count == 5) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
answer = max(answer, board[i][j]);
}
}
return;
}
int copyboard[MAX][MAX];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
copyboard[i][j] = board[i][j];
}
}
for (int k = 0; k < 4; k++) {
moveblock(k);
dfs(count + 1);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
board[i][j] = copyboard[i][j];
}
}
}
}
int main(void) {
cin >> n;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> board[i][j];
}
}
dfs(0);
cout << answer << endl;
return 0;
}
'백준 알고리즘 > DFS' 카테고리의 다른 글
백준 미세먼지 안녕 C++ (0) | 2019.10.15 |
---|---|
백준 15683번 C++ (0) | 2019.09.19 |
백준 2468번 C++ (0) | 2019.02.25 |
백준 2667번 C++ (0) | 2019.02.25 |
백준 1260번 C++ (0) | 2019.02.19 |