-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
104 lines (92 loc) · 2.98 KB
/
Main.java
File metadata and controls
104 lines (92 loc) · 2.98 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
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;
public class Main {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static int N;
static int[][] Map;
static int MAX = Integer.MIN_VALUE;
static ArrayList<Integer> list = new ArrayList<Integer>();
static ArrayList<Integer> plusList = new ArrayList<Integer>();
public static void main(String[] args) throws IOException {
run();
bw.flush();
bw.close();
br.close();
}
public static void run() throws IOException {
N = Integer.parseInt(br.readLine());
Map = new int[N][N];
for(int i=0; i<N; i++){
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
for(int j=0; j<N; j++){
Map[i][j] = Integer.parseInt(st.nextToken());
}
}
for(int i=1; i<=4; i++) DFS(0, i, Map);
bw.write(MAX+"\n");
}
public static void DFS(int count, int idx, int[][] map) {
if(count == 5){
for(int i=0; i<N; i++){
for(int j=0; j<N; j++){
MAX = Math.max(MAX, map[i][j]);
}
}
return;
}
int[][] nextMap = new int[N][N];
for (int i=0; i<N; i++){
list.clear();
plusList.clear();
// idx: 1=상, 2=하, 3=좌, 4=우
// 상하로 움직일 때는 세로 줄 기준으로 움직이니까 [j][i]
// 좌우로 움직일 때는 가로 줄 기준으로 움직이니까 그대로 [i][j]
// plusList에서 꺼낼 때, 아래부터 또는 오른쪽부터 nextMap에 값을 넣으려면 list가 역순으로 되어있어야 함.
if(idx==1){
for(int j=0; j<N; j++){
if(map[j][i] != 0) list.add(map[j][i]);
}
}
if(idx==2){
for(int j=0; j<N; j++){
if(map[j][i] != 0) list.add(map[j][i]);
}
Collections.reverse(list);
}
if(idx==3){
for(int j=0; j<N; j++){
if(map[i][j] != 0) list.add(map[i][j]);
}
}
if(idx==4){
for(int j=0; j<N; j++){
if(map[i][j] != 0) list.add(map[i][j]);
}
Collections.reverse(list);
}
for(int j=0; j<list.size(); j++){
if(j < list.size()-1 && list.get(j).equals(list.get(j+1))){
plusList.add(list.get(j)*2);
j++;
} else {
plusList.add(list.get(j));
}
}
for(int j=0; j<plusList.size(); j++){
// 그림 그려보면, 상하좌우 순서로 값을 넣는 것임을 알 수 있음.
if(idx == 1) nextMap[j][i] = plusList.get(j); // 좌상단부터 상->하 방향
else if(idx == 2) nextMap[N-1-j][i] = plusList.get(j); // 좌하단부터 하->상 방향
else if(idx == 3) nextMap[i][j] = plusList.get(j); // 좌상단부터 좌->우 방향
else if(idx == 4) nextMap[i][N-1-j] = plusList.get(j); // 우상단부터 우->좌 방향
}
}
for(int i=1; i<=4; i++) DFS(count+1, i, nextMap);
}
}