There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolling up, down, left or right, but it won't stop rolling until hitting a wall. When the ball stops, it could choose the next direction.
Given the ball's start position, the destination and the maze, determine whether the ball could stop at the destination.
The maze is represented by a binary 2D array. 1 means the wall and 0 means the empty space. You may assume that the borders of the maze are all walls. The start and destination coordinates are represented by row and column indexes.
Example 1
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.

 
Example 2
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.

 
Note:
  1. There is only one ball and one destination in the maze.
  2. Both the ball and the destination exist on an empty space, and they will not be at the same position initially.
  3. 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.
  4. 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的更多相关文章

  1. Backtracking algorithm: rat in maze

    Sept. 10, 2015 Study again the back tracking algorithm using recursive solution, rat in maze, a clas ...

  2. (期望)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 ...

  3. 1204. Maze Traversal

    1204.   Maze Traversal A common problem in artificial intelligence is negotiation of a maze. A maze ...

  4. uva705--slash maze

    /*这道题我原本是将斜线迷宫扩大为原来的两倍,但是在这种情况下对于在斜的方向上的搜索会变的较容易出错,所以参考了别人的思路后将迷宫扩展为原来的3倍,这样就变成一般的迷宫问题了*/ #include&q ...

  5. 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 ...

  6. Borg Maze(MST & bfs)

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9220   Accepted: 3087 Descrip ...

  7. poj 3026 bfs+prim Borg Maze

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9718   Accepted: 3263 Description The B ...

  8. HDU 4035:Maze(概率DP)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=4035 Maze Special Judge Problem Description   When w ...

  9. POJ 3026 : Borg Maze(BFS + Prim)

    http://poj.org/problem?id=3026 Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  10. Borg Maze 分类: POJ 2015-07-27 15:28 5人阅读 评论(0) 收藏

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9971   Accepted: 3347 Description The B ...

随机推荐

  1. js中prototype和constructor的认识

    最初对js中 object.constructor 的认识: 我们都知道,在JS中有一个function的东西.一般人们叫它函数.比如下面的代码 function Person(name)    {  ...

  2. nodeJS环境搭建

    1. Node.js是什么 1.1 Node.js是一个基于Chrome V8引擎的[JavaScript运行环境]. Node.js使用了一个事件驱动.非阻塞式I/O 的模型. 1.2 Node.j ...

  3. delphi中Tkbmmemtable数据转成SQL脚本

    unit UMemtableToSql; interface uses SysUtils, Classes, DB, kbmMemTable, Variants, Dialogs, SuperObje ...

  4. HDU 6154 CaoHaha's staff(2017中国大学生程序设计竞赛 - 网络选拔赛)

    题目代号:HDU 6154 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6154 CaoHaha's staff Time Limit: 2000/1 ...

  5. axios的post传参时,将参数转为form表单格式

    import axios from 'axios'; import alert from './alert.js'; import Qs from 'qs' //引入qs 时axios的自带模块 le ...

  6. jQuery文档操作之修改操作

    replaceWith() 语法: $(selector).replaceWith(content); 将所有匹配的元素替换成指定的string.js对象.jQuery对象. // 将所有的h5标题替 ...

  7. SVN如何处理包含@2x or @3x的图片文件

    一般iOS图片文件都会包含@2x,@3x之类的字符比如icon@2x,icon@3x,当你在svn命令行中add或是delete的时候总是报错说file does not exit之类的错误,其实之类 ...

  8. JVM 监控工具——jconsole

    [官方文档]:Using JConsole 1. 简介 JConsole是一个内置Java性能分析器,可以从命令行或在GUI shell中运行.您可以轻松地使用JConsole(或者,它更高端的“近亲 ...

  9. TCP最大报文段长度MSS

    最大报文段长度(MSS)表示TCP传往另一端的最大数据库的长度.当一个连接建立时,连接的双方都要通告各自需要接收的MSS选项(MSS选项只能出现在SYN报文段中).如果一方不接收来自另一方的MSS值, ...

  10. 【Blog怎么玩】什么叫EntryName友好地址名?

    EntryName友好地址名 其实就是你这个页面的别名,如果设置的好的话,在SEO搜索中,会更清晰的显示出来. 好处1:清晰的URL 比如我有一篇展示可爱兔子的博文,我想让它的网址清晰的显示出来,而不 ...