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
|
public class AStar {
public static final int[][] MAZE = { {0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 1, 0, 0, 0}, {0, 0, 0, 1, 0, 0, 0}, {0, 0, 0, 1, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0} };
public static Grid aStarSearch(Grid start, Grid end) { ArrayList<Grid> openList = new ArrayList<Grid>(); ArrayList<Grid> closeList = new ArrayList<Grid>(); openList.add(start); while (openList.size() > 0) { Grid currentGrid = findMinGird(openList); openList.remove(currentGrid); closeList.add(currentGrid); List<Grid> neighbors = findNeighbors(currentGrid, openList, closeList); for (Grid grid : neighbors) { grid.initGrid(currentGrid, end); openList.add(grid); } for (Grid grid : openList) { if ((grid.x == end.x) && (grid.y == end.y)) { return grid; } } } return null; }
private static Grid findMinGird(ArrayList<Grid> openList) { Grid tempGrid = openList.get(0); for (Grid grid : openList) { if (grid.f < tempGrid.f) { tempGrid = grid; } } return tempGrid; }
private static ArrayList<Grid> findNeighbors(Grid grid, List<Grid> openList, List<Grid> closeList) { ArrayList<Grid> gridList = new ArrayList<Grid>(); if (isValidGrid(grid.x, grid.y - 1, openList, closeList)) { gridList.add(new Grid(grid.x, grid.y - 1)); } if (isValidGrid(grid.x, grid.y + 1, openList, closeList)) { gridList.add(new Grid(grid.x, grid.y + 1)); } if (isValidGrid(grid.x - 1, grid.y, openList, closeList)) { gridList.add(new Grid(grid.x - 1, grid.y)); } if (isValidGrid(grid.x + 1, grid.y, openList, closeList)) { gridList.add(new Grid(grid.x + 1, grid.y)); } return gridList; }
private static boolean isValidGrid(int x, int y, List<Grid> openList, List<Grid> closeList) { if (x < 0 || x >= MAZE.length || y < 0 || y >= MAZE[0].length) { return false; } if (MAZE[x][y] == 1) { return false; } if (containGrid(openList, x, y)) { return false; } if (containGrid(closeList, x, y)) { return false; } return true; }
private static boolean containGrid(List<Grid> grids, int x, int y) { for (Grid grid : grids) { if ((grid.x == x) && (grid.y == y)) { return true; } } return false; }
static class Grid { public int x; public int y; public int f; public int g; public int h; public Grid parent;
public Grid(int x, int y) { this.x = x; this.y = y; }
public void initGrid(Grid parent, Grid end) { this.parent = parent; this.g = parent.g + 1; this.h = Math.abs(this.x - end.x) + Math.abs(this.y - end.y); this.f = this.g + this.h; } }
public static void main(String[] args) { Grid startGrid = new Grid(2, 1); Grid endGrid = new Grid(2, 5); Grid resultGrid = aStarSearch(startGrid, endGrid); ArrayList<Grid> path = new ArrayList<Grid>(); while (resultGrid != null) { path.add(new Grid(resultGrid.x, resultGrid.y)); resultGrid = resultGrid.parent; } for (int i = 0; i < MAZE.length; i++) { for (int j = 0; j < MAZE[0].length; j++) { if (containGrid(path, i, j)) { System.out.print("*, "); } else { System.out.print(MAZE[i][j] + ", "); } } System.out.println(); } } }
|