Skip to content

Commit 0854a5a

Browse files
authored
[20250627] BOJ / P4 / 2048 (Hard) / 이강현
1 parent 69a56dd commit 0854a5a

File tree

1 file changed

+166
-0
lines changed

1 file changed

+166
-0
lines changed
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class Main {
6+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
8+
static StringTokenizer st;
9+
static int N;
10+
static int ans = 0;
11+
12+
public static void main(String[] args) throws IOException {
13+
N = Integer.parseInt(br.readLine());
14+
int[][] board = new int[N][N];
15+
16+
for (int i = 0; i < N; i++) {
17+
st = new StringTokenizer(br.readLine());
18+
for (int j = 0; j < N; j++) {
19+
board[i][j] = Integer.parseInt(st.nextToken());
20+
}
21+
}
22+
23+
backtracking(0, board);
24+
25+
bw.write(ans +"");
26+
bw.close();
27+
}
28+
static void backtracking(int moveCnt, int[][] board) throws IOException {
29+
if(moveCnt == 10) {
30+
for (int i = 0; i < N; i++) {
31+
for (int j = 0; j < N; j++) {
32+
ans = Math.max(ans, board[i][j]);
33+
}
34+
}
35+
return;
36+
}
37+
int max = 0;
38+
for (int i = 0; i < N; i++) {
39+
for (int j = 0; j < N; j++) {
40+
max = Math.max(max, board[i][j]);
41+
}
42+
}
43+
if(max * (1<<(10 - moveCnt)) <= ans) return;
44+
for (int k = 1; k <= 4; k++) {
45+
int[][] newBoard = new int[N][N];
46+
for (int i = 0; i < N; i++) {
47+
System.arraycopy(board[i], 0, newBoard[i], 0, N);
48+
}
49+
move(k, newBoard);
50+
backtracking(moveCnt + 1, newBoard);
51+
}
52+
}
53+
static void move(int opt, int[][] board){ //1이면 위, 2면 아래, 3이면 왼쪽, 4면 오른쪽
54+
boolean[][] visited = new boolean[N][N];
55+
if(opt == 1) {
56+
for (int i = 1; i < N; i++) {
57+
for (int j = 0; j < N; j++) {
58+
if(board[i][j] == 0) { //빈칸이면 넘어감
59+
continue;
60+
}
61+
int tmp = i-1; //tmp는 현재 칸과 마주할 칸을 나타냄
62+
while(tmp >= 0 && board[tmp][j]==0){ //바로 윗칸이 빈칸이 아닐때까지 이동
63+
tmp--;
64+
}
65+
if(tmp<0){// 현재 칸 위가 다 빈칸일 경우
66+
board[0][j] = board[i][j]; //맨 윗칸에 현재칸 두고 끝
67+
board[i][j] = 0;
68+
continue;
69+
}
70+
if(board[i][j] == board[tmp][j] && !visited[tmp][j]){ //충돌시 같고 이미 합쳐진 블록이 아닐경우
71+
board[tmp][j] *= 2;
72+
visited[tmp][j] = true;
73+
board[i][j] = 0;
74+
}else{ //다르다면 tmp 한칸 앞에 그냥 둠
75+
board[tmp+1][j] = board[i][j];
76+
if(tmp+1 != i){ //tmp가 애초에 i랑 딱 붙어있을 경우에는 그대로가 유지되게 함.
77+
board[i][j] = 0;
78+
}
79+
}
80+
}
81+
}
82+
}else if(opt == 2) {
83+
for (int i = N-2; i >= 0; i--) {
84+
for (int j = 0; j < N; j++) {
85+
if(board[i][j] == 0) {
86+
continue;
87+
}
88+
int tmp = i+1;
89+
while(tmp < N && board[tmp][j]==0){
90+
tmp++;
91+
}
92+
if(tmp>=N){
93+
board[N-1][j] = board[i][j];
94+
board[i][j] = 0;
95+
continue;
96+
}
97+
if(board[i][j] == board[tmp][j] && !visited[tmp][j]){
98+
board[tmp][j] *= 2;
99+
visited[tmp][j] = true;
100+
board[i][j] = 0;
101+
}else{
102+
board[tmp-1][j] = board[i][j];
103+
if(tmp-1 != i){
104+
board[i][j] = 0;
105+
}
106+
}
107+
}
108+
}
109+
}else if(opt == 3) {
110+
for (int j = 1; j < N; j++) {
111+
for (int i = 0; i < N; i++) {
112+
if(board[i][j] == 0) {
113+
continue;
114+
}
115+
int tmp = j-1;
116+
while(tmp >= 0 && board[i][tmp]==0){
117+
tmp--;
118+
}
119+
if(tmp<0){
120+
board[i][0] = board[i][j];
121+
board[i][j] = 0;
122+
continue;
123+
}
124+
if(board[i][j] == board[i][tmp] && !visited[i][tmp]){
125+
board[i][tmp] *= 2;
126+
visited[i][tmp] = true;
127+
board[i][j] = 0;
128+
}else{
129+
board[i][tmp+1] = board[i][j];
130+
if(tmp+1 != j){
131+
board[i][j] = 0;
132+
}
133+
}
134+
}
135+
}
136+
}else if(opt == 4) {
137+
for (int j = N-2; j >= 0; j--) {
138+
for (int i = 0; i < N; i++) {
139+
if(board[i][j] == 0) {
140+
continue;
141+
}
142+
int tmp = j+1;
143+
while(tmp < N && board[i][tmp]==0){
144+
tmp++;
145+
}
146+
if(tmp>=N){
147+
board[i][N-1] = board[i][j];
148+
board[i][j] = 0;
149+
continue;
150+
}
151+
if(board[i][j] == board[i][tmp] && !visited[i][tmp]){
152+
board[i][tmp] *= 2;
153+
visited[i][tmp] = true;
154+
board[i][j] = 0;
155+
}else{
156+
board[i][tmp-1] = board[i][j];
157+
if(tmp-1 != j){
158+
board[i][j] = 0;
159+
}
160+
}
161+
}
162+
}
163+
}
164+
}
165+
}
166+
```

0 commit comments

Comments
 (0)