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 ...
随机推荐
- mysql优化(上)
磁盘组成 和 磁盘读取过程 尽量减少 i/o 操作. 表结构设计:(1)三范式 : 原子性(不可拆分).唯一性(不能有完全相同的数据).无冗余性(不能有多余的数据),对于冗余性说明一下:拿订单 ...
- SpringBoot项目中,Mybatis的使用
项目中使用MyBatis的地方很少,可以说是基本不用,慕课网上面这个项目介绍给也就是一些比较简单的使用例子,我用JPA比较的多,MyBatis有两种使用方式 1.导入MyBatis的依赖 <de ...
- HGOI 20191029am 题解
Promblem A 小G的字符串 给定$n,k$,构造一个长度为$n$,只能使用$k$种小写字母的字符串. 要求相邻字符不能相同且$k$种字母都要出现 输出字典序最小的字符串,无解输出$-1$. 对 ...
- Trie树(c++实现)——转载自jihite的博客
Trie树(c++实现) 原理 先看个例子,存储字符串abc.ab.abm.abcde.pm可以利用以下方式存储 上边就是Trie树的基本原理:利用字串的公共前缀来节省存储空间,最大限度的减少无谓 ...
- android自定义键盘(解决弹出提示的字体颜色问题)
最近准备要做一个项目,需要用到自定义小键盘来确保安全,而且还需要精确获得用户点击键盘时的落点位置.力度.指尖接触屏幕的面积等参数. 在写自定义键盘的时候,用到了国内网上的一些代码,出处是 向先人致敬! ...
- Spring Boot教程(四十一)LDAP来管理用户信息(1)
LDAP简介 LDAP(轻量级目录访问协议,Lightweight Directory Access Protocol)是实现提供被称为目录服务的信息服务.目录服务是一种特殊的数据库系统,其专门针对读 ...
- log4j 多进程配置要注意的
多进程写日志文件 方法一: 解决log4j公用配置文件,多进程同时写同一个log文件,因存在操作系统pv操作问题, 导致部分日志丢失.解决方案是不同的进程写不同的log文件 测试于:Log4j 1.2 ...
- C++入门经典-例8.8-虚继承
1:以前讲到从CBird类和CFish类派生子类CWaterBird时,在CWaterBird类中将存在两个CAnimal类的复制.那么如何在派生CWaterBird类时使其只存在一个CAnimal基 ...
- 20165213 Exp7 网络欺诈防范
Exp7 网络欺诈防范 一. 实践内容 简单应用SET工具建立冒名网站 1.首先使用sudo vi /etc/apache2/ports.conf 进行查看listen的端口号,若不是80改为80. ...
- SpringBoot Thymeleaf 配置多个Template Locations
@Configuration public class ThymeleafConfigration { @Bean public SpringResourceTemplateResolver firs ...