[LC] 490. The Maze
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的更多相关文章
- [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 ...
- LeetCode 490. The Maze
原题链接在这里:https://leetcode.com/problems/the-maze/ 题目: There is a ball in a maze with empty spaces and ...
- 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 ...
- [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 ...
- 490. The Maze
原题链接:https://leetcode.com/articles/the-maze/ 这道题目是需要冲会员才能使用的,然而我个穷逼现在还是失业状态根本冲不起...以后如果把免费题目都刷完了的话,再 ...
- 【LeetCode】490. The Maze 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 日期 题目地址:https://leetcod ...
- [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 ...
- [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 ...
- [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 ...
随机推荐
- eclipse配置tomcat详细步骤
1.下载tomcat9并解压到D盘根目录下 2.Windows——>Preferences——>Server——>Runtime Environments——>Add 3.选择 ...
- 如何快速完成一份学术型PPT
大多人都知道有模板这么个东西. 但是拿到手却不会运用,所以只得急的找人帮忙. 毕竟一套模板的素材图表和你要展示的内容,很多都太不一样. 这种情况,怎么办?下面就来告诉你. 选中一套模版后,放大看看 ...
- 谈谈 Act 的依赖注入 和 模板输出 - 回答 drinkjava 同学提问
1. 背景 依赖注入工具 jBeanBox 的作者 drinkjava 同学最近在 Actframework gitee 项目 的提出了如下评论: 你这个DI工具的出发点可能有问题,一个MVC工具为什 ...
- 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 ...
- IDEA中使用Lombok时候,getter,setter注解不生效/每次重启后才生效
Relevance.java代码如下: import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; imp ...
- SEO优化技巧
一.搜索引擎工作原理 当我们在输入框中输入关键词,点击搜索或查询时,然后得到结果.深究其背后的故事,搜索引擎做了很多事情. 在搜索引擎网站,比如百度,在其后台有一个非常庞大的数据库,里面存储了海量的关 ...
- C语言笔记 16_标准库&stdio&stdlib&string&time
<stdio.h> 简介 stdio .h 头文件定义了三个变量类型.一些宏和各种函数来执行输入和输出. 库变量 下面是头文件 stdio.h 中定义的变量类型: 序号 变量 & ...
- 2017年3月16工作日志【mysql更改字段参数、java8 map()调用方法示例】
修改某个表的字段类型及指定为空或非空 >alter table 表名称 change 字段名称 字段名称 字段类型 [是否允许非空],变更字段名称及属性 >alter table 表名称 ...
- python机器学习(2:KNN算法)
1.KNN 简介:knn算法是监督学习中分类方法的一种.它又被叫k近邻算法,是一个概念极其简单而分类效果又很优秀的分类算法. 核心思想:在训练集中选出离输入的数据最近的k个数据,根据这k个数据的类别判 ...
- [LC] 373. Find K Pairs with Smallest Sums
You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. Define ...