|
| 1 | +```java |
| 2 | +import java.awt.Point; |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.BufferedWriter; |
| 5 | +import java.io.IOException; |
| 6 | +import java.io.InputStreamReader; |
| 7 | +import java.io.OutputStreamWriter; |
| 8 | +import java.util.ArrayDeque; |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.Arrays; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Queue; |
| 13 | +import java.util.StringTokenizer; |
| 14 | + |
| 15 | +public class BJ_17142_연구소_3 { |
| 16 | + |
| 17 | + private static final int[] dx = {0, 1, 0, -1}; |
| 18 | + private static final int[] dy = {1, 0, -1, 0}; |
| 19 | + |
| 20 | + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 21 | + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 22 | + private static StringTokenizer st; |
| 23 | + |
| 24 | + private static int N; |
| 25 | + private static int M; |
| 26 | + private static int ans; |
| 27 | + private static int virusCnt; |
| 28 | + |
| 29 | + private static int[][] matrix; |
| 30 | + private static int[] activeVirus; |
| 31 | + private static List<Point> virus; |
| 32 | + private static Queue<Node> q; |
| 33 | + private static boolean[][] visited; |
| 34 | + |
| 35 | + private static class Node { |
| 36 | + int x; |
| 37 | + int y; |
| 38 | + int cnt; |
| 39 | + |
| 40 | + public Node(int x, int y, int cnt) { |
| 41 | + this.x = x; |
| 42 | + this.y = y; |
| 43 | + this.cnt = cnt; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + public static void main(String[] args) throws IOException { |
| 48 | + init(); |
| 49 | + sol(); |
| 50 | + } |
| 51 | + |
| 52 | + private static void init() throws IOException { |
| 53 | + st = new StringTokenizer(br.readLine()); |
| 54 | + N = Integer.parseInt(st.nextToken()); |
| 55 | + M = Integer.parseInt(st.nextToken()); |
| 56 | + ans = Integer.MAX_VALUE; |
| 57 | + |
| 58 | + matrix = new int[N][N]; |
| 59 | + virus = new ArrayList<>(); |
| 60 | + int v = 0; |
| 61 | + for (int i = 0; i < N; i++) { |
| 62 | + st = new StringTokenizer(br.readLine()); |
| 63 | + for (int j = 0; j < N; j++) { |
| 64 | + matrix[i][j] = Integer.parseInt(st.nextToken()); |
| 65 | + |
| 66 | + if (matrix[i][j] == 2) { |
| 67 | + virus.add(new Point(i, j)); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + virusCnt = virus.size(); |
| 72 | + activeVirus = new int[M]; |
| 73 | + |
| 74 | + q = new ArrayDeque<>(); |
| 75 | + visited = new boolean[N][N]; |
| 76 | + } |
| 77 | + |
| 78 | + private static void sol() throws IOException { |
| 79 | + perm(0, 0); |
| 80 | + |
| 81 | + bw.write(ans == Integer.MAX_VALUE ? "-1" : ans + ""); |
| 82 | + bw.flush(); |
| 83 | + bw.close(); |
| 84 | + br.close(); |
| 85 | + } |
| 86 | + |
| 87 | + private static void perm(int depth, int n) { |
| 88 | + if (depth == M) { |
| 89 | + bfs(); |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + for (int i = n; i < virusCnt; i++) { |
| 94 | + activeVirus[depth] = i; |
| 95 | + perm(depth + 1, i + 1); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + private static void bfs() { |
| 100 | + // clear queue and visited |
| 101 | + clear(); |
| 102 | + |
| 103 | + // put active virus |
| 104 | + for (int i = 0; i < M; i++) { |
| 105 | + Point curVirus = virus.get(activeVirus[i]); |
| 106 | + q.offer(new Node(curVirus.x, curVirus.y, 0)); |
| 107 | + visited[curVirus.x][curVirus.y] = true; |
| 108 | + } |
| 109 | + |
| 110 | + int res = 0; |
| 111 | + while (!q.isEmpty()) { |
| 112 | + Node cur = q.poll(); |
| 113 | + if (matrix[cur.x][cur.y] == 0) { |
| 114 | + res = Math.max(res, cur.cnt); |
| 115 | + } |
| 116 | + |
| 117 | + for (int d = 0; d < 4; d++) { |
| 118 | + int nx = cur.x + dx[d]; |
| 119 | + int ny = cur.y + dy[d]; |
| 120 | + |
| 121 | + if (OOB(nx, ny) || visited[nx][ny] || matrix[nx][ny] == 1) { |
| 122 | + continue; |
| 123 | + } |
| 124 | + |
| 125 | + visited[nx][ny] = true; |
| 126 | + q.offer(new Node(nx, ny, cur.cnt + 1)); |
| 127 | + } |
| 128 | + } |
| 129 | + if (checkValid()) { |
| 130 | + ans = Math.min(ans, res); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + private static void clear() { |
| 135 | + q.clear(); |
| 136 | + |
| 137 | + for (int i = 0; i < N; i++) { |
| 138 | + Arrays.fill(visited[i], false); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + private static boolean checkValid() { |
| 143 | + for (int i = 0; i < N; i++) { |
| 144 | + for (int j = 0; j < N; j++) { |
| 145 | + if (matrix[i][j] == 0 && !visited[i][j]) { |
| 146 | + return false; |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + return true; |
| 151 | + } |
| 152 | + |
| 153 | + private static boolean OOB(int x, int y) { |
| 154 | + return x < 0 || x >= N || y < 0 || y >= N; |
| 155 | + } |
| 156 | + |
| 157 | +} |
| 158 | +``` |
0 commit comments