|
| 1 | +```java |
| 2 | +import java.util.*; |
| 3 | +import java.io.*; |
| 4 | + |
| 5 | +public class Main { |
| 6 | + static class Node { |
| 7 | + int i, j,time; |
| 8 | + |
| 9 | + public Node(int i, int j, int time) { |
| 10 | + this.i = i; |
| 11 | + this.j = j; |
| 12 | + this.time = time; |
| 13 | + } |
| 14 | + } |
| 15 | + |
| 16 | + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 17 | + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 18 | + static StringTokenizer st; |
| 19 | + static int N; |
| 20 | + static int M; |
| 21 | + static int[][] matrix; |
| 22 | + static List<Node> virus = new ArrayList<>(); |
| 23 | + static List<Node> selected = new ArrayList<>(); |
| 24 | + static int[] di = {0, 0, -1, 1}; |
| 25 | + static int[] dj = {-1, 1, 0, 0}; |
| 26 | + static int min = Integer.MAX_VALUE; |
| 27 | + static int zeroCnt = 0; |
| 28 | + |
| 29 | + public static void main(String[] args) throws Exception { |
| 30 | + st = new StringTokenizer(br.readLine()); |
| 31 | + N = Integer.parseInt(st.nextToken()); |
| 32 | + M = Integer.parseInt(st.nextToken()); |
| 33 | + matrix = new int[N][N]; |
| 34 | + |
| 35 | + for (int i = 0; i < N; i++) { |
| 36 | + st = new StringTokenizer(br.readLine()); |
| 37 | + for (int j = 0; j < N; j++) { |
| 38 | + matrix[i][j] = Integer.parseInt(st.nextToken()); |
| 39 | + if (matrix[i][j] == 2) { |
| 40 | + virus.add(new Node(i, j,0)); |
| 41 | + }else if(matrix[i][j] == 0){ |
| 42 | + zeroCnt++; |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + combination(0, 0); |
| 47 | + if(min == Integer.MAX_VALUE) bw.write("-1"); |
| 48 | + else bw.write(min + ""); |
| 49 | + bw.close(); |
| 50 | + } |
| 51 | + |
| 52 | + public static void combination(int start, int count) { |
| 53 | + if (count == M) { |
| 54 | + BFS(); |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + for (int i = start; i < virus.size(); i++) { |
| 59 | + selected.add(virus.get(i)); |
| 60 | + combination(i + 1, count + 1); |
| 61 | + selected.remove(selected.size() - 1); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + public static void BFS() { |
| 66 | + PriorityQueue<Node> pq = new PriorityQueue<>((a,b) -> a.time - b.time); |
| 67 | + int[][] dist = new int[N][N]; |
| 68 | + for (int i = 0; i < N; i++) { |
| 69 | + Arrays.fill(dist[i],Integer.MAX_VALUE); |
| 70 | + } |
| 71 | + for (Node temp : selected) { |
| 72 | + pq.add(temp); |
| 73 | + dist[temp.i][temp.j] = 0; |
| 74 | + } |
| 75 | + int maxTime = 0; |
| 76 | + int remainZero = zeroCnt; |
| 77 | + |
| 78 | + while (!pq.isEmpty()) { |
| 79 | + Node cur = pq.poll(); |
| 80 | + if (matrix[cur.i][cur.j] == 0) remainZero--; |
| 81 | + if (remainZero == 0) { |
| 82 | + maxTime = Math.max(maxTime, cur.time); |
| 83 | + min = Math.min(min,maxTime); |
| 84 | + return; |
| 85 | + } |
| 86 | + if(dist[cur.i][cur.j] < cur.time) continue; |
| 87 | + for (int k = 0; k < 4; k++) { |
| 88 | + int newi = cur.i + di[k]; |
| 89 | + int newj = cur.j + dj[k]; |
| 90 | + |
| 91 | + if (newi < 0 || newi >= N || newj < 0 || newj >= N || matrix[newi][newj] == 1) |
| 92 | + continue; |
| 93 | + |
| 94 | + if (dist[newi][newj] > cur.time + 1) { |
| 95 | + pq.add(new Node(newi, newj, cur.time + 1)); |
| 96 | + dist[newi][newj] = cur.time + 1; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + } |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | + |
| 105 | +``` |
0 commit comments