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); } }
}
|