[BOJ] 12100 2048(Easy)

Time Lapse :1hour 0min 0sec

12100.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <vector>
using namespace std;
int N;
long long answer = 0;

vector<vector<int>> rotate(vector<vector<int>> b) {
vector<vector<int>> ret(N+1, vector<int>(N+1));
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
ret[j][N - i - 1] = b[i][j];
}
}
return ret;
}
void DFS(int depth, vector < vector <int>> board) {
if (depth <= 5) {
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
answer = answer < board[i][j] ? board[i][j] : answer;
if(depth==5)
return;
}
vector<vector<int>> _board(N+1, vector<int>(N+1));
vector<vector<int>> _board2(N+1, vector<int>(N+1));
_board = board;
for (int i = 0; i < 4; i++) {
_board2 = _board;
for (int j = 0; j < N; j++) {
int zero_cnt = 0;
for (int k = 0; k < _board2[j].size(); k++) {
if (_board2[j][k] == 0) {
_board2[j].erase(_board2[j].begin() + k);
--k; ++zero_cnt;
}
}
for (int k = 0; k < zero_cnt; k++)
_board2[j].push_back(0);
for (int k = 0; k < _board2[j].size()-1; k++) {
if (_board2[j][k] == _board2[j][k + 1]&&_board2[j][k]!=0) {
_board2[j][k] *= 2;
_board2[j].erase(_board2[j].begin() + k + 1);
_board2[j].push_back(0);
}
}
}
DFS(depth + 1, _board2);
_board = rotate(_board);
}
}

int main(void)
{
cin >> N;
vector<vector<int>> board(N+1, vector<int>(N+1));
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
cin >> board[i][j];

DFS(0, board);
cout << answer << endl;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/23/PS/BOJ/12100/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.