-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2048_(Easy).cpp
More file actions
126 lines (105 loc) · 2.3 KB
/
2048_(Easy).cpp
File metadata and controls
126 lines (105 loc) · 2.3 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <iostream>
#include <vector>
using namespace std;
int max(int a, int b) { return a > b ? a : b; }
int n;
int answer = 0;
void Rotate(vector<vector<int>>& board) // right
{
vector<vector<int>> rotated(n, vector<int>(n));
for(int i=0; i<n; ++i)
{
for (int j=0; j<n; ++j)
{
rotated[j][n-1 - i] = board[i][j];
}
}
board = rotated;
}
void PushUp(vector<vector<int>>& board)
{
vector<vector<bool>> changed(n, vector<bool>(n, false));
for (int i=0; i<n; ++i)
{
for (int j=0; j<n; ++j)
{
int cur = board[i][j];
int ni = i-1;
while (ni >= 0)
{
if (board[ni][j] != 0)
break;
--ni;
} // ni == -1 혹은 0이 아닌 위치
if (ni < 0 || board[ni][j] != cur || changed[ni][j])
{
board[ni+1][j] = cur;
if (ni != i -1)
board[i][j] = 0;
}
else //if (!changed[ni][j])
{
changed[ni][j] = true;
board[ni][j] *= 2;
board[i][j] = 0;
}
}
}
}
int FindMax(const vector<vector<int>>& board)
{
int m = 0;
for (int i=0; i<n; ++i)
{
for (int j=0; j<n; ++j)
{
m = max(m, board[i][j]);
}
}
return m;
}
void Print(vector<vector<int>>& board)
{
for (int i=0; i<n; ++i)
{
for(int j=0; j<n; ++j)
{
cout << board[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
void search(int depth, int maxDepth, vector<vector<int>> board)
{
if (depth == maxDepth)
{
answer = max(FindMax(board), answer);
return;
}
for (int i=0; i<4; ++i)
{
Rotate(board);
vector<vector<int>> temp = board;
PushUp(temp);
search(depth+1, maxDepth, temp);
}
}
int main()
{
// freopen("../../input.txt", "r", stdin);
cin.tie(0);
ios_base::sync_with_stdio(false);
cin >> n;
vector<vector<int>> board(n, vector<int>(n));
for (int i=0; i<n; ++i)
{
for(int j=0; j<n; ++j)
{
cin >> board[i][j];
}
}
search(0, 5, board);
cout << answer << endl;
return 0;
}