-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path14500.java
More file actions
83 lines (64 loc) · 2.14 KB
/
14500.java
File metadata and controls
83 lines (64 loc) · 2.14 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
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
arr = new int[n][m];
visited = new boolean[n][m];
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
arr[i][j] = sc.nextInt();
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
dfs(i, j, 0, 0); // 이어지는 4개는 dfs로 탐색
checkT(i, j); // ㅗ ㅜ ㅓ ㅏ 계산
}
}
System.out.println(max);
}
static int[][] arr;
static boolean[][] visited;
static int max = 0;
static int[] dx = {1, 0, -1, 0};
static int[] dy = {0, 1, 0, -1};
static void dfs(int x, int y, int cnt, int sum) {
if(cnt == 4) {
max = Integer.max(max, sum);
return;
}
for(int i=0; i<4; i++) {
int xx = x+dx[i];
int yy = y+dy[i];
if(xx<0 || xx>=arr.length || yy<0 || yy>=arr[0].length)
continue;
visited[x][y] = true;
if(!visited[xx][yy])
dfs(xx, yy, cnt+1, sum+arr[x][y]);
visited[x][y] = false;
}
}
static void checkT(int x, int y) {
int var;
// 위 3개
if (x-1 >= 0 && y+1 < arr[0].length && y-1 >= 0) {
var = arr[x][y] + arr[x-1][y-1] + arr[x-1][y] + arr[x-1][y+1];
max = Integer.max(max, var);
}
// 좌측 3개
if (x-1 >= 0 && y-1 >= 0 && x+1 < arr.length) {
var = arr[x][y] + arr[x-1][y-1] + arr[x][y-1] + arr[x+1][y-1];
max = Integer.max(max, var);
}
// 우측 3개
if (x-1 >= 0 && y+1 < arr[0].length && x+1 < arr.length) {
var = arr[x][y] + arr[x-1][y+1] + arr[x][y+1] + arr[x+1][y+1];
max = Integer.max(max, var);
}
// 아래 3개
if (x+1 < arr.length && y+1 < arr[0].length && y-1 >= 0) {
var = arr[x][y] + arr[x+1][y-1] + arr[x+1][y] + arr[x+1][y+1];
max = Integer.max(max, var);
}
}
}