1+ package N14500 ;
2+
3+ import java .io .BufferedReader ;
4+ import java .io .BufferedWriter ;
5+ import java .io .IOException ;
6+ import java .io .InputStreamReader ;
7+ import java .io .OutputStreamWriter ;
8+ import java .util .StringTokenizer ;
9+
10+ public class N14500_seoyeon {
11+ // 지도 크기
12+ static int N , M ;
13+ // 게임 배열
14+ static int [][] game = new int [510 ][510 ];
15+ // 테트로미노 합 가질 변수, 가장 큰 합을 저장할 변수
16+ static int sum , maxSum ;
17+ // 방문 배열
18+ static boolean [][] visited = new boolean [510 ][510 ];
19+ // 방향 배열
20+ static int [] dr = {-1 , 1 , 0 , 0 };
21+ static int [] dc = {0 , 0 , -1 , 1 };
22+
23+ public static void main (String [] args ) throws IOException {
24+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
25+ BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (System .out ));
26+
27+ StringTokenizer st = new StringTokenizer (br .readLine ());
28+ N = Integer .parseInt (st .nextToken ());
29+ M = Integer .parseInt (st .nextToken ());
30+
31+ for (int i = 0 ; i < N ; i ++) {
32+ st = new StringTokenizer (br .readLine ());
33+ for (int j = 0 ; j < M ; j ++) {
34+ game [i ][j ] = Integer .parseInt (st .nextToken ());
35+ }
36+ }
37+
38+ for (int i = 0 ; i < N ; i ++) {
39+ for (int j = 0 ; j < M ; j ++) {
40+ // 방문 처리
41+ visited [i ][j ] = true ;
42+ dfs (i , j , 1 , game [i ][j ]);
43+ visited [i ][j ] = false ;
44+ checkT (i , j );
45+ }
46+ }
47+
48+ bw .write (Integer .toString (maxSum ));
49+ bw .flush ();
50+ bw .close ();
51+ br .close ();
52+
53+ }
54+
55+ // T 모양을 검사하는 함수
56+ private static void checkT (int r , int c ) {
57+ //ㅜ
58+ if (r >= 0 && r + 1 < N && c - 1 >= 0 && c + 1 < M ) {
59+ int sum = game [r ][c ] + game [r + 1 ][c - 1 ] + game [r + 1 ][c ] + game [r + 1 ][c + 1 ];
60+ if (maxSum < sum ) {
61+ maxSum = sum ;
62+ }
63+ }
64+ //ㅗ
65+ if (r + 1 < N && c - 1 >= 0 && c + 1 < M ) {
66+ sum = game [r ][c ] + game [r + 1 ][c ] + game [r ][c - 1 ] + game [r ][c + 1 ];
67+ if (sum > maxSum ) {
68+ maxSum = sum ;
69+ }
70+ }
71+
72+ // ㅓ
73+ if (r - 1 >= 0 && r + 1 < N && c - 1 >= 0 ) {
74+ sum = game [r ][c ] + game [r - 1 ][c ] + game [r + 1 ][c ] + game [r ][c - 1 ];
75+ if (sum > maxSum ) {
76+ maxSum = sum ;
77+ }
78+ }
79+
80+ // ㅏ
81+ if (r - 1 >= 0 && r + 1 < N && c + 1 < M ) {
82+ sum = game [r ][c ] + game [r - 1 ][c ] + game [r + 1 ][c ] + game [r ][c + 1 ];
83+ if (sum > maxSum ) {
84+ maxSum = sum ;
85+ }
86+ }
87+ }
88+
89+ private static void dfs (int r , int c , int depth , int currSum ) {
90+ // base condition
91+ if (depth == 4 ) {
92+ if (currSum > maxSum ) {
93+ maxSum = currSum ;
94+ }
95+ return ;
96+ }
97+
98+ // 탐색
99+ for (int i = 0 ; i < dr .length ; i ++) {
100+ int nr = r + dr [i ];
101+ int nc = c + dc [i ];
102+
103+ // 배열 범위 초과 확인, 방문 체크
104+ if (nr < 0 || nr >= N || nc < 0 || nc >= M || visited [nr ][nc ]) {
105+ continue ;
106+ }
107+
108+ visited [nr ][nc ] = true ;
109+ dfs (nr , nc , depth + 1 , currSum + game [nr ][nc ]);
110+ // 백트래킹
111+ visited [nr ][nc ] = false ;
112+
113+ }
114+ }
115+
116+ }
0 commit comments