|
| 1 | +```java |
| 2 | + import java.io.*; |
| 3 | + import java.util.*; |
| 4 | + |
| 5 | + class Solution { |
| 6 | + static char[][] map; |
| 7 | + static int[][] dustDis; |
| 8 | + static Node[] dusts; |
| 9 | + static Map<Integer, Integer> dustIndex; |
| 10 | + static int N = 1; |
| 11 | + static int M = 1; |
| 12 | + static boolean[][] visited; |
| 13 | + static int count; |
| 14 | + static BufferedReader br; |
| 15 | + static StringTokenizer st; |
| 16 | + |
| 17 | + public static void main(String args[]) throws Exception { |
| 18 | + br = new BufferedReader(new InputStreamReader(System.in)); |
| 19 | + StringBuilder sb = new StringBuilder(); |
| 20 | + |
| 21 | + while (true) { |
| 22 | + M = read(); |
| 23 | + N = read(); |
| 24 | + if (M == 0 && N == 0) break; |
| 25 | + |
| 26 | + dusts = new Node[11]; |
| 27 | + count = 1; |
| 28 | + dustIndex = new HashMap<>(); |
| 29 | + map = new char[N][M]; |
| 30 | + |
| 31 | + for (int i = 0; i < N; i++) { |
| 32 | + for (int j = 0; j < M; j++) { |
| 33 | + map[i][j] = (char) System.in.read(); |
| 34 | + if (map[i][j] == '*') { |
| 35 | + dustIndex.put(i * 20 + j, count); |
| 36 | + dusts[count] = new Node(i, j); |
| 37 | + count++; |
| 38 | + } |
| 39 | + if (map[i][j] == 'o') { |
| 40 | + dusts[0] = new Node(i, j); |
| 41 | + } |
| 42 | + } |
| 43 | + System.in.read(); |
| 44 | + } |
| 45 | + |
| 46 | + dustDis = new int[count + 1][count + 1]; |
| 47 | + for (int i = 0; i < count; i++) { |
| 48 | + bfs(dusts[i].i, dusts[i].j, i); |
| 49 | + } |
| 50 | + |
| 51 | + int ans = bitMasking(); |
| 52 | + sb.append(ans == 0 || ans == (Integer.MAX_VALUE >> 1) ? -1 : ans).append("\n"); |
| 53 | + } |
| 54 | + |
| 55 | + System.out.print(sb); |
| 56 | + } |
| 57 | + |
| 58 | + public static int bitMasking() { |
| 59 | + count--; |
| 60 | + int[][] dustDP = new int[1 << count][count + 1]; |
| 61 | + for (int i = 0; i < (1 << count); i++) |
| 62 | + Arrays.fill(dustDP[i], Integer.MAX_VALUE >> 1); |
| 63 | + |
| 64 | + for (int i = 0; i < count; i++) dustDP[1 << i][i + 1] = dustDis[0][i + 1]; |
| 65 | + |
| 66 | + for (int i = 0; i < (1 << count); i++) { |
| 67 | + for (int k = 1; k <= count; k++) { |
| 68 | + for (int j = 0; j < count; j++) { |
| 69 | + int a = 1 << j; |
| 70 | + int ba = a | i; |
| 71 | + if (ba == i || ba >= dustDP.length || dustDis[k][j + 1] == 0) continue; |
| 72 | + dustDP[ba][j + 1] = Math.min(dustDP[ba][j + 1], dustDP[i][k] + dustDis[k][j + 1]); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + int min = Integer.MAX_VALUE; |
| 78 | + for (int i = 0; i <= count; i++) { |
| 79 | + min = Math.min(dustDP[(1 << count) - 1][i], min); |
| 80 | + } |
| 81 | + return min; |
| 82 | + } |
| 83 | + |
| 84 | + public static void bfs(int si, int sj, int index) { |
| 85 | + visited = new boolean[N][M]; |
| 86 | + int[] di = {1, -1, 0, 0}; |
| 87 | + int[] dj = {0, 0, 1, -1}; |
| 88 | + |
| 89 | + Queue<Dust> queue = new LinkedList<>(); |
| 90 | + queue.add(new Dust(si, sj, 0)); |
| 91 | + visited[si][sj] = true; |
| 92 | + |
| 93 | + while (!queue.isEmpty()) { |
| 94 | + Dust dust = queue.poll(); |
| 95 | + int i = dust.i; |
| 96 | + int j = dust.j; |
| 97 | + int count = dust.count; |
| 98 | + |
| 99 | + int d = dustIndex.getOrDefault(i * 20 + j, -1); |
| 100 | + if (d != -1) { |
| 101 | + dustDis[index][d] = count; |
| 102 | + } |
| 103 | + |
| 104 | + for (int k = 0; k < 4; k++) { |
| 105 | + int ii = i + di[k]; |
| 106 | + int jj = j + dj[k]; |
| 107 | + |
| 108 | + if (!check(ii, jj)) continue; |
| 109 | + visited[ii][jj] = true; |
| 110 | + queue.add(new Dust(ii, jj, count + 1)); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + public static boolean check(int i, int j) { |
| 116 | + if (i < 0 || i >= N || j < 0 || j >= M) return false; |
| 117 | + if (map[i][j] == 'x') return false; |
| 118 | + return !visited[i][j]; |
| 119 | + } |
| 120 | + |
| 121 | + public static class Node { |
| 122 | + int i, j; |
| 123 | + public Node(int i, int j) { |
| 124 | + this.i = i; |
| 125 | + this.j = j; |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + public static class Dust { |
| 130 | + int i, j, count; |
| 131 | + public Dust(int i, int j, int count) { |
| 132 | + this.i = i; |
| 133 | + this.j = j; |
| 134 | + this.count = count; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + private static int read() throws Exception { |
| 139 | + int d, o; |
| 140 | + boolean negative = false; |
| 141 | + d = System.in.read(); |
| 142 | + if (d == '-') { |
| 143 | + negative = true; |
| 144 | + d = System.in.read(); |
| 145 | + } |
| 146 | + |
| 147 | + o = d & 15; |
| 148 | + while ((d = System.in.read()) > 32) |
| 149 | + o = (o << 3) + (o << 1) + (d & 15); |
| 150 | + |
| 151 | + return negative? -o:o; |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | +``` |
0 commit comments