Skip to content

Commit 980caef

Browse files
authored
Merge pull request #389 from AlgorithmWithGod/Seol-JY
[20250626] BOJ / G1 / 2048 (Easy) / 설진영
2 parents e287695 + cf87d54 commit 980caef

File tree

1 file changed

+133
-0
lines changed

1 file changed

+133
-0
lines changed
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
```java
2+
import java.io.BufferedReader;
3+
import java.io.InputStreamReader;
4+
import java.util.StringTokenizer;
5+
6+
public class Main {
7+
8+
static int n, answer;
9+
static int[][] map;
10+
11+
public static void main(String[] args) throws Exception {
12+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
13+
n = Integer.parseInt(br.readLine());
14+
answer = 0;
15+
map = new int[n][n];
16+
17+
for (int i = 0; i < n; i++) {
18+
StringTokenizer st = new StringTokenizer(br.readLine());
19+
for (int j = 0; j < n; j++) {
20+
map[i][j] = Integer.parseInt(st.nextToken());
21+
}
22+
}
23+
24+
game(0);
25+
System.out.println(answer);
26+
}
27+
28+
public static void game(int count) {
29+
if (count == 5) {
30+
findMax();
31+
return;
32+
}
33+
34+
int[][] backup = new int[n][n];
35+
for (int i = 0; i < n; i++) {
36+
backup[i] = map[i].clone();
37+
}
38+
39+
for (int dir = 0; dir < 4; dir++) {
40+
move(dir);
41+
game(count + 1);
42+
43+
for (int i = 0; i < n; i++) {
44+
map[i] = backup[i].clone();
45+
}
46+
}
47+
}
48+
49+
public static void move(int dir) {
50+
if (dir == 0) {
51+
for (int j = 0; j < n; j++) {
52+
int index = 0;
53+
int prev = 0;
54+
for (int i = 0; i < n; i++) {
55+
if (map[i][j] != 0) {
56+
if (prev == map[i][j]) {
57+
map[index - 1][j] = prev * 2;
58+
prev = 0;
59+
} else {
60+
prev = map[i][j];
61+
map[index][j] = prev;
62+
index++;
63+
}
64+
if (i != index - 1) map[i][j] = 0;
65+
}
66+
}
67+
}
68+
} else if (dir == 1) {
69+
for (int j = 0; j < n; j++) {
70+
int index = n - 1;
71+
int prev = 0;
72+
for (int i = n - 1; i >= 0; i--) {
73+
if (map[i][j] != 0) {
74+
if (prev == map[i][j]) {
75+
map[index + 1][j] = prev * 2;
76+
prev = 0;
77+
} else {
78+
prev = map[i][j];
79+
map[index][j] = prev;
80+
index--;
81+
}
82+
if (i != index + 1) map[i][j] = 0;
83+
}
84+
}
85+
}
86+
} else if (dir == 2) {
87+
for (int i = 0; i < n; i++) {
88+
int index = 0;
89+
int prev = 0;
90+
for (int j = 0; j < n; j++) {
91+
if (map[i][j] != 0) {
92+
if (prev == map[i][j]) {
93+
map[i][index - 1] = prev * 2;
94+
prev = 0;
95+
} else {
96+
prev = map[i][j];
97+
map[i][index] = prev;
98+
index++;
99+
}
100+
if (j != index - 1) map[i][j] = 0;
101+
}
102+
}
103+
}
104+
} else {
105+
for (int i = 0; i < n; i++) {
106+
int index = n - 1;
107+
int prev = 0;
108+
for (int j = n - 1; j >= 0; j--) {
109+
if (map[i][j] != 0) {
110+
if (prev == map[i][j]) {
111+
map[i][index + 1] = prev * 2;
112+
prev = 0;
113+
} else {
114+
prev = map[i][j];
115+
map[i][index] = prev;
116+
index--;
117+
}
118+
if (j != index + 1) map[i][j] = 0;
119+
}
120+
}
121+
}
122+
}
123+
}
124+
125+
public static void findMax() {
126+
for (int i = 0; i < n; i++) {
127+
for (int j = 0; j < n; j++) {
128+
answer = Math.max(answer, map[i][j]);
129+
}
130+
}
131+
}
132+
}
133+
```

0 commit comments

Comments
 (0)