-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path12094.java
More file actions
143 lines (128 loc) · 4.16 KB
/
Copy path12094.java
File metadata and controls
143 lines (128 loc) · 4.16 KB
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
// https://www.acmicpc.net/problem/12094
// 2048 (Hard)
import java.util.*;
import java.lang.*;
import java.io.*;
class Main {
static int n, map[][], answer = 0, MAX = 0;
static final int[] DIRX = {-1, 0, 1, 0}, DIRY = {0, 1, 0, -1};
public static void main(String[] args) throws IOException {
BufferedReader scan = new BufferedReader(new InputStreamReader(System.in));
n = Integer.parseInt(scan.readLine());
map = new int[n][n];
for(int i=0; i<n; i++){
StringTokenizer st = new StringTokenizer(scan.readLine());
for(int j=0; j<n; j++){
map[i][j] = Integer.parseInt(st.nextToken());
answer = Math.max(answer, map[i][j]);
MAX += map[i][j];
}
}
dfs(0);
System.out.println(answer);
}
public static void dfs(int depth){
//print("cur depth: "+depth , map, answer);
int curMax = getMax(map);
answer = Math.max(answer, curMax);
if(depth == 10) return;
if(curMax * Math.pow(2, 10-depth) <= answer) return; // too small stop
if(curMax * 2 > MAX) return; // reach max stop
int[][] reset = map;
for(int i=0; i<4; i++){
map = getMovedMap(map, i);
if(map != null) {
dfs(depth + 1);
}
map = reset;
}
}
public static int[][] clone(int[][] arr){
int[][] result = new int[n][n];
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
result[i][j] = arr[i][j];
return result;
}
// return new move result(changed map), if not changed return null
public static int[][] getMovedMap(int[][] MAP, int dir){
int[][] map = clone(MAP);
boolean changed = false;
if(dir == 0 || dir == 3){
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
if(map[i][j] == 0) continue;
if(move(map, i, j, dir))
changed = true;
}
}
}
else{ // if(dir == 1 || dir == 2)
for(int i=n-1; i>=0; i--){
for(int j=n-1; j>=0; j--){
if(map[i][j] == 0) continue;
if(move(map, i, j, dir))
changed = true;
}
}
}
if(!changed) return null;
return map;
}
public static boolean move(int[][] map, int X, int Y, int dir){
int x = X, y = Y;
boolean changed = false;
// find thre same number to combine
while(true){
x -= DIRX[dir]; y -= DIRY[dir];
if(!check(x, y)) break;
if(map[x][y] == 0) continue;
else if(map[x][y] == map[X][Y]){
changed = true;
map[x][y] = 0;
map[X][Y] *= 2;
break;
}
else break;
}
x = X; y = Y;
int locX = x, locY = y;
// move map[X][Y] to right location
while(true){
x += DIRX[dir]; y += DIRY[dir];
if(!check(x, y)) break;
if(map[x][y] == 0){
locX = x; locY = y;
continue;
}
else break;
}
if(locX == X && locY == Y) return changed;
map[locX][locY] = map[X][Y];
map[X][Y] = 0;
return true;
}
public static boolean check(int x, int y){
if(x < 0 || n <= x || y < 0 || n <= y)
return false;
return true;
}
public static int getMax(int[][] map){
int max = 0;
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
if(max < map[i][j])
max = map[i][j];
return max;
}
public static void print(String str, int[][] map, int answer){
StringBuilder sb = new StringBuilder();
sb.append(str).append('\n');
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
sb.append(map[i][j]).append(' ');
}sb.append('\n');
}sb.append(answer).append('\n');
System.out.println(sb);
}
}