Skip to content

Commit f973302

Browse files
authored
[20250707] BOJ / G1 / 2048 (Easy) / 권혁준
1 parent 7f02cc3 commit f973302

File tree

1 file changed

+180
-0
lines changed

1 file changed

+180
-0
lines changed
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
```java
2+
import java.util.*;
3+
import java.io.*;
4+
5+
class IOController {
6+
BufferedReader br;
7+
BufferedWriter bw;
8+
StringTokenizer st;
9+
10+
public IOController() {
11+
br = new BufferedReader(new InputStreamReader(System.in));
12+
bw = new BufferedWriter(new OutputStreamWriter(System.out));
13+
st = new StringTokenizer("");
14+
}
15+
16+
String nextLine() throws Exception {
17+
String line = br.readLine();
18+
st = new StringTokenizer(line);
19+
return line;
20+
}
21+
22+
String nextToken() throws Exception {
23+
while (!st.hasMoreTokens()) nextLine();
24+
return st.nextToken();
25+
}
26+
27+
int nextInt() throws Exception {
28+
return Integer.parseInt(nextToken());
29+
}
30+
31+
long nextLong() throws Exception {
32+
return Long.parseLong(nextToken());
33+
}
34+
35+
double nextDouble() throws Exception {
36+
return Double.parseDouble(nextToken());
37+
}
38+
39+
void close() throws Exception {
40+
bw.flush();
41+
bw.close();
42+
}
43+
44+
void write(String content) throws Exception {
45+
bw.write(content);
46+
}
47+
48+
}
49+
50+
public class Main {
51+
52+
static IOController io;
53+
54+
//
55+
56+
static int N;
57+
static int[][] board;
58+
static int ans = 0;
59+
static String[] dirs = {"left", "right", "up", "down"};
60+
61+
public static void main(String[] args) throws Exception {
62+
63+
io = new IOController();
64+
65+
init();
66+
solve();
67+
68+
io.close();
69+
70+
}
71+
72+
public static void init() throws Exception {
73+
74+
N = io.nextInt();
75+
board = new int[N][N];
76+
for(int i=0;i<N;i++) for(int j=0;j<N;j++) board[i][j] = io.nextInt();
77+
78+
}
79+
80+
static void solve() throws Exception {
81+
82+
backtracking(0);
83+
io.write(ans + "\n");
84+
85+
}
86+
87+
static void backtracking(int turn) {
88+
89+
for(int i=0;i<N;i++) for(int j=0;j<N;j++) ans = Math.max(ans, board[i][j]);
90+
91+
if(turn >= 5) return;
92+
93+
for(String dir : dirs) {
94+
95+
int[][] copy = new int[N][N];
96+
for(int i=0;i<N;i++) for(int j=0;j<N;j++) copy[i][j] = board[i][j];
97+
move(dir);
98+
backtracking(turn+1);
99+
for(int i=0;i<N;i++) for(int j=0;j<N;j++) board[i][j] = copy[i][j];
100+
101+
}
102+
103+
}
104+
105+
static void move(String direction) {
106+
107+
if(direction.equals("left")) {
108+
for(int i=0;i<N;i++) {
109+
List<Integer> list = new ArrayList<>();
110+
for(int j=0;j<N;j++) if(board[i][j] != 0) {
111+
list.add(board[i][j]);
112+
}
113+
List<Integer> newList = compress(list);
114+
for(int j=0;j<N;j++) {
115+
board[i][j] = j >= newList.size() ? 0 : newList.get(j);
116+
}
117+
}
118+
}
119+
else if(direction.equals("right")) {
120+
for(int i=0;i<N;i++) {
121+
List<Integer> list = new ArrayList<>();
122+
for(int j=N-1;j>=0;j--) if(board[i][j] != 0) {
123+
list.add(board[i][j]);
124+
}
125+
List<Integer> newList = compress(list);
126+
for(int j=N-1;j>=0;j--) {
127+
board[i][j] = N-j > newList.size() ? 0 : newList.get(N-j-1);
128+
}
129+
}
130+
}
131+
else if(direction.equals("up")) {
132+
for(int j=0;j<N;j++) {
133+
List<Integer> list = new ArrayList<>();
134+
for(int i=0;i<N;i++) if(board[i][j] != 0) {
135+
list.add(board[i][j]);
136+
}
137+
List<Integer> newList = compress(list);
138+
for(int i=0;i<N;i++) {
139+
board[i][j] = i >= newList.size() ? 0 : newList.get(i);
140+
}
141+
}
142+
}
143+
else {
144+
for(int j=0;j<N;j++) {
145+
List<Integer> list = new ArrayList<>();
146+
for(int i=N-1;i>=0;i--) if(board[i][j] != 0) {
147+
list.add(board[i][j]);
148+
}
149+
List<Integer> newList = compress(list);
150+
for(int i=N-1;i>=0;i--) {
151+
board[i][j] = N-i > newList.size() ? 0 : newList.get(N-i-1);
152+
}
153+
}
154+
}
155+
156+
}
157+
158+
static List<Integer> compress(List<Integer> list) {
159+
160+
List<Integer> newList = new ArrayList<>();
161+
for(int j=0;j<list.size();j++) {
162+
int prev = list.get(j);
163+
if(j == list.size()-1) {
164+
newList.add(prev);
165+
break;
166+
}
167+
int next = list.get(j+1);
168+
if(prev == next) {
169+
newList.add(prev<<1);
170+
j++;
171+
}
172+
else newList.add(prev);
173+
}
174+
175+
return newList;
176+
177+
}
178+
179+
}
180+
```

0 commit comments

Comments
 (0)