|
| 1 | +import java.io.BufferedReader; |
| 2 | +import java.io.IOException; |
| 3 | +import java.io.InputStreamReader; |
| 4 | +import java.util.StringTokenizer; |
| 5 | + |
| 6 | +public class JM_9944 { |
| 7 | + static int N; |
| 8 | + static int M; |
| 9 | + static char[][] map; |
| 10 | + private static final int[][] DIR = {{0, 1}, {-1, 0}, {1, 0}, {0, -1}}; |
| 11 | + |
| 12 | + private static boolean isRange(int y, int x) { |
| 13 | + return 0 <= y && y < N && 0 <= x && x < M; |
| 14 | + } |
| 15 | + |
| 16 | + private static int moveBack(int y, int x, int ny, int nx, int d) { |
| 17 | + int moveCnt = 0; |
| 18 | + while (!(y == ny && x == nx)) { |
| 19 | + map[ny][nx] = '.'; |
| 20 | + moveCnt += 1; |
| 21 | + ny -= DIR[d][0]; |
| 22 | + nx -= DIR[d][1]; |
| 23 | + } |
| 24 | + return moveCnt; |
| 25 | + } |
| 26 | + |
| 27 | + private static int[] move(int y, int x, int d) { |
| 28 | + int moveCnt = 0; |
| 29 | + while (true) { |
| 30 | + int ny = y + DIR[d][0]; |
| 31 | + int nx = x + DIR[d][1]; |
| 32 | + if(!isRange(ny, nx) || map[ny][nx] != '.') break; |
| 33 | + map[ny][nx] = '#'; |
| 34 | + moveCnt += 1; |
| 35 | + y = ny; |
| 36 | + x = nx; |
| 37 | + } |
| 38 | + return new int[]{y, x, moveCnt}; |
| 39 | + } |
| 40 | + |
| 41 | + private static int dfs(int y, int x, int blankCnt) { |
| 42 | + if(blankCnt == 0) { |
| 43 | + return 0; |
| 44 | + } |
| 45 | + |
| 46 | + int res = -1; |
| 47 | + for (int d = 0; d < 4; d++) { |
| 48 | + int[] next = move(y, x, d); |
| 49 | + int ny = next[0]; |
| 50 | + int nx = next[1]; |
| 51 | + blankCnt -= next[2]; |
| 52 | + |
| 53 | + if(!(y == ny && x == nx)) { |
| 54 | + int tmp = dfs(ny, nx, blankCnt); |
| 55 | + if(tmp != -1 && (res == -1 || res > tmp + 1)) { |
| 56 | + res = tmp + 1; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + blankCnt += moveBack(y, x, ny, nx, d); |
| 61 | + } |
| 62 | + |
| 63 | + return res; |
| 64 | + } |
| 65 | + |
| 66 | + private static int solve(int blankCnt) { |
| 67 | + int res = -1; |
| 68 | + |
| 69 | + for (int i = 0; i < N; i++) { |
| 70 | + for (int j = 0; j < M; j++) { |
| 71 | + if(map[i][j] == '*') continue; |
| 72 | + map[i][j] = '#'; |
| 73 | + int tmp = dfs(i, j, blankCnt - 1); |
| 74 | + if(tmp != -1 && (res == -1 || res > tmp)) { |
| 75 | + res = tmp; |
| 76 | + } |
| 77 | + map[i][j] = '.'; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + return res; |
| 82 | + } |
| 83 | + |
| 84 | + public static void main(String[] args) throws IOException { |
| 85 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 86 | + StringTokenizer st; |
| 87 | + String line; |
| 88 | + |
| 89 | + int T = 1; |
| 90 | + StringBuilder sb = new StringBuilder(); |
| 91 | + while((line = br.readLine()) != null && line.length() > 0) { |
| 92 | + st = new StringTokenizer(line); |
| 93 | + N = Integer.parseInt(st.nextToken()); |
| 94 | + M = Integer.parseInt(st.nextToken()); |
| 95 | + |
| 96 | + int blankCnt = 0; |
| 97 | + map = new char[N][M]; |
| 98 | + for (int i = 0; i < N; i++) { |
| 99 | + map[i] = br.readLine().toCharArray(); |
| 100 | + for (int j = 0; j < M; j++) { |
| 101 | + if(map[i][j] == '.') blankCnt++; |
| 102 | + } |
| 103 | + } |
| 104 | + sb.append("Case ").append(T).append(": ").append(solve(blankCnt)).append("\n"); |
| 105 | + T++; |
| 106 | + } |
| 107 | + System.out.println(sb); |
| 108 | + } |
| 109 | +} |
0 commit comments