[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 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:
- 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/BFS, 需要注意的是每次append某个方向时要加入一个while loop使得一直碰到边缘或者墙壁, 也就是'1' 为止, 然后以此作为条件去判断是否append进入
stack或者queue里面.
1. Constraints
1) 基本都是valid
2. Ideas
DFS/BFS T; O(m*n) S: O(m*n)
3. Code
class Solution:
def maze(self, maze, start, destination):
lr, lc, visited = len(maze), len(maze[0]), set([tuple(start)])
queue = collections.deque([tuple(start)])
# stack = [tuple(start)] # DFS, stack.pop() 去代替queue.popleft()即可
dirs = [(0,-1), (0,1), (-1,0), (1,0)]
while queue:
pr, pc = queue.popleft()
if [pr,pc] == destination:
return True
for c1, c2 in dirs:
nr, nc = pr + c1, pc + c2
while 0 <= nr <lr and 0 <= nc < lc and maze[nr][nc] != 1:
nr += c1
nc += c2
nr -= c1
nc -= c2
if maze[nr][nc] == 0 and (nr, nc) not in visited:
queue.append((nr, nc))
visited.add((nr, nc))
return False
[LeetCode] 490. The Maze_Medium tag: BFS/DFS的更多相关文章
- [LeetCode] 207 Course Schedule_Medium tag: BFS, DFS
There are a total of n courses you have to take, labeled from 0 to n-1. Some courses may have prereq ...
- [LeetCode] 133. Clone Graph_ Medium tag: BFS, DFS
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...
- [LeetCode] 529. Minesweeper_ Medium_ tag: BFS
Let's play the minesweeper game (Wikipedia, online game)! You are given a 2D char matrix representin ...
- [LeetCode] 690. Employee Importance_Easy tag: BFS
You are given a data structure of employee information, which includes the employee's unique id, his ...
- [LeetCode] 733. Flood Fill_Easy tag: BFS
An image is represented by a 2-D array of integers, each integer representing the pixel value of the ...
- [LeetCode] 130. Surrounded Regions_Medium tag: DFS/BFS
Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...
- [LeetCode] 849. Maximize Distance to Closest Person_Easy tag: BFS
In a row of seats, 1 represents a person sitting in that seat, and 0 represents that the seat is emp ...
- [LeetCode] 513. Find Bottom Left Tree Value_ Medium tag: BFS
Given a binary tree, find the leftmost value in the last row of the tree. Example 1: Input: 2 / \ 1 ...
- [LeetCode] 821. Shortest Distance to a Character_Easy tag: BFS
Given a string S and a character C, return an array of integers representing the shortest distance f ...
随机推荐
- day_5.14 py 飞机大战Demo
飞机未完,继续做 2018-5-14 21:05:45 明天继续 循环里面的坑; 删除列表元素后循环了打印的不一样,主要是比如相邻的删除了,33,44 删除33 循环一次后44跑到33位置 ...
- 三剑客之awk
简介 awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大.简单来说awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再 ...
- echarts pie 图表 显示的label 中内容 字体大小自定义
option = { tooltip: { trigger: 'item', formatter: "{a} <br/>{b}: {c} ({d}%)" }, lege ...
- .NET Core 中 IOptions 有什么用
我只发现IOptions的一个用处——方便了在.NET Core应用程序中使用强类型配置. 如果没有IOptions,使用强类型配置需要自己解决下面2个问题: 1)将配置文件(比如appsetting ...
- window10装机 nvem简介,针对于 联想R720系列
1.nvem格式的硬盘比较新,传统的老毛桃,大白菜,并不能有效使用. 2.对于镜像类 goust类,仍然具备 硬盘兼容性差(对于gpt,MAR,uefi并不够智能)成功率极低,容易丢失一些关键文件,造 ...
- screen基本用法
当某些命令执行时间过长或者某些程序关闭shell后自动断开时,就能使用screen 1.安装yum -y install screen 2.输入screen命令进入screen控制台 3.输入scre ...
- 【Java】一台服务器配置多个Tomcat
需求缘由 最近接收了一个新的工具业务:ipublish发布系统,刚接手这个业务的时候,发现每次发布新的代码 需要到群里告知大家,我要停服务几分钟,准备更新代码啦.这尼玛 哪个公司都不敢这么牛逼的和用户 ...
- 为单实例数据库配置ASM
环境配置沿用搭建RAC的环境配置 配置ASM可以在数据库软件安装之前进行,也可以在安装完数据库软件配置数据库前进行 [root@rac01 Packages]# cd /etc/yum.repos.d ...
- 《Mysql DML语句》
1:DISTINCT 用于去重,但是需要注意的是,它是用于所有列的,也就是说,除非指定的列全部相同,否则所有的行都会被检索出来. 2:ORDER BY 用于排序,但是应该注意的是,它因该是 SELEC ...
- pandas基础
1.相关库导入 2.创建数据结构 pandas 有两个重要的数据结构: Series 和 DataFrame 创建Series数组,代表一行或一列 创建DataFrame ,代表二维数组 第一种方式: ...