1
+ import java .io .*;
2
+
3
+ public class YJ_테트리스_블럭_안의_합_최대화_하기 {
4
+ static int n = 0 ;
5
+ static int m = 0 ;
6
+
7
+ static int [][] block ;
8
+ static boolean [][] visited ;
9
+ static int maxSum = 0 ;
10
+ public static void main (String [] args ) throws IOException {
11
+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
12
+ String [] line = br .readLine ().split ("\\ s" );
13
+ n = Integer .parseInt (line [0 ]);
14
+ m = Integer .parseInt (line [1 ]);
15
+
16
+ block = new int [n ][m ];
17
+ for (int i =0 ; i <n ; i ++){
18
+ String [] t = br .readLine ().split ("\\ s" );
19
+ for (int j =0 ; j <m ; j ++){
20
+ block [i ][j ] = Integer .parseInt (t [j ]);
21
+ }
22
+ }
23
+
24
+ visited = new boolean [n ][m ];
25
+ //블럭의 한칸마다 전체를 DFS 로 탐색 * 전체블럭 수
26
+ for (int i =0 ; i <n ; i ++){
27
+ for (int j =0 ; j <m ; j ++){
28
+ visited [i ][j ] = true ;
29
+ dfs (1 ,i ,j ,block [i ][j ]);
30
+ visited [i ][j ] = false ;
31
+ //ᅡᅩᅮᅥ 도형
32
+
33
+ }
34
+ }
35
+
36
+ System .out .println (maxSum );
37
+ }
38
+
39
+ //좌우상하
40
+ static int [] dx = {0 ,0 ,-1 ,1 };
41
+ static int [] dy = {-1 ,1 ,0 ,0 };
42
+ static void dfs (int depth , int x , int y , int sum ){
43
+ if (depth == 4 ){
44
+ maxSum = Math .max (maxSum , sum );
45
+ return ;
46
+ }
47
+
48
+ //상하좌우 모두 탐색
49
+ for (int i =0 ; i <4 ; i ++){
50
+ int nx = x + dx [i ];
51
+ int ny = y + dy [i ];
52
+
53
+ if (stop (nx ,ny )){
54
+ continue ;
55
+ }
56
+ visited [nx ][ny ] = true ;
57
+ if (depth == 2 ){
58
+ dfs (depth +1 , x ,y ,sum +block [nx ][ny ]);
59
+ }
60
+ dfs (depth +1 , nx , ny , sum + block [nx ][ny ]);
61
+ visited [nx ][ny ] = false ;
62
+ }
63
+
64
+ }
65
+
66
+ private static boolean stop (int nx , int ny ){
67
+ return nx < 0 || nx >= n || ny < 0 || ny >= m || visited [nx ][ny ];
68
+ }
69
+ }
0 commit comments