|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.StringTokenizer; |
| 4 | + |
| 5 | +public class BJ_14500_테트로미노 { |
| 6 | + |
| 7 | + private static final int[] dx = { 0, 1, 0, -1 }; |
| 8 | + private static final int[] dy = { 1, 0, -1, 0 }; |
| 9 | + |
| 10 | + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 11 | + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 12 | + private static StringTokenizer st; |
| 13 | + |
| 14 | + private static int N, M, ans; |
| 15 | + private static int[][] matrix; |
| 16 | + private static boolean[][] visited; |
| 17 | + |
| 18 | + public static void main(String[] args) throws IOException { |
| 19 | + init(); |
| 20 | + sol(); |
| 21 | + } |
| 22 | + |
| 23 | + private static void init() throws IOException { |
| 24 | + st = new StringTokenizer(br.readLine()); |
| 25 | + N = Integer.parseInt(st.nextToken()); |
| 26 | + M = Integer.parseInt(st.nextToken()); |
| 27 | + ans = 0; |
| 28 | + |
| 29 | + matrix = new int[N][M]; |
| 30 | + for (int i = 0; i < N; i++) { |
| 31 | + st = new StringTokenizer(br.readLine()); |
| 32 | + for (int j = 0; j < M; j++) { |
| 33 | + matrix[i][j] = Integer.parseInt(st.nextToken()); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + visited = new boolean[N][M]; |
| 38 | + } |
| 39 | + |
| 40 | + private static void sol() throws IOException { |
| 41 | + for (int i = 0; i < N; i++) { |
| 42 | + for (int j = 0; j < M; j++) { |
| 43 | + visited[i][j] = true; |
| 44 | + dfs(1, i, j, matrix[i][j]); |
| 45 | + visited[i][j] = false; |
| 46 | + } |
| 47 | + } |
| 48 | + bw.write(ans + ""); |
| 49 | + bw.flush(); |
| 50 | + bw.close(); |
| 51 | + br.close(); |
| 52 | + } |
| 53 | + |
| 54 | + private static void dfs(int depth, int cx, int cy, int tmp) throws IOException { |
| 55 | + if (depth == 4) { |
| 56 | + ans = Math.max(ans, tmp); |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + if (tmp + (4 - depth) * 1000 <= ans) { |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + for (int d = 0; d < 4; d++) { |
| 65 | + int nx = cx + dx[d]; |
| 66 | + int ny = cy + dy[d]; |
| 67 | + |
| 68 | + if (nx < 0 || N <= nx || ny < 0 || M <= ny || visited[nx][ny]) continue; |
| 69 | + |
| 70 | + // 凸 처리 - 다음칸으로 이동하지 않음으로써 중간 칸에서 분기 가능하도록 함 |
| 71 | + if (depth == 2) { |
| 72 | + visited[nx][ny] = true; |
| 73 | + dfs(depth + 1, cx, cy, tmp + matrix[nx][ny]); |
| 74 | + visited[nx][ny] = false; |
| 75 | + } |
| 76 | + |
| 77 | + visited[nx][ny] = true; |
| 78 | + dfs(depth + 1, nx, ny, tmp + matrix[nx][ny]); |
| 79 | + visited[nx][ny] = false; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | +} |
| 84 | +``` |
0 commit comments