The Maze
Input 1: a maze represented by a 2D array 0 0 1 0 0
0 0 0 0 0
0 0 0 1 0
1 1 0 1 1
0 0 0 0 0 Input 2: start coordinate (rowStart, colStart) = (0, 4)
Input 3: destination coordinate (rowDest, colDest) = (4, 4) Output: true
Explanation: One possible way is : left -> down -> left -> down -> right -> down -> right.

Input 1: a maze represented by a 2D array 0 0 1 0 0
0 0 0 0 0
0 0 0 1 0
1 1 0 1 1
0 0 0 0 0 Input 2: start coordinate (rowStart, colStart) = (0, 4)
Input 3: destination coordinate (rowDest, colDest) = (3, 2) Output: false
Explanation: There is no way for the ball to stop at the destination.

- There is only one ball and one destination in the maze.
- Both the ball and the destination exist on an empty space, and they will not be at the same position initially.
- The given maze does not contain border (like the red rectangle in the example pictures), but you could assume the border of the maze are all walls.
- The maze contains at least 2 empty spaces, and both the width and height of the maze won't exceed 100.
DFS
对于dfs,如果当前“决定”对后续有影响,可以使用第16行这种方法不断递归。
class Solution {
public boolean hasPath(int[][] maze, int[] start, int[] destination) {
int m = maze.length, n = maze[].length;
boolean[][] visited = new boolean[m][n];
return dfs(maze, visited, start, destination);
}
private boolean dfs(int[][] maze, boolean[][] visited, int[] start, int[] destination) {
int row = start[], col = start[];
if (row < || row >= maze.length || col < || col >= maze[].length || visited[row][col]) return false;
visited[row][col] = true;
if (row == destination[] && col == destination[]) return true;
int[] directions = { , , , -, };
for (int i = ; i < directions.length - ; i++) {
int[] newStart = roll(maze, start[], start[], directions[i], directions[i + ]);
if (dfs(maze, visited, newStart, destination)) return true;
}
return false;
}
private int[] roll(int[][] maze, int row, int col, int rowInc, int colInc) {
while (canRoll(maze, row + rowInc, col + colInc)) {
row += rowInc;
col += colInc;
}
return new int[]{row, col};
}
private boolean canRoll(int[][] maze, int row, int col) {
if (row >= maze.length || row < || col >= maze[].length || col < || maze[row][col] == ) return false;
return true;
}
}
BFS
class Solution {
public boolean hasPath(int[][] maze, int[] start, int[] destination) {
Deque<int[]> queue = new ArrayDeque<>();
boolean[][] visited = new boolean[maze.length][maze[].length];
queue.offer(start);
while (!queue.isEmpty()) {
int[] cur = queue.poll();
int row = cur[], col = cur[];
if (row == destination[] && col == destination[]) {
return true;
}
if (visited[row][col]) {
continue;
}
visited[row][col] = true;
int[] directions = { , , , -, };
for (int i = ; i < directions.length - ; i++) {
int[] newStart = roll(maze, row, col, directions[i], directions[i + ]);
queue.offer(newStart);
}
}
return false;
}
private int[] roll(int[][] maze, int row, int col, int rowInc, int colInc) {
while (canRoll(maze, row + rowInc, col + colInc)) {
row += rowInc;
col += colInc;
}
return new int[] { row, col };
}
private boolean canRoll(int[][] maze, int row, int col) {
if (row >= maze.length || row < || col >= maze[].length || col < || maze[row][col] == )
return false;
return true;
}
}
The Maze的更多相关文章
- Backtracking algorithm: rat in maze
Sept. 10, 2015 Study again the back tracking algorithm using recursive solution, rat in maze, a clas ...
- (期望)A Dangerous Maze(Light OJ 1027)
http://www.lightoj.com/volume_showproblem.php?problem=1027 You are in a maze; seeing n doors in fron ...
- 1204. Maze Traversal
1204. Maze Traversal A common problem in artificial intelligence is negotiation of a maze. A maze ...
- uva705--slash maze
/*这道题我原本是将斜线迷宫扩大为原来的两倍,但是在这种情况下对于在斜的方向上的搜索会变的较容易出错,所以参考了别人的思路后将迷宫扩展为原来的3倍,这样就变成一般的迷宫问题了*/ #include&q ...
- HDU 4048 Zhuge Liang's Stone Sentinel Maze
Zhuge Liang's Stone Sentinel Maze Time Limit: 10000/4000 MS (Java/Others) Memory Limit: 32768/327 ...
- Borg Maze(MST & bfs)
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9220 Accepted: 3087 Descrip ...
- poj 3026 bfs+prim Borg Maze
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9718 Accepted: 3263 Description The B ...
- HDU 4035:Maze(概率DP)
http://acm.split.hdu.edu.cn/showproblem.php?pid=4035 Maze Special Judge Problem Description When w ...
- POJ 3026 : Borg Maze(BFS + Prim)
http://poj.org/problem?id=3026 Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- Borg Maze 分类: POJ 2015-07-27 15:28 5人阅读 评论(0) 收藏
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9971 Accepted: 3347 Description The B ...
随机推荐
- MessagePack Java 0.6.X 可选字段
你可添加一个新的字段来保持可用性.在新字段中使用 @Optional 注解. @Message public static class MyMessage { public String na ...
- Battle ships HDU - 5093二分匹配
Battle shipsHDU - 5093 题目大意:n*m的地图,*代表海洋,#代表冰山,o代表浮冰,海洋上可以放置船舰,但是每一行每一列只能有一个船舰(类似象棋的車),除非同行或者同列的船舰中间 ...
- return返回方法值:狮子玩具
public class Lion { String color ="黄色"; public void run(){ System.out.println("正在以0.1 ...
- React Redux 与胖虎
这是一篇详尽的 React Redux 扫盲文. 对 React Redux 已经比较熟悉的同学可以直接看 <React Redux 与胖虎他妈>. 是什么 React Redux 是 R ...
- 主线程 Looper.loop() 死循环为何不会ANR
先看下 ActivityThread 中的这段代码: 而 loop() 方法中,存在一个死循环: public static void loop() { ... ... ... for (;;) { ...
- ActiveXObject常用方法
function getusername() { var WshNetwork = new ActiveXObject("WScript.Network"); alert(&quo ...
- 黑马lavarel教程---10、lavarel模型关联
黑马lavarel教程---10.lavarel模型关联 一.总结 一句话总结: 1.模型关联比较方便,一次定义,后面都可以使用 2.关联关系 使用动态属性进行调用 1.一对多,多对多实例? 一对多: ...
- Ubuntu16.04下安装最新版本的CMake
当前最新版CMake为3.9.1.. Ubuntu中更新cmake到最新版本,过程如下: 1. 卸载已经安装的旧版的CMake[非必需] apt-get autoremove cmake 2. 文 ...
- PHPstrom中关闭提示信息
设置里面搜索parameter name hints
- 访问H2数据库的SpringBoot工程
JDK:1.8.0_212 IDE:STS4(Spring Tool Suit4 Version: 4.3.2.RELEASE) 工程下载:https://files.cnblogs.com/file ...