|
| 1 | +```java |
| 2 | +import java.util.*; |
| 3 | +import java.io.*; |
| 4 | +class Node implements Comparable<Node>{ |
| 5 | + int i; |
| 6 | + int j; |
| 7 | + int dist; |
| 8 | + Node(int i, int j, int dist){ |
| 9 | + this.i = i; |
| 10 | + this.j = j; |
| 11 | + this.dist = dist; |
| 12 | + } |
| 13 | + @Override |
| 14 | + public int compareTo(Node o){ |
| 15 | + return Integer.compare(this.dist,o.dist); |
| 16 | + } |
| 17 | +} |
| 18 | +public class Main { |
| 19 | + static int M; |
| 20 | + static int N; |
| 21 | + static int[][] map; |
| 22 | + static List<Node> players = new ArrayList<>(); //플레이어 위치 key:i value:j |
| 23 | + static Map<Integer,Integer> damage = new HashMap<>(); //플레이어의 데미지 key:플레이어 숫자 value: 데미지 |
| 24 | + static List<Node> arriveTime; //도착 시간 |
| 25 | + static int bossHealth; |
| 26 | + public static void main(String[] args) throws Exception { |
| 27 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 28 | + BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 29 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 30 | + M = Integer.parseInt(st.nextToken()); |
| 31 | + N = Integer.parseInt(st.nextToken()); |
| 32 | + int P = Integer.parseInt(st.nextToken()); |
| 33 | + map = new int[M][N]; |
| 34 | + arriveTime = new ArrayList<>(P); |
| 35 | + for(int i=0;i<M;i++){ |
| 36 | + char[] str = br.readLine().toCharArray(); |
| 37 | + for(int j=0;j<N;j++){ |
| 38 | + if(str[j] == '.'){map[i][j] = 0;} |
| 39 | + else if(str[j] == 'X'){map[i][j] = -1;} |
| 40 | + else if(str[j] == 'B'){map[i][j] = 27;} |
| 41 | + else{ |
| 42 | + map[i][j] = str[j]-'a'+1; |
| 43 | + players.add(new Node(i,j,0)); |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + for(int i=0;i<P;i++){ |
| 48 | + st = new StringTokenizer(br.readLine()); |
| 49 | + int player = st.nextToken().charAt(0)-'a'+1; |
| 50 | + damage.put(player,Integer.parseInt(st.nextToken())); |
| 51 | + } |
| 52 | + bossHealth = Integer.parseInt(br.readLine()); |
| 53 | + |
| 54 | + //각 플레이어마다 BFS |
| 55 | + for(Node n:players){ |
| 56 | + BFS(n.i,n.j); |
| 57 | + } |
| 58 | + Collections.sort(arriveTime); |
| 59 | + |
| 60 | + int prevTime = 0; |
| 61 | + int curTime = 0; |
| 62 | + int adder = 0; |
| 63 | + int hitPlayers = 0; |
| 64 | + for(int i=0;i<arriveTime.size();i++){ |
| 65 | + Node n = arriveTime.get(i); |
| 66 | + if(i==0){ //처음 도착했을때 |
| 67 | + curTime = n.dist; |
| 68 | + prevTime = n.dist; |
| 69 | + adder = damage.get(map[n.i][n.j]); |
| 70 | + hitPlayers++; |
| 71 | + continue; |
| 72 | + } |
| 73 | + if(prevTime == n.dist)//이전 플레이어와 동일한 시간대에 도착했을때 |
| 74 | + { |
| 75 | + adder += damage.get(map[n.i][n.j]); |
| 76 | + hitPlayers++; |
| 77 | + continue; |
| 78 | + } |
| 79 | + else{ |
| 80 | + prevTime = curTime; |
| 81 | + curTime = n.dist; |
| 82 | + bossHealth-=adder*(curTime-prevTime); |
| 83 | + adder += damage.get(map[n.i][n.j]); |
| 84 | + |
| 85 | + if(bossHealth>0){ |
| 86 | + hitPlayers++; |
| 87 | + }else{ |
| 88 | + break; |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + bw.write(hitPlayers+""); |
| 93 | + bw.flush(); |
| 94 | + } |
| 95 | + static void BFS(int i,int j){ |
| 96 | + Queue<Node> q = new LinkedList<>(); |
| 97 | + boolean[][] visited = new boolean[M][N]; |
| 98 | + q.offer(new Node(i,j,0)); |
| 99 | + visited[i][j] = true; |
| 100 | + |
| 101 | + int[] di = {-1,1,0,0}; |
| 102 | + int[] dj = {0,0,-1,1}; |
| 103 | + while(!q.isEmpty()){ |
| 104 | + Node cur = q.poll(); |
| 105 | + if(map[cur.i][cur.j] == 27){ |
| 106 | + arriveTime.add(new Node(i,j,cur.dist)); |
| 107 | + break; |
| 108 | + } |
| 109 | + for(int k=0;k<4;k++){ |
| 110 | + int newi = cur.i + di[k]; |
| 111 | + int newj = cur.j + dj[k]; |
| 112 | + if(newi>=0 && newi<M && newj>=0 && newj<N && !visited[newi][newj] && map[newi][newj] != -1){ |
| 113 | + q.offer(new Node(newi,newj,cur.dist+1)); |
| 114 | + visited[newi][newj] = true; |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +``` |
0 commit comments