Muscardinus
2048(Easy) 본문
728x90
https://www.acmicpc.net/problem/12100
#include <iostream>
using namespace std;
int n;
int answer = 0;
struct BOARD {
int map[20][20];
void rotate() { //90도 회전
int temp[20][20] = { 0 };
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
temp[j][n-i-1] = map[i][j];
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
map[i][j] = temp[i][j];
}
}
}
void up() { //위로 올리기
int temp[20][20];
for (int x = 0; x < n; x++) {
int flag = 0; int target = -1;
for (int y = 0; y < n; y++) {
if (map[y][x] == 0) continue;
if (flag == 1 && temp[target][x] == map[y][x]) {
temp[target][x] *= 2;
flag = 0;
}
else {
temp[++target][x] = map[y][x];
flag = 1;
}
}
target++;
for (; target < n; target++) {
temp[target][x] = 0;
}
}
for (int y = 0; y < n; y++) {
for (int x = 0; x < n; x++) {
map[y][x] = temp[y][x];
}
}
}
int get_max() {
int result = 0;
for (int y = 0; y < n; y++) {
for (int x = 0; x < n; x++) {
if (result < map[y][x]) result = map[y][x];
}
}
return result;
}
};
void dfs(BOARD cur, int count) {
if (count == 5) {
if (answer < cur.get_max()) { answer = cur.get_max(); }
return;
}
for (int dir = 0; dir < 4; dir++) {
BOARD next = cur;
next.up();
dfs(next, count + 1);
cur.rotate();
}
}
int main() {
ios_base::sync_with_stdio(false);
BOARD board;
cin >> n;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> board.map[i][j];
}
}
dfs(board, 0);
cout << answer << "\n";
return 0;
}
728x90
Comments