|
| 1 | +import java.util.*; |
| 2 | +import java.io.*; |
| 3 | + |
| 4 | +//BFS |
| 5 | +public class YJ_22944 { |
| 6 | + static class Pos { |
| 7 | + int x; |
| 8 | + int y; |
| 9 | + int hp; |
| 10 | + int distance; |
| 11 | + int umbrella; |
| 12 | + int umbrellaCount; |
| 13 | + |
| 14 | + public Pos(int x, int y, int hp, int distance, int umbrella, int umbrellaCount) { |
| 15 | + this.x = x; |
| 16 | + this.y = y; |
| 17 | + this.hp = hp; |
| 18 | + this.distance = distance; |
| 19 | + this.umbrella = umbrella; |
| 20 | + this.umbrellaCount = umbrellaCount; |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + static final char S = 'S'; |
| 25 | + static final char U = 'U'; |
| 26 | + static final char E = 'E'; |
| 27 | + |
| 28 | + static int[] current; |
| 29 | + static char[][] space; |
| 30 | + |
| 31 | + public static void main(String[] args) throws IOException { |
| 32 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 33 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 34 | + int n = Integer.parseInt(st.nextToken()); |
| 35 | + int h = Integer.parseInt(st.nextToken()); |
| 36 | + int d = Integer.parseInt(st.nextToken()); |
| 37 | + space = new char[n][n]; |
| 38 | + |
| 39 | + for(int i=0; i<n; i++){ |
| 40 | + String data = br.readLine(); |
| 41 | + for(int j=0; j<n; j++){ |
| 42 | + if(S == data.charAt(j)){ |
| 43 | + current = new int[] {i,j}; |
| 44 | + } |
| 45 | + space[i][j] = data.charAt(j); |
| 46 | + } |
| 47 | + } |
| 48 | + visited = new boolean[n][n][11]; |
| 49 | + |
| 50 | + System.out.println(bfs(h,d)); |
| 51 | + } |
| 52 | + |
| 53 | + static boolean[][][] visited; |
| 54 | + static int[] nx = {1,0,-1,0}; |
| 55 | + static int[] ny = {0,1,0,-1}; |
| 56 | + static int bfs(int h, int d) { |
| 57 | + Deque<Pos> deque = new ArrayDeque<>(); |
| 58 | + int uc = 0; |
| 59 | + deque.offer(new Pos(current[0],current[1],h,0,0,0)); |
| 60 | + visited[current[0]][current[1]][0] = true; |
| 61 | + |
| 62 | + while(!deque.isEmpty()){ |
| 63 | + Pos pos = deque.poll(); |
| 64 | + |
| 65 | + for(int i=0; i<4; i++){ |
| 66 | + int x = pos.x + nx[i]; |
| 67 | + int y = pos.y + ny[i]; |
| 68 | + if(stop(x,y,pos.umbrellaCount)){ |
| 69 | + continue; |
| 70 | + } |
| 71 | + |
| 72 | + int hp = pos.hp; |
| 73 | + int umbrella = pos.umbrella; |
| 74 | + char current = space[x][y]; |
| 75 | + |
| 76 | + if(current == E){ |
| 77 | + return ++pos.distance; |
| 78 | + } |
| 79 | + //우산획득 시 내구도 감소+우산갯수 카운팅, 아니면 죽음비 맞기 |
| 80 | + if(current == U){ |
| 81 | + umbrella = d-1; |
| 82 | + pos.umbrellaCount = ++uc; |
| 83 | + space[x][y] = '.'; |
| 84 | + }else{ |
| 85 | + if(umbrella > 0){ |
| 86 | + umbrella--; |
| 87 | + }else{ |
| 88 | + hp--; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + if(hp == 0){ |
| 93 | + continue; |
| 94 | + } |
| 95 | + visited[x][y][pos.umbrellaCount] = true; |
| 96 | + deque.offer(new Pos(x,y,hp,pos.distance+1,umbrella,pos.umbrellaCount)); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + return -1; |
| 101 | + } |
| 102 | + |
| 103 | + private static boolean stop(int x, int y, int count){ |
| 104 | + return x < 0 || y < 0 || x >= space.length || y >= space.length || visited[x][y][count]; |
| 105 | + } |
| 106 | +} |
0 commit comments