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.
class Solution {
public boolean hasPath(int[][] maze, int[] start, int[] destination) {
int row = maze.length;
int col = maze[0].length;
Queue<Cell> queue = new LinkedList<>();
boolean[][] visited = new boolean[row][col];
queue.offer(new Cell(start[0], start[1]));
visited[start[0]][start[1]] = true;
int[][] directions = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
while (!queue.isEmpty()) {
Cell cur = queue.poll();
int curX = cur.x;
int curY = cur.y;
for (int[] direction: directions) {
int newX = curX;
int newY = curY;
while (newX >= 0 && newX < row && newY >= 0 && newY < col && maze[newX][newY] == 0) {
newX += direction[0];
newY += direction[1];
}
newX -= direction[0];
newY -= direction[1];
if (visited[newX][newY]) {
continue;
}
if (newX == destination[0] && newY ==destination[1]) {
return true;
}
queue.offer(new Cell(newX, newY));
visited[newX][newY] = true;
}
}
return false;
}
} class Cell {
int x;
int y;
public Cell(int x, int y) {
this.x = x;
this.y = y;
}
}

[LC] 490. The Maze的更多相关文章

  1. [LeetCode] 490. The Maze 迷宫

    There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...

  2. LeetCode 490. The Maze

    原题链接在这里:https://leetcode.com/problems/the-maze/ 题目: There is a ball in a maze with empty spaces and ...

  3. LC 499. The Maze III 【lock,hard】

    There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...

  4. [LC] 505. The Maze II

    There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...

  5. 490. The Maze

    原题链接:https://leetcode.com/articles/the-maze/ 这道题目是需要冲会员才能使用的,然而我个穷逼现在还是失业状态根本冲不起...以后如果把免费题目都刷完了的话,再 ...

  6. 【LeetCode】490. The Maze 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 日期 题目地址:https://leetcod ...

  7. [LeetCode] 490. The Maze_Medium tag: BFS/DFS

    There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...

  8. [LeetCode] 499. The Maze III 迷宫 III

    There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...

  9. [LeetCode] 505. The Maze II 迷宫 II

    There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...

随机推荐

  1. 计算机网络(3): ICMP报文

  2. 爬虫防止浏览器防止debug处理

    方式一(基于你会前端,我比较喜欢这种方式) #复制html页面 #复制其中的js,css(css可有可无,如果加css和不加css情况不一样,网页可能做了css反爬处理) #全局搜索debug or ...

  3. 201512-2 消除类游戏 Java

    思路: 用二维数组,对于每一个棋子,向右看三个,向下看三个,如果相等则置为负数,最后遍历输出. import java.util.Scanner; public class Main { public ...

  4. PAT A1133 Splitting A Linked List (25) [链表]

    题目 Given a singly linked list, you are supposed to rearrange its elements so that all the negative v ...

  5. 2×c列联表|多组比例简式|卡方检验|χ2检验与连续型资料假设检验

    第四章 χ2检验 χ2检验与连续型资料假设检验的区别? 卡方检验的假设检验是什么? 理论值等于实际值 何条件下卡方检验的需要矫正?如何矫正? 卡方检验的自由度如何计算? Df=k-1而不是n-1 卡方 ...

  6. eclipse创建文件携带作者时间

    windows–>preference Java–>Code Style–>Code Templates code–>new Java files 编辑它 ${filecomm ...

  7. 自动按键的Sendkeys工具的下载和使用

    大家好! 下面介绍一款自动按键的小工具:Sendkeys 下载地址 Sendkeys.rar 按键脚本的书写规则如下: 启动本工具后,在工具中打开一个脚本文件,然后在工具中按下Ctrl+A全选所有脚本 ...

  8. Python3.7离线安装Requests无法正常使用问题

    继续搬砖...... 春节前,克服了网络受限的情况下离线安装Python库文件问题,传送门如下: https://www.cnblogs.com/mrgavin/p/12202214.html htt ...

  9. Faraday Future,FF2019年一季度前完成第一阶段5亿美元左右的A+轮融资,2019年年底前完成7亿美元的Pre-IPO轮融资,2020IPO

    FF2019年一季度前完成第一阶段5亿美元左右的A+轮融资,2019年年底前完成7亿美元的Pre-IPO轮融资,2020IPO 区块链公司先行宣布将对FF进行投资.EVAIO(中文名:伊娃)公司 跨链 ...

  10. Python笔记_第三篇_面向对象_1.面向对象的基本概念和作用域

    1. 软件编程的实质: 软件编程就是将我们的思维转变成计算机能够识别语言的一个过程.重要的是思想,代码技术反而次要.因此思想也是最难的,突破固定的思想是关键 2. 什么是面向过程: * 自上而下顺序执 ...