-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbaekjoon12110.cpp
173 lines (160 loc) · 3.28 KB
/
baekjoon12110.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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <iostream>
#include <vector>
using namespace std;
int N, answer, xMove[4] = { 0, 0, -1, 1 }, yMove[4] = { -1, 1, 0, 0 };
vector<vector<int>> arr(21, vector<int>(21, 0));
vector<vector<int>> movingMap(vector<vector<int>> v, int direction) {
bool check[21][21] = { false, };
if(direction == 0){ // 왼쪽이동
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (v[i][j] == 0)
continue;
int x = i;
int y = j;
int nx, ny;
while (1) {
nx = x + xMove[direction];
ny = y + yMove[direction];
if (nx < 0 || ny < 0 || nx >= N || ny >= N)
break;
if (v[nx][ny] == 0) {
v[nx][ny] = v[x][y];
v[x][y] = 0;
}
else {
if (!check[nx][ny] && (v[nx][ny] == v[x][y])) {
v[nx][ny] += v[x][y];
v[x][y] = 0;
check[nx][ny] = true;
}
break;
}
x = nx;
y = ny;
}
}
}
}
if (direction == 1) { // 오른쪽이동
for (int i = 0; i < N; i++) {
for (int j = N-1; j >= 0; j--) {
if (v[i][j] == 0)
continue;
int x = i;
int y = j;
int nx, ny;
while (1) {
nx = x + xMove[direction];
ny = y + yMove[direction];
if (nx < 0 || ny < 0 || nx >= N || ny >= N)
break;
if (v[nx][ny] == 0) {
v[nx][ny] = v[x][y];
v[x][y] = 0;
}
else {
if (!check[nx][ny] && (v[nx][ny] == v[x][y])) {
v[nx][ny] += v[x][y];
v[x][y] = 0;
check[nx][ny] = true;
}
break;
}
x = nx;
y = ny;
}
}
}
}
if (direction == 2) { // 위로 이동
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (v[i][j] == 0)
continue;
int x = i;
int y = j;
int nx, ny;
while (1) {
nx = x + xMove[direction];
ny = y + yMove[direction];
if (nx < 0 || ny < 0 || nx >= N || ny >= N)
break;
if (v[nx][ny] == 0) {
v[nx][ny] = v[x][y];
v[x][y] = 0;
}
else {
if (!check[nx][ny] && (v[nx][ny] == v[x][y])) {
v[nx][ny] += v[x][y];
v[x][y] = 0;
check[nx][ny] = true;
}
break;
}
x = nx;
y = ny;
}
}
}
}
if (direction == 3) { // 아래 이동
for (int i = N-1; i >= 0; i--) {
for (int j = 0; j < N; j++) {
if (v[i][j] == 0)
continue;
int x = i;
int y = j;
int nx, ny;
while (1) {
nx = x + xMove[direction];
ny = y + yMove[direction];
if (nx < 0 || ny < 0 || nx >= N || ny >= N)
break;
if (v[nx][ny] == 0) {
v[nx][ny] = v[x][y];
v[x][y] = 0;
}
else {
if (!check[nx][ny] && (v[nx][ny] == v[x][y])) {
v[nx][ny] += v[x][y];
v[x][y] = 0;
check[nx][ny] = true;
}
break;
}
x = nx;
y = ny;
}
}
}
}
return v;
}
void gameStart(vector<vector<int>> & original, int cur) {
if (cur == 5) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
answer = max(answer, original[i][j]);
}
}
return;
}
for (int i = 0; i < 4; i++) {
vector<vector<int>> tempArr(21, vector<int>(21, 0));
tempArr = movingMap(original, i);
gameStart(tempArr, cur + 1);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> N;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cin >> arr[i][j];
}
}
gameStart(arr, 0);
cout << answer;
}