|
| 1 | +### 并查集 |
| 2 | +#### 模板 |
| 3 | +```java |
| 4 | +/** |
| 5 | + * 1.find: 确定元素子集 |
| 6 | + * 2.union: 合并两个子集 |
| 7 | + * 3.makeSet: 简历单元素集合 |
| 8 | + * 路径压缩优化: 认老大, 减少深度 |
| 9 | + */ |
| 10 | +class unionFind { |
| 11 | + private int count = 0; |
| 12 | + private int[] parent; |
| 13 | + public unionFind(int n) { |
| 14 | + count = n; |
| 15 | + parent = new int[n]; |
| 16 | + for (int i = 0; i < n; i++) { |
| 17 | + parent[i] = i; |
| 18 | + } |
| 19 | + } |
| 20 | + |
| 21 | + public int find(int p) { |
| 22 | + while (p != parent[p]) { |
| 23 | + // 路径压缩 |
| 24 | + parent[p] = parent[parent[p]]; |
| 25 | + p = parent[p]; |
| 26 | + } |
| 27 | + return p; |
| 28 | + } |
| 29 | + |
| 30 | + public void union(int p, int q) { |
| 31 | + int rootP = find(p); |
| 32 | + int rootQ = find(q); |
| 33 | + if (rootP == rootQ) return; |
| 34 | + parent[rootP] = rootQ; |
| 35 | + count--; |
| 36 | + } |
| 37 | +} |
| 38 | +``` |
| 39 | +<br/> |
| 40 | + |
| 41 | +### A* |
| 42 | +#### 模板 |
| 43 | +```java |
| 44 | +/** |
| 45 | + * BFS + 优先级 |
| 46 | + */ |
| 47 | +public class AStar { |
| 48 | + public final static int BAR = 1; // 障碍值 |
| 49 | + public final static int PATH = 2; // 路径 |
| 50 | + public final static int DIRECT_VALUE = 10; // 横竖移动代价 |
| 51 | + public final static int OBLIQUE_VALUE = 14; // 斜移动代价 |
| 52 | + |
| 53 | + Queue<Node> openList = new PriorityQueue<Node>(); // 优先队列(升序) |
| 54 | + List<Node> closeList = new ArrayList<Node>(); |
| 55 | + |
| 56 | + /** |
| 57 | + * 开始算法 |
| 58 | + */ |
| 59 | + public void start(MapInfo mapInfo) { |
| 60 | + if(mapInfo==null) return; |
| 61 | + // clean |
| 62 | + openList.clear(); |
| 63 | + closeList.clear(); |
| 64 | + // 开始搜索 |
| 65 | + openList.add(mapInfo.start); |
| 66 | + moveNodes(mapInfo); |
| 67 | + } |
| 68 | + |
| 69 | + |
| 70 | + /** |
| 71 | + * 移动当前结点 |
| 72 | + */ |
| 73 | + private void moveNodes(MapInfo mapInfo) { |
| 74 | + while (!openList.isEmpty()) { |
| 75 | + Node current = openList.poll(); |
| 76 | + closeList.add(current); |
| 77 | + addNeighborNodeInOpen(mapInfo,current); |
| 78 | + if (isCoordInClose(mapInfo.end.coord)) { |
| 79 | + drawPath(mapInfo.maps, mapInfo.end); |
| 80 | + break; |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * 在二维数组中绘制路径 |
| 87 | + */ |
| 88 | + private void drawPath(int[][] maps, Node end) { |
| 89 | + if(end==null||maps==null) return; |
| 90 | + System.out.println("总代价:" + end.G); |
| 91 | + while (end != null) { |
| 92 | + Coord c = end.coord; |
| 93 | + maps[c.y][c.x] = PATH; |
| 94 | + end = end.parent; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + |
| 99 | + /** |
| 100 | + * 添加所有邻结点到open表 |
| 101 | + */ |
| 102 | + private void addNeighborNodeInOpen(MapInfo mapInfo,Node current) { |
| 103 | + int x = current.coord.x; |
| 104 | + int y = current.coord.y; |
| 105 | + // 左 |
| 106 | + addNeighborNodeInOpen(mapInfo,current, x - 1, y, DIRECT_VALUE); |
| 107 | + // 上 |
| 108 | + addNeighborNodeInOpen(mapInfo,current, x, y - 1, DIRECT_VALUE); |
| 109 | + // 右 |
| 110 | + addNeighborNodeInOpen(mapInfo,current, x + 1, y, DIRECT_VALUE); |
| 111 | + // 下 |
| 112 | + addNeighborNodeInOpen(mapInfo,current, x, y + 1, DIRECT_VALUE); |
| 113 | + // 左上 |
| 114 | + addNeighborNodeInOpen(mapInfo,current, x - 1, y - 1, OBLIQUE_VALUE); |
| 115 | + // 右上 |
| 116 | + addNeighborNodeInOpen(mapInfo,current, x + 1, y - 1, OBLIQUE_VALUE); |
| 117 | + // 右下 |
| 118 | + addNeighborNodeInOpen(mapInfo,current, x + 1, y + 1, OBLIQUE_VALUE); |
| 119 | + // 左下 |
| 120 | + addNeighborNodeInOpen(mapInfo,current, x - 1, y + 1, OBLIQUE_VALUE); |
| 121 | + } |
| 122 | + |
| 123 | + |
| 124 | + /** |
| 125 | + * 添加一个邻结点到open表 |
| 126 | + */ |
| 127 | + private void addNeighborNodeInOpen(MapInfo mapInfo,Node current, int x, int y, int value) { |
| 128 | + if (canAddNodeToOpen(mapInfo,x, y)) { |
| 129 | + Node end=mapInfo.end; |
| 130 | + Coord coord = new Coord(x, y); |
| 131 | + int G = current.G + value; // 计算邻结点的G值 |
| 132 | + Node child = findNodeInOpen(coord); |
| 133 | + if (child == null) { |
| 134 | + int H=calcH(end.coord,coord); // 计算H值 |
| 135 | + if(isEndNode(end.coord,coord)) { |
| 136 | + child=end; |
| 137 | + child.parent=current; |
| 138 | + child.G=G; |
| 139 | + child.H=H; |
| 140 | + } else { |
| 141 | + child = new Node(coord, current, G, H); |
| 142 | + } |
| 143 | + openList.add(child); |
| 144 | + } else if (child.G > G) { |
| 145 | + child.G = G; |
| 146 | + child.parent = current; |
| 147 | + openList.add(child); |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + |
| 153 | + /** |
| 154 | + * 从Open列表中查找结点 |
| 155 | + */ |
| 156 | + private Node findNodeInOpen(Coord coord) { |
| 157 | + if (coord == null || openList.isEmpty()) return null; |
| 158 | + for (Node node : openList) { |
| 159 | + if (node.coord.equals(coord)) { |
| 160 | + return node; |
| 161 | + } |
| 162 | + } |
| 163 | + return null; |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * 计算H的估值:“曼哈顿”法,坐标分别取差值相加 |
| 168 | + */ |
| 169 | + private int calcH(Coord end,Coord coord) { |
| 170 | + return Math.abs(end.x - coord.x) |
| 171 | + + Math.abs(end.y - coord.y); |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * 判断结点是否是最终结点 |
| 176 | + */ |
| 177 | + private boolean isEndNode(Coord end,Coord coord) { |
| 178 | + return coord != null && end.equals(coord); |
| 179 | + } |
| 180 | + |
| 181 | + |
| 182 | + /** |
| 183 | + * 判断结点能否放入Open列表 |
| 184 | + */ |
| 185 | + private boolean canAddNodeToOpen(MapInfo mapInfo,int x, int y) { |
| 186 | + // 是否在地图中 |
| 187 | + if (x < 0 || x >= mapInfo.width || y < 0 || y >= mapInfo.hight) return false; |
| 188 | + // 判断是否是不可通过的结点 |
| 189 | + if (mapInfo.maps[y][x] == BAR) return false; |
| 190 | + // 判断结点是否存在close表 |
| 191 | + if (isCoordInClose(x, y)) return false; |
| 192 | + |
| 193 | + return true; |
| 194 | + } |
| 195 | + |
| 196 | + |
| 197 | + /** |
| 198 | + * 判断坐标是否在close表中 |
| 199 | + */ |
| 200 | + private boolean isCoordInClose(Coord coord) { |
| 201 | + return coord!=null&&isCoordInClose(coord.x, coord.y); |
| 202 | + } |
| 203 | + |
| 204 | + |
| 205 | + /** |
| 206 | + * 判断坐标是否在close表中 |
| 207 | + */ |
| 208 | + private boolean isCoordInClose(int x, int y) { |
| 209 | + if (closeList.isEmpty()) return false; |
| 210 | + for (Node node : closeList) { |
| 211 | + if (node.coord.x == x && node.coord.y == y) { |
| 212 | + return true; |
| 213 | + } |
| 214 | + } |
| 215 | + return false; |
| 216 | + } |
| 217 | +} |
| 218 | + |
| 219 | +``` |
| 220 | + |
| 221 | +### 红黑树 AVL |
| 222 | +``` |
| 223 | +旋转 |
| 224 | + 左旋 |
| 225 | + 右旋 |
| 226 | + 左右旋 |
| 227 | + 右左旋 |
| 228 | +``` |
0 commit comments