|
| 1 | +import java.io.*; |
| 2 | +import java.util.*; |
| 3 | + |
| 4 | +/* |
| 5 | + * 죽음의 비 |
| 6 | + */ |
| 7 | + |
| 8 | +public class DH_22944 { |
| 9 | + static int[] dr = {-1, 1, 0, 0}, dc = {0, 0, -1, 1}; |
| 10 | + static class Point { |
| 11 | + int k, r, c, d, p, u; |
| 12 | + public Point(int k, int r, int c, int d, int p, int u) { |
| 13 | + this.k = k; |
| 14 | + this.r = r; |
| 15 | + this.c = c; |
| 16 | + this.d = d; |
| 17 | + this.p = p; |
| 18 | + this.u = u; |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + static int N, H, D; |
| 23 | + static char[][] map; |
| 24 | + static boolean[][][] v; |
| 25 | + static Deque<Point> q; |
| 26 | + |
| 27 | + public static void main(String[] args) throws Exception { |
| 28 | + initInput(); |
| 29 | + System.out.println(solution()); |
| 30 | + } |
| 31 | + |
| 32 | + static int solution() { |
| 33 | + int result = -1; |
| 34 | + |
| 35 | + while(!q.isEmpty()) { |
| 36 | + Point current = q.poll(); |
| 37 | + |
| 38 | + if(map[current.r][current.c] == 'E') { |
| 39 | + result = current.d; |
| 40 | + break; |
| 41 | + } |
| 42 | + |
| 43 | + for(int d = 0; d < 4; d++) { |
| 44 | + int nr = current.r + dr[d]; |
| 45 | + int nc = current.c + dc[d]; |
| 46 | + int nk = current.k; |
| 47 | + int np = current.p; |
| 48 | + int nu = current.u; |
| 49 | + |
| 50 | + if(!check(nr, nc) || v[current.k][nr][nc]) continue; |
| 51 | + |
| 52 | + if(map[nr][nc] == '.') { |
| 53 | + if(nu > 0) nu -= 1; |
| 54 | + else np -= 1; |
| 55 | + } |
| 56 | + |
| 57 | + // 우산이라면 |
| 58 | + else if(map[nr][nc] == 'U') { |
| 59 | + map[nr][nc] = '.'; |
| 60 | + nu = D - 1; |
| 61 | + nk = current.k + 1; |
| 62 | + } |
| 63 | + |
| 64 | + if(np == 0) continue; |
| 65 | + |
| 66 | + v[current.k][nr][nc] = true; |
| 67 | + q.add(new Point(nk, nr, nc, current.d + 1, np, nu)); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + return result; |
| 72 | + } |
| 73 | + |
| 74 | + static boolean check(int r, int c) { |
| 75 | + return r >= 0 && r < N && c >= 0 && c < N; |
| 76 | + } |
| 77 | + |
| 78 | + static void initInput() throws Exception { |
| 79 | + System.setIn(new FileInputStream("./input/BOJ22944.txt")); |
| 80 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 81 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 82 | + |
| 83 | + N = Integer.parseInt(st.nextToken()); // 한 변의 길이 |
| 84 | + H = Integer.parseInt(st.nextToken()); // 현재 체력 |
| 85 | + D = Integer.parseInt(st.nextToken()); // 우산의 내구도 |
| 86 | + |
| 87 | + q = new ArrayDeque<Point>(); |
| 88 | + int uCnt = 0; |
| 89 | + |
| 90 | + int sr = 0, sc =0; |
| 91 | + |
| 92 | + map = new char[N][N]; |
| 93 | + |
| 94 | + for(int r = 0; r < N; r++) { |
| 95 | + String s = br.readLine(); |
| 96 | + map[r] = s.toCharArray(); |
| 97 | + |
| 98 | + for(int c = 0; c < N; c++) { |
| 99 | + if(map[r][c] == 'S') { |
| 100 | + sr = r; sc = c; |
| 101 | + map[r][c] = '.'; |
| 102 | + } |
| 103 | + if(map[r][c] == 'U') uCnt += 1; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + v = new boolean[uCnt + 1][N][N]; |
| 108 | + q.add(new Point(0, sr, sc, 0, H, 0)); |
| 109 | + v[0][sr][sc] = true; |
| 110 | + } |
| 111 | +} |
0 commit comments