1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
| import java.awt.Color; import java.awt.GridLayout; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionListener; import java.util.ArrayList; import java.util.HashSet;
import javax.swing.JFrame; import javax.swing.JPanel;
public class PathFinder { ArrayList<Node> open = new ArrayList(); HashSet<Node> closed = new HashSet(); int width, height; int[][] map; int targetX, targetY; int startX, startY; private JFrame frame;
public static void main (String[] args) { PathFinder p = new PathFinder(); p.width = 18; p.height = 16; p.map = new int[p.width][p.height]; p.map[4][1] = -1; p.map[4][2] = -1; p.map[4][3] = -1; p.startX = 2; p.startY = 2; p.targetX = 16; p.targetY = 12; p.run(); }
private void run () { frame = new JFrame(); frame.getContentPane().setLayout(new GridLayout(height, width, 5, 5)); frame.addMouseMotionListener(new MouseMotionListener() { public void mouseDragged (MouseEvent e) { }
public void mouseMoved (MouseEvent e) { startX = (int)(e.getX() / 800f * width); startY = (int)(e.getY() / 600f * height); update(); } }); frame.setSize(800, 600); frame.setLocationRelativeTo(null); frame.setVisible(true); update(); }
void update () { frame.getContentPane().removeAll(); ArrayList<int[]> path = go(); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { JPanel panel = new JPanel(); panel.setOpaque(true); panel.setBackground(map[x][y] == 0 ? Color.black : Color.red); if (path != null) { for (int[] coord : path) if (x == coord[0] && y == coord[1]) panel.setBackground(Color.green); } if (x == startX && y == startY) panel.setBackground(Color.blue); if (x == targetX && y == targetY) panel.setBackground(Color.blue); frame.getContentPane().add(panel); } } frame.validate(); }
public ArrayList<int[]> go () { long start = System.nanoTime();
open.clear(); closed.clear();
Node root = new Node(null, startX, startY, 0); open.add(root);
while (!open.isEmpty()) { int lowestIndex = -1; int lowestCost = Integer.MAX_VALUE; for (int i = 0, n = open.size(); i < n; i++) { Node node = open.get(i); int cost = node.g + node.h; if (cost < lowestCost) { lowestCost = cost; lowestIndex = i; } }
Node check = open.remove(lowestIndex); if (check.x == targetX && check.y == targetY) { ArrayList<int[]> path = new ArrayList(); while (check != root) { path.add(0, new int[] {check.x, check.y}); check = check.parent; }
long end = System.nanoTime(); System.out.println((end - start) / 1000000f);
return path; } closed.add(check);
addNode(check, check.x, check.y + 1, 10); addNode(check, check.x, check.y - 1, 10); addNode(check, check.x + 1, check.y, 10); addNode(check, check.x - 1, check.y, 10); addNode(check, check.x + 1, check.y + 1, 14); addNode(check, check.x - 1, check.y - 1, 14); addNode(check, check.x + 1, check.y - 1, 14); addNode(check, check.x - 1, check.y + 1, 14); }
return null; }
private void addNode (Node parent, int x, int y, int cost) { if (x < 0 || y < 0) return; if (x >= width || y >= height) return; if (map[x][y] != 0) return; Node node = new Node(parent, x, y, cost); if (closed.contains(node)) return; int existingIndex = open.indexOf(node); if (existingIndex == -1) { open.add(node); } else { Node existing = open.get(existingIndex); if (node.g < existing.g) { existing.parent = parent; existing.g = node.g; } } }
private class Node { public Node parent; public int x, y; public int g, h;
public Node (Node parent, int x, int y, int cost) { this.parent = parent; this.x = x; this.y = y; g = parent == null ? 0 : parent.g + cost; h = Math.abs(x - targetX) + Math.abs(y - targetY); h *= 10; }
public int hashCode () { int result = 1; result = 31 * result + x; result = 31 * result + y; return result; }
public boolean equals (Object object) { Node node = (Node)object; return node.x == x && node.y == y; } } } |