-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
88 lines (76 loc) · 2.5 KB
/
Main.java
File metadata and controls
88 lines (76 loc) · 2.5 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
import java.io.BufferedReader;
import java.util.StringTokenizer;
import java.io.InputStreamReader;
import java.io.IOException;
public class Main {
static int N,M,answer;
static int[][] map;
static boolean[][] visited;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
map = new int[N][M];
for(int i=0; i<N; i++){
st = new StringTokenizer(br.readLine());
for(int j=0; j<M; j++){
map[i][j] = Integer.parseInt(st.nextToken());
}
}
visited = new boolean[N][M];
answer = 0;
for(int i=0; i<N; i++){
for(int j=0; j<M; j++){
visited[i][j] = true;
dfs(i,j,1,map[i][j]);
checkExceptionBlock(i,j);
visited[i][j] = false;
}
}
System.out.println(answer);
br.close();
}
public static void dfs(int x, int y, int cnt, int sum) {
if( cnt == 4) {
answer = Math.max(answer, sum);
return;
}
int[] dx = {-1,1,0,0};
int[] dy = {0,0,-1,1};
for(int i=0; i<4; i++){
int nx = x + dx[i];
int ny = y + dy[i];
if(nx>=0 && ny>=0 && nx<N && ny<M
&& !visited[nx][ny]){
visited[nx][ny] = true;
dfs(nx,ny,cnt+1,sum+map[nx][ny]);
visited[nx][ny] = false;
}
}
}
// ㅏ, ㅗ, ㅜ, ㅓ
public static void checkExceptionBlock(int x, int y) {
int sum = 0;
// ㅏ
if(x-1>=0 && x+1<N && y+1<M ) {
sum = map[x][y] + map[x-1][y] + map[x][y+1] + map[x+1][y];
answer = Math.max(answer, sum);
}
// ㅗ
if(x-1>=0 && y-1>=0 && y+1<M ) {
sum = map[x][y] + map[x][y-1] + map[x][y+1] + map[x-1][y];
answer = Math.max(answer, sum);
}
// ㅜ
if(y-1>=0 && y+1<M && x+1<N ) {
sum = map[x][y] + map[x][y-1] + map[x][y+1] + map[x+1][y];
answer = Math.max(answer, sum);
}
// ㅓ
if(x-1>=0 && x+1<N && y-1>=0 ) {
sum = map[x][y] + map[x-1][y] + map[x+1][y] + map[x][y-1];
answer = Math.max(answer, sum);
}
}
}