1+ #include < bits/stdc++.h>
2+ using namespace std ;
3+ int N;
4+ int ret = INT_MIN ;
5+ vector<vector<int >> inp;
6+ int dy[4 ] = {-1 ,1 ,0 ,0 };
7+ int dx[4 ] = {0 ,0 ,-1 ,1 };
8+ void check () {
9+ for (int i=0 ; i<N; i++) {
10+ for (int j=0 ; j<N; j++) {
11+ ret = max (ret, inp[i][j]);
12+ }
13+ }
14+ }
15+ void move (int y, int x) {
16+ if (y == 0 ) {
17+ if (x == -1 ) { // 0,-1 왼쪽 방향
18+ for (int i=0 ; i<N; i++) {
19+ queue<int > q;
20+ for (int j=0 ; j<N; j++) {
21+ if (inp[i][j] == 0 ) continue ;
22+ q.push (inp[i][j]);
23+ }
24+ int index=0 ;
25+ while (!q.empty ()) {
26+ int cur = q.front ();
27+ q.pop ();
28+
29+ if (!q.empty () && cur == q.front ()) {
30+ cur *= 2 ;
31+ q.pop ();
32+ }
33+
34+ inp[i][index++] = cur;
35+ }
36+ for (int j=index; j<N; j++) {
37+ inp[i][j] = 0 ;
38+ }
39+ }
40+ } else { // 0,1 오른쪽 방향
41+ for (int i=0 ; i<N; i++) {
42+ queue<int > q;
43+ for (int j=N-1 ; j>=0 ; j--) {
44+ if (inp[i][j] == 0 ) continue ;
45+ q.push (inp[i][j]);
46+ }
47+ int index=N-1 ;
48+ while (!q.empty ()) {
49+ int cur = q.front ();
50+ q.pop ();
51+
52+ if (!q.empty () && cur == q.front ()) {
53+ cur *= 2 ;
54+ q.pop ();
55+ }
56+
57+ inp[i][index--] = cur;
58+ }
59+ for (int j=index; j>=0 ; j--) {
60+ inp[i][j] = 0 ;
61+ }
62+ }
63+ }
64+ } else {
65+ if (y == -1 ) { // -1,0 위 방향
66+ for (int i=0 ; i<N; i++) {
67+ queue<int > q;
68+ for (int j=0 ; j<N; j++) {
69+ if (inp[j][i] == 0 ) continue ;
70+ q.push (inp[j][i]);
71+ }
72+ int index=0 ;
73+ while (!q.empty ()) {
74+ int cur = q.front ();
75+ q.pop ();
76+
77+ if (!q.empty () && cur == q.front ()) {
78+ cur *= 2 ;
79+ q.pop ();
80+ }
81+
82+ inp[index++][i] = cur;
83+ }
84+ for (int j=index; j<N; j++) {
85+ inp[j][i] = 0 ;
86+ }
87+ }
88+ } else { // 1,0 아래 방향
89+ for (int i=0 ; i<N; i++) {
90+ queue<int > q;
91+ for (int j=N-1 ; j>=0 ; j--) {
92+ if (inp[j][i] == 0 ) continue ;
93+ q.push (inp[j][i]);
94+ }
95+ int index=N-1 ;
96+ while (!q.empty ()) {
97+ int cur = q.front ();
98+ q.pop ();
99+
100+ if (!q.empty () && cur == q.front ()) {
101+ cur *= 2 ;
102+ q.pop ();
103+ }
104+
105+ inp[index--][i] = cur;
106+ }
107+ for (int j=index; j>=0 ; j--) {
108+ inp[j][i] = 0 ;
109+ }
110+ }
111+ }
112+ }
113+ }
114+ void solve (int y, int x, int cnt) {
115+ check ();
116+ if (cnt == 6 ) {
117+ return ;
118+ }
119+
120+ move (y,x);
121+
122+ vector<vector<int >> tmp;
123+ tmp.resize (N, vector<int >(N));
124+ for (int i=0 ; i<4 ; i++) {
125+ tmp = inp;
126+ solve (dy[i],dx[i],cnt+1 );
127+ inp = tmp;
128+ }
129+ }
130+ int main () {
131+
132+ ios_base::sync_with_stdio (false );
133+ cin.tie (nullptr );
134+ cout.tie (nullptr );
135+
136+ cin >> N;
137+ inp.resize (N, vector<int >(N));
138+ for (int i=0 ; i<N; i++) {
139+ for (int j=0 ; j<N; j++) {
140+ cin >> inp[i][j];
141+ }
142+ }
143+
144+ if (N == 1 ) {
145+ cout << inp[0 ][0 ];
146+ return 0 ;
147+ }
148+
149+ vector<vector<int >> tmp;
150+ tmp.resize (N, vector<int >(N));
151+ for (int i=0 ; i<4 ; i++) {
152+ tmp = inp;
153+ solve (dy[i], dx[i], 1 );
154+ inp = tmp;
155+ }
156+
157+ cout << ret;
158+
159+ return 0 ;
160+ }
0 commit comments