|
| 1 | +package weekly; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.HashMap; |
| 5 | +import java.util.HashSet; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Map; |
| 8 | +import java.util.PriorityQueue; |
| 9 | +import java.util.Set; |
| 10 | + |
| 11 | +public class wk343 { |
| 12 | + |
| 13 | + //模拟 |
| 14 | + public int isWinner(int[] player1, int[] player2) { |
| 15 | + int sum1 = 0, sum2 = 0; |
| 16 | + for (int i = 0; i < player1.length; i++) { |
| 17 | + boolean f1 = false, f2 = false; |
| 18 | + int j = i - 1; |
| 19 | + while (j >= 0 && j >= i - 2) { |
| 20 | + if (player1[j] == 10) { |
| 21 | + f1 = true; |
| 22 | + } |
| 23 | + if (player2[j] == 10) { |
| 24 | + f2 = true; |
| 25 | + } |
| 26 | + j--; |
| 27 | + } |
| 28 | + if (f1) { |
| 29 | + sum1 += player1[i] * 2; |
| 30 | + } else { |
| 31 | + sum1 += player1[i]; |
| 32 | + } |
| 33 | + if (f2) { |
| 34 | + sum2 += player2[i] * 2; |
| 35 | + } else { |
| 36 | + sum2 += player2[i]; |
| 37 | + } |
| 38 | + } |
| 39 | + int ans = 0; |
| 40 | + if (sum1 > sum2) { |
| 41 | + ans = 1; |
| 42 | + } else if (sum2 > sum1) { |
| 43 | + ans = 2; |
| 44 | + } |
| 45 | + return ans; |
| 46 | + } |
| 47 | + |
| 48 | + |
| 49 | + //模拟 |
| 50 | + static public int firstCompleteIndex(int[] arr, int[][] mat) { |
| 51 | + Map<Integer, Integer> map = new HashMap<>(); |
| 52 | + int n = mat[0].length; |
| 53 | + int m = mat.length; |
| 54 | + for (int i = 0; i < mat.length; i++) { |
| 55 | + for (int j = 0; j < mat[0].length; j++) { |
| 56 | + map.put(mat[i][j], i * n + j); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + Map<Integer, Integer> col = new HashMap<>(); |
| 61 | + Map<Integer, Integer> row = new HashMap<>(); |
| 62 | + |
| 63 | + for (int i = 0; i < arr.length; i++) { |
| 64 | + Integer integer = map.get(arr[i]); |
| 65 | + int x = integer / n; |
| 66 | + int y = integer % n; |
| 67 | + row.put(x, row.getOrDefault(x, 0) + 1); |
| 68 | + col.put(y, col.getOrDefault(y, 0) + 1); |
| 69 | + if (row.get(x) == n || col.get(y) == m) { |
| 70 | + return i; |
| 71 | + } |
| 72 | + } |
| 73 | + return arr.length; |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | + |
| 79 | +// static int[][] moves = new int[][]{{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; |
| 80 | +// |
| 81 | +// static public int minimumCost(int[] start, int[] target, int[][] specialRoads) { |
| 82 | +// |
| 83 | +// PriorityQueue<int[]> priorityQueue = new PriorityQueue<>((a, b) -> a[0] - b[0]); |
| 84 | +// priorityQueue.add(new int[]{0, start[0], start[1]}); |
| 85 | +// |
| 86 | +// Map<String, List<Integer>> map = new HashMap<>(); |
| 87 | +// for (int i = 0; i < specialRoads.length; i++) { |
| 88 | +// int[] specialRoad = specialRoads[i]; |
| 89 | +// if (Math.abs(specialRoad[0] - specialRoad[2]) + Math.abs(specialRoad[1] - specialRoad[3]) >= specialRoad[4]) { |
| 90 | +// String k = specialRoad[0] + "-" + specialRoad[1]; |
| 91 | +// if (!map.containsKey(k)) { |
| 92 | +// map.put(k, new ArrayList<>()); |
| 93 | +// } |
| 94 | +// map.get(k).add(i); |
| 95 | +// } |
| 96 | +// } |
| 97 | +// // System.out.println(map.size()); |
| 98 | +// Set<String> set = new HashSet<>(); |
| 99 | +// while (!priorityQueue.isEmpty()) { |
| 100 | +// int[] cur = priorityQueue.poll(); |
| 101 | +// int cost = cur[0]; |
| 102 | +// int x = cur[1]; |
| 103 | +// int y = cur[2]; |
| 104 | +// if (set.contains(x + "-" + y)) continue; |
| 105 | +// if (x == target[0] && y == target[1]) { |
| 106 | +// return cost; |
| 107 | +// } |
| 108 | +// set.add(x + "-" + y); |
| 109 | +// |
| 110 | +// for (int[] move : moves) { |
| 111 | +// int nx = move[0] + x; |
| 112 | +// int ny = move[1] + y; |
| 113 | +// if(nx<=target[0]&&ny<=target[1]&&!set.contains(nx+"-"+ny)){ |
| 114 | +// priorityQueue.add(new int[]{cost + 1, nx, ny}); |
| 115 | +// } |
| 116 | +// } |
| 117 | +// for (Integer index : map.getOrDefault(x + "-" + y, new ArrayList<>())) { |
| 118 | +// if(set.contains(specialRoads[index][2]+"-"+specialRoads[index][3])) continue; |
| 119 | +// priorityQueue.add(new int[]{cost + specialRoads[index][4], specialRoads[index][2], specialRoads[index][3]}); |
| 120 | +// } |
| 121 | +// } |
| 122 | +// return -1; |
| 123 | +// } |
| 124 | + |
| 125 | + |
| 126 | + //朴素Dijkstra |
| 127 | + /* public int minimumCost(int[] start, int[] target, int[][] specialRoads) { |
| 128 | + long t = (long) target[0] << 32 | target[1]; |
| 129 | + Map<Long, Integer> dis = new HashMap(); |
| 130 | + dis.put(t, Math.abs(start[0] - target[0]) + Math.abs(start[1] - target[1])); |
| 131 | + dis.put((long) start[0] << 32 | start[1], 0); |
| 132 | + Set<Long> vis = new HashSet<>(); |
| 133 | + for (; ; ) { |
| 134 | + long v = -1; |
| 135 | + int dv = -1; |
| 136 | + for (Map.Entry<Long, Integer> e : dis.entrySet()) { |
| 137 | + if (!vis.contains(e.getKey()) && (dv < 0 || e.getValue() < dv)) { |
| 138 | + v = e.getKey(); |
| 139 | + dv = e.getValue(); |
| 140 | + } |
| 141 | + } |
| 142 | + if (v == t) return dv; // 到终点的最短路已确定 |
| 143 | + vis.add(v); |
| 144 | + int vx = (int) (v >> 32), vy = (int) (v & Integer.MAX_VALUE); |
| 145 | + // 更新到终点的最短路 |
| 146 | + dis.merge(t, dv + (target[0] - vx + target[1] - vy), Math::min); |
| 147 | + //计算改点到其他special的距离 |
| 148 | + for (int[] r : specialRoads) { |
| 149 | + int d = dv + Math.abs(r[0] - vx) + Math.abs(r[1] - vy) + r[4]; |
| 150 | + long w = (long) r[2] << 32 | r[3]; |
| 151 | + if (d < dis.getOrDefault(w, Integer.MAX_VALUE)) |
| 152 | + dis.put(w, d); |
| 153 | + } |
| 154 | + } |
| 155 | + }*/ |
| 156 | + |
| 157 | + int dis(int x1, int y1, int x2, int y2) { |
| 158 | + return Math.abs(x1 - x2) + Math.abs(y1 - y2); |
| 159 | + } |
| 160 | + |
| 161 | + //优先队列 Dijkstra |
| 162 | + public int minimumCost(int[] start, int[] target, int[][] specialRoads) { |
| 163 | + PriorityQueue<int[]> priorityQueue = new PriorityQueue<>((a, b) -> a[0] - b[0]); |
| 164 | + priorityQueue.add(new int[]{0, start[0], start[1]}); |
| 165 | + priorityQueue.add(new int[]{Integer.MAX_VALUE, target[0], target[1]}); |
| 166 | + Set<Long> visited = new HashSet<>(); |
| 167 | + while (!priorityQueue.isEmpty()) { |
| 168 | + int[] poll = priorityQueue.poll(); |
| 169 | + long flag = ((long) poll[1] << 32) + poll[2]; |
| 170 | + System.out.println(poll[0]+" "+poll[1]+" "+poll[2]); |
| 171 | + if (visited.contains(flag)) continue; |
| 172 | + if (poll[1] == target[0] && poll[2] == target[1]) { |
| 173 | + return poll[0]; |
| 174 | + } |
| 175 | + visited.add(flag); |
| 176 | + priorityQueue.add(new int[]{dis(target[0], target[1], poll[1], poll[2])+poll[0], target[0], target[1]}); |
| 177 | + for (int[] specialRoad : specialRoads) { |
| 178 | + long f = ((long) specialRoad[2] << 32) + specialRoad[3]; |
| 179 | + if (visited.contains(f)) continue; |
| 180 | + int d = Math.min(specialRoad[4] + dis(poll[1], poll[2], specialRoad[0], specialRoad[1]),dis(specialRoad[2],specialRoad[3],poll[1],poll[2]))+poll[0]; |
| 181 | + priorityQueue.add(new int[]{d, specialRoad[2], specialRoad[3]}); |
| 182 | + } |
| 183 | + } |
| 184 | + return -1; |
| 185 | + } |
| 186 | + public static void main(String[] args) { |
| 187 | + wk343 w=new wk343(); |
| 188 | + w.minimumCost(new int[]{1,1},new int[]{10,9},new int[][]{ |
| 189 | + {5,2,3,6,3},{5,6,9,5,3},{5,9,1,2,5},{8,6,9,8,1} |
| 190 | + }); |
| 191 | + } |
| 192 | + |
| 193 | + |
| 194 | +} |
0 commit comments