The Maze II
题目链接:https://leetcode.com/contest/...
一般这种题,给一个起点,给一个终点,最后要求最短的路径,都是用bfs来解。
public class Solution {
public String findShortestWay(int[][] maze, int[] ball, int[] hole) {
/* minheap: check the path with smaller length first
* path[x][y]: record path from ball to (x, y)
* visited[x][y]: length from ball to (x, y)
*/
PriorityQueue<Node> heap = new PriorityQueue<>(new Comparator<Node>() {
public int compare(Node a, Node b) {
return a.len - b.len;
}
});
heap.offer(new Node(ball[0], ball[1], 1));
col = maze[0].length;
// track result
String result = "";
int len = Integer.MAX_VALUE;
// length and path so far
visited = new int[maze.length][maze[0].length];
visited[ball[0]][ball[1]] = 1;
path = new String[maze.length][maze[0].length];
path[ball[0]][ball[1]] = "";
while(!heap.isEmpty()) {
Node cur = heap.poll();
visited[cur.x][cur.y] = cur.len;
// find the hole, update result
if(cur.x == hole[0] && cur.y == hole[1]) {
if(len > cur.len || (len == cur.len && result.compareTo(path[cur.x][cur.y]) > 0)) {
len = cur.len;
result = path[cur.x][cur.y];
}
continue;
}
// 4 directions
for(int i = 0; i < 4; i++) {
int nx = cur.x, ny = cur.y;
int depth = cur.len;
// find the next position (reach before the wall)
while(nx + dx[i] >= 0 && nx + dx[i] < maze.length && ny + dy[i] >= 0 && ny + dy[i] < maze[0].length) {
// find the path so far from ball to [nx, ny]
String curPath = path[cur.x][cur.y] + (nx == cur.x && ny == cur.y ? "" : dir[i]);
// reach the hole
if(nx == hole[0] && ny == hole[1]) break;
// meet the wall
if(maze[nx + dx[i]][ny + dy[i]] == 1) break;
// loop update
depth++;
nx += dx[i];
ny += dy[i];
}
// pruning
if(depth > len) continue;
String curPath = path[cur.x][cur.y] + (nx == cur.x && ny == cur.y ? "" : dir[i]);
if(visited[nx][ny] != 0 && visited[nx][ny] < depth) continue;
if(visited[nx][ny] != 0 && visited[nx][ny] == depth && path[nx][ny].compareTo(curPath) <= 0) continue;
// add new path
visited[nx][ny] = depth;
path[nx][ny] = curPath;
heap.offer(new Node(nx, ny, depth));
}
}
return result.length() == 0 ? "impossible" : result;
}
int[] dx = new int[] {1, 0, 0, -1};
int[] dy = new int[] {0, -1, 1, 0};
char[] dir = new char[] {'d', 'l', 'r', 'u'};
int[][] visited;
String[][] path;
int col;
class Node {
int x, y;
int len;
Node(int x, int y, int len) {
this.x = x;
this.y = y;
this.len = len;
}
@Override
public int hashCode() {
return this.x * col + this.y;
}
@Override
public boolean equals(Object a) {
if(a instaceof Node) {
Node b = (Node) a;
return b.x == this.x && b.y == this.y;
}
return false;
}
}
}
test的图不是很大,dfs也能ac。
public class Solution {
public String findShortestWay(int[][] maze, int[] ball, int[] hole) {
/* use global variable: result and len
*/
visited = new boolean[maze.length][maze[0].length];
dfs(maze, ball[0], ball[1], hole, 1, "");
return result.length() == 0 ? "impossible" : result;
}
String result = "";
int len = Integer.MAX_VALUE;
boolean[][] visited;
int[] dx = new int[] {1, 0, 0, -1};
int[] dy = new int[] {0, -1, 1, 0};
char[] dir = new char[] {'d', 'l', 'r', 'u'};
// dfs
private void dfs(int[][] maze, int x, int y, int[] hole, int dep, String path) {
if(dep > len) return;
if(x == hole[0] && y == hole[1]) {
if(len > dep) {
len = dep; result = path;
}
if(len == dep && path.compareTo(result) < 0) {
len = dep; result = path;
}
return;
}
if(visited[x][y]) return;
visited[x][y] = true;
// 4 directions
for(int i = 0; i < 4; i++) {
int nx = x, ny = y;
while(nx + dx[i] >= 0 && nx + dx[i] < maze.length && ny + dy[i] >= 0 && ny + dy[i] < maze[0].length) {
// meet the wall
if(maze[nx + dx[i]][ny + dy[i]] == 1) break;
nx += dx[i]; ny += dy[i];
if(visited[nx][ny]) break;
if(nx == hole[0] && ny == hole[1]) break;
}
if(!visited[nx][ny]) dfs(maze, nx, ny, hole, dep + Math.abs(nx - x) + Math.abs(ny - y), path + dir[i]);
}
visited[x][y] = false;
}
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。