|
| 1 | +```java |
| 2 | +import java.io.BufferedReader; |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.InputStreamReader; |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.Arrays; |
| 7 | +import java.util.Collections; |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.HashSet; |
| 10 | +import java.util.LinkedList; |
| 11 | +import java.util.PriorityQueue; |
| 12 | +import java.util.Queue; |
| 13 | +import java.util.StringTokenizer; |
| 14 | + |
| 15 | +public class Main { |
| 16 | + |
| 17 | + // 각 s,k에서 모두 bfs를 진행하여 거리표 만들고. MST. 끝 |
| 18 | + |
| 19 | + static char WALL = '1'; |
| 20 | + static char EMPTY = '0'; |
| 21 | + static char START = 'S'; |
| 22 | + static char KEY = 'K'; |
| 23 | + static int[] dy = {-1,0,1,0}; |
| 24 | + static int[] dx = {0,1,0,-1}; |
| 25 | + |
| 26 | + static class Box{ |
| 27 | + int y,x,num; |
| 28 | + char type; |
| 29 | + |
| 30 | + public Box (int y, int x, char c, int idx) { |
| 31 | + this.y = y; |
| 32 | + this.x = x; |
| 33 | + this.num = idx; |
| 34 | + this.type = c; |
| 35 | + } |
| 36 | + |
| 37 | + } |
| 38 | + |
| 39 | + static class Edge implements Comparable<Edge>{ |
| 40 | + Box from; |
| 41 | + Box to; |
| 42 | + int dis; |
| 43 | + |
| 44 | + public Edge(Box from, Box to, int dis) { |
| 45 | + this.from = from; |
| 46 | + this.to = to; |
| 47 | + this.dis = dis; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public int compareTo(Edge e) { |
| 52 | + return Integer.compare(this.dis, e.dis); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + |
| 57 | + static Box[][] arr; |
| 58 | + static Box[] point; |
| 59 | + static int[] parent; |
| 60 | + static int size,keyCnt,boxCnt,ans; |
| 61 | + |
| 62 | + static PriorityQueue<Edge> pq = new PriorityQueue<>(); |
| 63 | + |
| 64 | + |
| 65 | + public static void main(String[] args) throws IOException { |
| 66 | + init(); |
| 67 | + process(); |
| 68 | + print(); |
| 69 | + |
| 70 | + |
| 71 | + } |
| 72 | + |
| 73 | + private static void init() throws IOException{ |
| 74 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 75 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 76 | + size = Integer.parseInt(st.nextToken()); |
| 77 | + keyCnt = Integer.parseInt(st.nextToken()); |
| 78 | + |
| 79 | + ans = 0; |
| 80 | + arr = new Box[size][size]; |
| 81 | + point = new Box[size*size]; |
| 82 | + parent = new int[size*size]; |
| 83 | + boxCnt = 0; |
| 84 | + |
| 85 | + for (int i = 0; i < size; i++) { |
| 86 | + char[] cArr= br.readLine().toCharArray(); |
| 87 | + for (int j = 0; j < size; j++) { |
| 88 | + arr[i][j] = new Box(i,j,cArr[j],boxCnt); |
| 89 | + if (arr[i][j].type == START || arr[i][j].type == KEY) { |
| 90 | + point[boxCnt++] = arr[i][j]; |
| 91 | + } |
| 92 | + |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + for (int i = 0; i < size*size; i++) { |
| 97 | + parent[i] = i; |
| 98 | + } |
| 99 | + |
| 100 | + |
| 101 | + } |
| 102 | + |
| 103 | + private static void process() throws IOException { |
| 104 | + getEdge(); |
| 105 | + getDis(); |
| 106 | + check(); |
| 107 | + |
| 108 | + } |
| 109 | + |
| 110 | + private static void check() { |
| 111 | + for (int i= 0; i < boxCnt; i++) { |
| 112 | + if ( find(i) != 0) { |
| 113 | + ans = -1; |
| 114 | + return; |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + private static void getDis() { |
| 120 | + |
| 121 | + while(!pq.isEmpty()) { |
| 122 | + Edge e = pq.poll(); |
| 123 | + union(e.from.num, e.to.num, e.dis); |
| 124 | + |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + private static int find(int idx) { |
| 129 | + if (parent[idx] == idx) return idx; |
| 130 | + |
| 131 | + return parent[idx] = find(parent[idx]); |
| 132 | + } |
| 133 | + |
| 134 | + private static void union(int idx1, int idx2,int dis) { |
| 135 | + int root1 = find(idx1); |
| 136 | + int root2 = find(idx2); |
| 137 | + |
| 138 | + if (root1 == root2) return; |
| 139 | + |
| 140 | + if (root1 < root2) { |
| 141 | + parent[root2] = root1; |
| 142 | + } else { |
| 143 | + parent[root1] = root2; |
| 144 | + } |
| 145 | + ans += dis; |
| 146 | + |
| 147 | + return; |
| 148 | + } |
| 149 | + |
| 150 | + |
| 151 | + private static void getEdge() { |
| 152 | + |
| 153 | + for (int i = 0; i < boxCnt; i++) { |
| 154 | + boolean[][] visited = new boolean[size][size]; |
| 155 | + |
| 156 | + Box box = point[i]; |
| 157 | + |
| 158 | + Queue<Box> q = new LinkedList<>(); |
| 159 | + |
| 160 | + q.add(box); |
| 161 | + visited[box.y][box.x] = true; |
| 162 | + |
| 163 | + |
| 164 | + int dis = 0; |
| 165 | + |
| 166 | + while(!q.isEmpty()) { |
| 167 | + dis++; |
| 168 | + int cnt = q.size(); |
| 169 | + for (int j = 0; j < cnt; j++) { |
| 170 | + Box b = q.poll(); |
| 171 | + int y = b.y; |
| 172 | + int x = b.x; |
| 173 | + |
| 174 | + |
| 175 | + |
| 176 | + for (int k = 0; k < 4; k++) { |
| 177 | + int ny = y + dy[k]; |
| 178 | + int nx = x + dx[k]; |
| 179 | + |
| 180 | + if ( ny < 0 || nx < 0 || ny >= size || nx >= size) continue; |
| 181 | + if ( visited[ny][nx] ) continue; |
| 182 | + if (arr[ny][nx].type == WALL) continue; |
| 183 | + |
| 184 | + visited[ny][nx] = true; |
| 185 | + |
| 186 | + if (arr[ny][nx].type != EMPTY) { |
| 187 | + Edge e = new Edge(box,arr[ny][nx],dis); |
| 188 | + pq.add(e); |
| 189 | + } |
| 190 | + |
| 191 | + q.add(arr[ny][nx]); |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + |
| 196 | + |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + private static void print() { |
| 202 | + System.out.println(ans); |
| 203 | + } |
| 204 | + |
| 205 | +} |
| 206 | +``` |
0 commit comments