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 168 169 170 171 172 173 174 175 176 177 178 179
| import java.util.LinkedList; import java.util.Queue;
public class BFS { public static int getShort(int[][] map){ int ans = 0; int startX = 0; int startY = 0; int maxX = map.length - 1; int maxY = map[0].length - 1;
Queue<Coordinate> coordinateQueue = new LinkedList<>();
for (int x = 0; x < map.length; x++) { for (int y = 0; y < map[0].length; y++){ if (map[x][y] == -10) { startX = x; startY = y; } } }
MovePoint movePoint = new MovePoint(map, maxX, maxY);
Coordinate coordinate = new Coordinate(startX, startY, 0); coordinateQueue.add(coordinate);
while (true) { if (coordinateQueue.isEmpty()) { break; } Coordinate point = coordinateQueue.poll(); movePoint.setCoordinate(point); Coordinate l = movePoint.left(); Coordinate b = movePoint.bottom(); Coordinate t = movePoint.top(); Coordinate r = movePoint.right(); if (l != null) { if (isAns(l, map)) { ans = l.step; break; } coordinateQueue.add(l); } if (b != null) { if (isAns(b, map)) { ans = b.step; break; } coordinateQueue.add(b); } if (t != null) { if (isAns(t, map)) { ans = t.step; break; } coordinateQueue.add(t); } if (r != null) { if (isAns(r, map)) { ans = r.step; break; } coordinateQueue.add(r); } } return ans; }
private static boolean isAns(Coordinate coordinate, int[][] map) { return map[coordinate.getX()][coordinate.getY()] == 10; }
public static void main(String[] args) { int[][] data = {{-10, 0, 0, 0}, {0, 1, 0, 1}, {0, 0, 1, 10}, {1, 0, 0, 0}}; System.out.println(isAns(new Coordinate(2, 3, 0), data)); } }
class Coordinate { int x; int y; int step = 0; public Coordinate(int x, int y, int step) { this.x = x; this.y = y; this.step = step; }
public int getX() { return x; } public int getY() { return y; } }
class MovePoint{ Coordinate coordinate; private final int maxX; private final int maxY; private final int[][] map; MovePoint(int[][] map, int maxX, int maxY) { this.maxY = maxY; this.maxX = maxX; this.map = map; }
public void setCoordinate(Coordinate coordinate) { this.coordinate = coordinate; }
public Coordinate top() { int x = coordinate.getX() - 1; int y = coordinate.getY(); boolean isLegal = x > maxX || x < 0 || y > maxY || y < 0 || map[x][y] == 1; if (isLegal) { return null; } else { map [x][y] = map[x][y] == 10 ? 10 : 1; coordinate.step++; return new Coordinate(x, y, coordinate.step); } }
public Coordinate bottom() { int x = coordinate.getX() + 1; int y = coordinate.getY(); boolean isLegal = x > maxX || x < 0 || y > maxY || y < 0 || map[x][y] == 1; if (isLegal) { return null; } else { map[x][y] = 1; coordinate.step++; return new Coordinate(x, y, coordinate.step); } }
public Coordinate right() { int x = coordinate.getX(); int y = coordinate.getY() + 1; boolean isLegal = x > maxX || x < 0 || y > maxY || y < 0 || map[x][y] == 1; if (isLegal) { return null; } else { map[x][y] = 1; coordinate.step++; return new Coordinate(x, y, coordinate.step); } }
public Coordinate left() { int x = coordinate.getX(); int y = coordinate.getY() - 1; boolean isLegal = x > maxX || x < 0 || y > maxY || y < 0 || map[x][y] == 1; if (isLegal) { return null; } else { map[x][y] = 1; coordinate.step++; return new Coordinate(x, y, coordinate.step); } }
}
|