2021/07/12
Leetcode:
- medium, 542. 01 Matrix`
- medium, 133. Clone Graph
- medium, 785. Is Graph Bipartite?
- medium, 1306. Jump Game III
- medium, 1926. Nearest Exit from Entrance in Maze
- hard, 847. Shortest Path Visiting All Nodes
- hard, 1192. Critical Connections in a Network
public int nearestExit(char[][] maze, int[] entrance) {
int[][] dirs = new int[][]{ {1,0}, {-1,0}, {0,1}, {0,-1} };
int rows = maze.length, cols = maze[0].length;
Queue<int[]> queue = new LinkedList<>();
maze[entrance[0]][entrance[1]] = '+';
queue.offer(new int[]{entrance[0],entrance[1]});
int moves = 1;
while(!queue.isEmpty()) {
int size = queue.size();
Queue<int[]> next = new LinkedList<>();
for(int k = 0; k < size; k++) {
int[] p = queue.poll();
for(int[] dir: dirs) {
int x = p[0] + dir[0];
int y = p[1] + dir[1];
if(x < 0 ||x >= rows || y < 0 || y >= cols || maze[x][y] == '+')
continue;
if(x == 0 || x == rows-1 || y == 0 || y == cols-1)
return moves;
maze[x][y] = '+';
next.offer(new int[]{x,y});
}
}
moves++;
queue = next;
}
return -1;
}