1
+ import java .io .*;
2
+ import java .util .*;
3
+
4
+ public class SB_테트리스_블럭_안의_합_최대화_하기 {
5
+ static int N ,M ;
6
+ static int [][] board ;
7
+ static boolean [][] visited ;
8
+ static int mx = 0 ;
9
+ static int [] dx = {-1 , 1 , 0 , 0 };
10
+ static int [] dy = {0 , 0 , -1 , 1 };
11
+
12
+ private static void dfs (int x , int y , int depth , int total ) {
13
+ // if (mx > mx*(4-depth)+total) return;
14
+ if (depth ==4 ){
15
+ mx = Math .max (mx , total );
16
+ return ;
17
+ }
18
+
19
+ for (int i = 0 ; i < 4 ; i ++) {
20
+ int nx = x +dx [i ];
21
+ int ny = y +dy [i ];
22
+ if (!isValid (nx , ny ) || visited [nx ][ny ]) continue ;
23
+ if (depth == 2 ) {
24
+ visited [nx ][ny ] = true ;
25
+ dfs (x , y , depth +1 , total +board [nx ][ny ]); // ᅮ 가 갈라지는 중점에서 양옆 탐색
26
+ visited [nx ][ny ] = false ;
27
+ }
28
+ visited [nx ][ny ] = true ;
29
+ dfs (nx , ny , depth +1 , total +board [nx ][ny ]);
30
+ visited [nx ][ny ] = false ;
31
+ }
32
+ }
33
+
34
+ private static boolean isValid (int x , int y ) {
35
+ return 0 <=x && x <N && 0 <=y && y <M ;
36
+ }
37
+ public static void main (String [] args ) throws IOException {
38
+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
39
+ StringTokenizer st = new StringTokenizer (br .readLine ());
40
+
41
+ N = Integer .parseInt (st .nextToken ());
42
+ M = Integer .parseInt (st .nextToken ());
43
+
44
+ board = new int [N ][M ];
45
+ visited = new boolean [N ][M ];
46
+ for (int i =0 ; i <N ; i ++){
47
+ st = new StringTokenizer (br .readLine ());
48
+ for (int j = 0 ; j < M ; j ++) {
49
+ board [i ][j ] = Integer .parseInt (st .nextToken ());
50
+ }
51
+ }
52
+
53
+ for (int i = 0 ; i < N ; i ++) {
54
+ for (int j = 0 ; j < M ; j ++) {
55
+ visited [i ][j ] = true ;
56
+ dfs (i , j , 1 , board [i ][j ]);
57
+ visited [i ][j ] = false ;
58
+ }
59
+ }
60
+ System .out .println (mx );
61
+ }
62
+ }
0 commit comments