💡 n x n인 이차원 배열인 그리드가 주어졌을 때, 출발지에서 목적지까지 도착하는 가장 빠른 경로의 길이를 반환하시오. 만약 경로가 없다면 -1을 반환하시오.
출발지 : top-left
목적지 : bottom -right
Example 1:
Input: grid = [[0,1],[1,0]]
Output: 2
Example 2:
Input: grid = [[0,0,0],[1,1,0],[1,1,0]]
Output: 4
Example 3:
Input: grid = [[1,0,0],[1,1,0],[1,1,0]]
Output: -1
n == grid.length
n == grid[i].length
1 <= n <= 100
grid[i][j] is 0 or 1
문제 링크 -> https://leetcode.com/problems/shortest-path-in-binary-matrix/
public class ShortestPathBinaryMatrix {
private int[][] grid;
private boolean[][] visited;
public int solve(int[][] grid) {
this.grid = grid;
this.visited = new boolean[grid.length][grid.length];
int startingPoint = grid[0][0];
int endPoint = grid[grid.length - 1][grid.length - 1];
if (startingPoint != 0 || endPoint != 0) {
return -1;
}
return bfs();
}
private int bfs() {
int result = -1;
Queue<PathVertex> queue = new ArrayDeque<>();
queue.offer(new PathVertex(0, 0, 1));
visited[0][0] = true;
while (!queue.isEmpty()) {
PathVertex pathVertex = queue.poll();
if (isGoal(pathVertex)) {
result = pathVertex.getPathLength();
break;
}
queue.addAll(findPath(pathVertex));
}
return result;
}
private boolean isGoal(PathVertex pathVertex) {
return pathVertex.getRow() == grid.length - 1 && pathVertex.getColumn() == grid.length - 1;
}
private List<PathVertex> findPath(PathVertex pathVertex) {
List<PathVertex> result = new ArrayList<>();
for (Directions direction : Directions.values()) {
int newRow = pathVertex.getRow() + direction.getDirection().getRow();
int newColumn = pathVertex.getColumn() + direction.getDirection().getColumn();
if (validCoordinate(newRow, newColumn)) {
PathVertex newPath = new PathVertex(newRow, newColumn, pathVertex.getPathLength() + 1);
result.add(newPath);
visited[newRow][newColumn] = true;
}
}
return result;
}
private boolean validCoordinate(int row, int column) {
return isInBoundary(row, column) && isPath(row, column) && isNotVisited(row, column);
}
private boolean isInBoundary(int row, int column) {
return row >= 0 && column >= 0 && row < grid.length && column < grid.length;
}
private boolean isPath(int curRow, int curColumn) {
return grid[curRow][curColumn] == 0;
}
private boolean isNotVisited(int curRow, int curColumn) {
return !this.visited[curRow][curColumn];
}
}
public class PathVertex {
private final int row;
private final int column;
private final int pathLength;
public PathVertex(int row, int column, int pathLength) {
this.row = row;
this.column = column;
this.pathLength = pathLength;
}
public int getRow() {
return row;
}
public int getColumn() {
return column;
}
public int getPathLength() {
return pathLength;
}
}
public class Direction {
private final int row;
private final int column;
public Direction(int row, int column) {
this.row = row;
this.column = column;
}
public int getRow() {
return row;
}
public int getColumn() {
return column;
}
}
public enum Directions {
NORTH(new Direction(-1, 0)),
NORTHEAST(new Direction(-1, 1)),
EAST(new Direction(0, 1)),
SOUTHEAST(new Direction(1, 1)),
SOUTH(new Direction(1, 0)),
SOUTHWEST(new Direction(1, -1)),
WEST(new Direction(0, -1)),
NORTHWEST(new Direction(-1, -1));
private final Direction direction;
Directions(Direction direction) {
this.direction = direction;
}
public Direction getDirection() {
return direction;
}
}
class ShortestPathBinaryMatrixTest {
@Test
void fail() {
int[][] grid1 = {
{1, 0, 0},
{1, 1, 0},
{1, 1, 0}
};
int[][] grid2 = {
{0, 0, 0},
{1, 1, 0},
{1, 1, 1}
};
int[][] grid3 = {
{0, 0, 0},
{1, 1, 1},
{1, 1, 0}
};
ShortestPathBinaryMatrix shortestPathBinaryMatrix = new ShortestPathBinaryMatrix();
int result1 = shortestPathBinaryMatrix.solve(grid1);
int result2 = shortestPathBinaryMatrix.solve(grid2);
int result3 = shortestPathBinaryMatrix.solve(grid3);
Assertions.assertEquals(-1, result1);
Assertions.assertEquals(-1, result2);
Assertions.assertEquals(-1, result3);
}
@Test
void solve() {
int[][] grid1 = {
{0, 0, 0},
{1, 1, 0},
{1, 1, 0}
};
int[][] grid2 = {
{0, 1, 1, 1, 1, 1, 1, 1},
{0, 1, 1, 0, 0, 0, 0, 0},
{0, 1, 0, 1, 1, 1, 1, 0},
{0, 1, 0, 1, 1, 1, 1, 0},
{0, 1, 1, 0, 0, 1, 1, 0},
{0, 1, 1, 1, 1, 0, 1, 0},
{0, 0, 0, 0, 0, 1, 1, 0},
{1, 1, 1, 1, 1, 1, 1, 0}
};
ShortestPathBinaryMatrix shortestPathBinaryMatrix = new ShortestPathBinaryMatrix();
int result1 = shortestPathBinaryMatrix.solve(grid1);
int result2 = shortestPathBinaryMatrix.solve(grid2);
Assertions.assertEquals(4, result1);
Assertions.assertEquals(25, result2);
}
}