|
| 1 | +package bfs; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.InputStreamReader; |
| 6 | +import java.util.*; |
| 7 | + |
| 8 | +public class SB_22944 { |
| 9 | + static int N, H, D; |
| 10 | + static int K = 0; |
| 11 | + static char[][] board; |
| 12 | + static Node start; |
| 13 | + static int[] dx = {-1, 1, 0, 0}; |
| 14 | + static int[] dy = {0, 0, -1, 1}; |
| 15 | + |
| 16 | + private static int bfs() { |
| 17 | + Queue<Node> que = new ArrayDeque<>(); |
| 18 | + int[][][] visited = new int[N][N][K + 1]; |
| 19 | + for (int[][] box : visited){ |
| 20 | + for (int[] row : box){ |
| 21 | + Arrays.fill(row, -1); |
| 22 | + } |
| 23 | + } |
| 24 | + que.offer(new Node(start.x, start.y, start.h, start.u, start.cntU)); |
| 25 | + visited[start.x][start.y][0] = 0; |
| 26 | + |
| 27 | + while (!que.isEmpty()) { |
| 28 | + Node cur = que.poll(); |
| 29 | + if (cur.h==0) continue; // 체력 0이면 죽음 |
| 30 | + if (board[cur.x][cur.y]=='E') { // 도착하면 탈출 |
| 31 | + return visited[cur.x][cur.y][cur.cntU]; |
| 32 | + } |
| 33 | + |
| 34 | + for (int i = 0; i < 4; i++) { |
| 35 | + int nx = cur.x + dx[i]; |
| 36 | + int ny = cur.y + dy[i]; |
| 37 | + if (!isValid(nx, ny) || visited[nx][ny][cur.cntU]!=-1) continue; |
| 38 | + if (board[nx][ny] == 'U') { |
| 39 | + if (cur.cntU + 1 <= K && visited[nx][ny][cur.cntU + 1] == -1) { // 우산 안넘치게 조건걸기 |
| 40 | + que.add(new Node(nx, ny, cur.h, D, cur.cntU + 1)); |
| 41 | + visited[nx][ny][cur.cntU+1] = visited[cur.x][cur.y][cur.cntU] + 1; |
| 42 | + } |
| 43 | + } |
| 44 | + else { |
| 45 | + if (cur.u > 0) que.add(new Node(nx, ny, cur.h, cur.u - 1, cur.cntU)); |
| 46 | + else que.add(new Node(nx, ny, cur.h-1, cur.u, cur.cntU)); |
| 47 | + visited[nx][ny][cur.cntU] = visited[cur.x][cur.y][cur.cntU] + 1; |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + return -1; |
| 52 | + } |
| 53 | + |
| 54 | + private static boolean isValid(int x, int y) { |
| 55 | + return 0 <= x && x < N && 0 <= y && y < N; |
| 56 | + } |
| 57 | + public static void main(String[] args) throws IOException { |
| 58 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 59 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 60 | + |
| 61 | + N = Integer.parseInt(st.nextToken()); |
| 62 | + H = Integer.parseInt(st.nextToken()); |
| 63 | + D = Integer.parseInt(st.nextToken()); |
| 64 | + |
| 65 | + board = new char[N][N]; |
| 66 | + for (int i = 0; i < N; i++) { |
| 67 | + String line = br.readLine(); |
| 68 | + for (int j = 0; j < N; j++) { |
| 69 | + board[i][j] = line.charAt(j); |
| 70 | + if (board[i][j]=='S') start = new Node(i, j, H, 0, 0); |
| 71 | + if (board[i][j]=='U') K++; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + System.out.println(bfs()); |
| 76 | + } |
| 77 | + |
| 78 | + static class Node{ |
| 79 | + int x, y, h, u; |
| 80 | + int cntU; // 우산 주운 개수 |
| 81 | + |
| 82 | + public Node(int x, int y, int h, int u, int cntU) { |
| 83 | + this.x = x; |
| 84 | + this.y = y; |
| 85 | + this.h = h; |
| 86 | + this.u = u; |
| 87 | + this.cntU = cntU; |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments