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 ...
随机推荐
- 【Python之路】异步IO
线程:CPU基本执行单元,可以与同属一个进程的其他线程共享资源,线程是属于进程的. 进程:资源单元,进程一般由程序.数据集.进程控制块三部分组成.一个进程默认有一个主线程, GIL:用于在进程中对所有 ...
- Laravel dingo,HTTP的请求头(accept)无法携带版本号的解决方法
Laravel dingo,HTTP的请求头(accept)无法携带版本号的解决方法 2017年9月6日 原创分享 zencodex 使用 Laravel dingo 做api开发时,涉及 A ...
- luogu3812 【模板】线性基
Code: #include <cstdio> #include <algorithm> #define ll long long #define N 64 #define s ...
- MessagePack Java 0.6.X 可选字段
你可添加一个新的字段来保持可用性.在新字段中使用 @Optional 注解. @Message public static class MyMessage { public String na ...
- 使用Hexo和Github搭建博客站
本人电脑系统为window 10专业工作站版,64位 相关步骤: 1.安装Node.js和配置好Node.js环境,打开cmd命令行,成功界面如下 2.安装Git和配置好Git环境,安装成功的象征就是 ...
- sql注入的基本小知识
load_fie('') into outfile '' into dumpfile('') 堆叠注入 ;insert into liunx密码读取 /etc/passwd /etc/shadow W ...
- LVS配置
今天面试时,突然被面试官问到怎样用shell命令搞定某个文件夹下java代码行数的统计. 想了一下,基本思路就是找到这个文件夹下面的所有java文件,然后每个文件统计一下代码,外层套个for循环,叠加 ...
- IDEA找回Run Dashboard
特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...
- python接口自动化:python3.6中import Crypto.Hash报错的解决方案
一:问题 python3.6中算法加密引入包Crypto报错,即便安装了: pip install crypto pip install pycrypto pip install pycryptodo ...
- SpringBoot Thymeleaf 配置多个Template Locations
@Configuration public class ThymeleafConfigration { @Bean public SpringResourceTemplateResolver firs ...