/
boj14500.java
74 lines (65 loc) · 2.1 KB
/
boj14500.java
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
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
private static int n, m, ans=0;
private static int dy[] = {0, -1, 0, 1};
private static int dx[] = {1, 0, -1, 0};
private static int map[][];
private static boolean visit[][];
public static void main(String[] args) throws Exception {
init();
for (int y=0; y<n; y++) {
for (int x=0; x<m; x++) {
dfs(y, x, 0, 0);
checkException(y, x);
}
}
System.out.println(ans);
}
private static void dfs(int y, int x, int sum, int count) {
if (count == 4) {
ans = Math.max(ans, sum);
return;
}
for (int i=0; i<4; i++) {
int ny = y+dy[i];
int nx = x+dx[i];
if (ny<0 || nx<0 || ny>=n || nx>=m || visit[ny][nx]) continue;
visit[ny][nx] = true;
dfs(ny, nx, sum+map[ny][nx], count+1);
visit[ny][nx] = false;
}
}
private static void checkException(int y, int x) {
int sum = map[y][x];
int count = 1;
int min = 987654321;
for (int i=0; i<4; i++) {
int ny = y+dy[i];
int nx = x+dx[i];
if (ny<0 || nx<0 || ny>=n || nx>=m) continue;
sum += map[ny][nx];
min = Math.min(min, map[ny][nx]);
count++;
}
if (count == 5) {
sum -= min;
}
ans = Math.max(ans, sum);
}
private static void init() throws Exception {
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];
visit = new boolean[n][m];
for (int y = 0; y < n; y++) {
st = new StringTokenizer(br.readLine());
for (int x = 0; x < m; x++) {
map[y][x] = Integer.parseInt(st.nextToken());
}
}
}
}