|
| 1 | +import java.io.BufferedReader; |
| 2 | +import java.io.IOException; |
| 3 | +import java.io.InputStreamReader; |
| 4 | +import java.util.ArrayDeque; |
| 5 | +import java.util.Arrays; |
| 6 | +import java.util.StringTokenizer; |
| 7 | + |
| 8 | +public class Main { |
| 9 | + |
| 10 | + // Test case의 개수가 주어지지 않음! |
| 11 | + static int N, M; |
| 12 | + static int[] dx = {-1, 0, 1, 0}; |
| 13 | + static int[] dy = {0, -1, 0, 1}; |
| 14 | + static char[][] map; |
| 15 | + static int ans; |
| 16 | + static int wholeCnt; |
| 17 | + static boolean[][] visitMap; |
| 18 | + |
| 19 | + public static void main(String[] args) throws IOException { |
| 20 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 21 | + StringTokenizer st; |
| 22 | + StringBuilder sb = new StringBuilder(); |
| 23 | + String input = ""; |
| 24 | + int tc = 1; |
| 25 | + |
| 26 | + while((input = br.readLine()) != null && !input.isEmpty()){ |
| 27 | + ans = Integer.MAX_VALUE; |
| 28 | + st = new StringTokenizer(input, " "); |
| 29 | + N = Integer.parseInt(st.nextToken()); |
| 30 | + M = Integer.parseInt(st.nextToken()); |
| 31 | + |
| 32 | + wholeCnt = 0; |
| 33 | + |
| 34 | + map = new char[N][M]; |
| 35 | + |
| 36 | + visitMap = new boolean[N][M]; |
| 37 | + |
| 38 | + for(int i = 0; i < N; i++){ |
| 39 | + String s = br.readLine(); |
| 40 | + for(int j = 0; j < M; j++){ |
| 41 | + map[i][j] = s.charAt(j); |
| 42 | + if(s.charAt(j) == '.'){ |
| 43 | + wholeCnt++; |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + for(int i = 0; i < N; i++){ |
| 48 | + for(int j = 0; j < M; j++){ |
| 49 | + for(int k = 0; k < 4; k++){ |
| 50 | + if(map[i][j] == '.' && isPossible(i + dx[k], j + dy[k])){ |
| 51 | + findPath(i, j, k, 1, 1); |
| 52 | + } else if(map[i][j] == '.' && !isPossible(i + dx[k], j + dy[k])){ |
| 53 | + findPath(i, j, k, 1, 0); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + if(ans == Integer.MAX_VALUE) |
| 59 | + ans = -1; |
| 60 | + System.out.println("Case "+tc++ +": " + ans); |
| 61 | + } |
| 62 | + System.out.println(sb); |
| 63 | + } |
| 64 | + |
| 65 | + public static void findPath(int x, int y, int dir, int cnt, int step){ |
| 66 | + if(cnt == wholeCnt) |
| 67 | + ans = Math.min(ans, step); |
| 68 | + visitMap[x][y] = true; |
| 69 | + int nx = x + dx[dir]; |
| 70 | + int ny = y + dy[dir]; |
| 71 | + |
| 72 | + // 갈 수 있는 경우 |
| 73 | + if(isPossible(nx, ny)){ |
| 74 | + findPath(nx, ny, dir, cnt+1, step); |
| 75 | + // 갈 수 없는 경우 |
| 76 | + } else { |
| 77 | + for(int i = 0; i < 4; i++){ |
| 78 | + if(dir == i) |
| 79 | + continue; |
| 80 | + nx = x + dx[i]; |
| 81 | + ny = y + dy[i]; |
| 82 | + if(isPossible(nx, ny)){ |
| 83 | + findPath(nx, ny, i, cnt+1, step+1); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + visitMap[x][y] = false; |
| 88 | + } |
| 89 | + |
| 90 | + public static boolean isPossible(int x, int y){ |
| 91 | + return !(x < 0 || y < 0 || x >= N || y >= M || visitMap[x][y] || map[x][y] == '*'); |
| 92 | + } |
| 93 | +} |
0 commit comments