[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 ...
随机推荐
- db2 基础语法
一.db2 基础 基本语法 注释:“--”(两个减号) 字符串连接:“||” 如set msg=’aaaa’||’bbbb’,则msg为’aaaabbbb’ 字符串的引用:‘’(一定用单引号),如果需 ...
- 使用commons-net做FTP功能的异常 java.lang.ClassNotFoundException: org.apache.oro.text.regex.Malformed
最近使用Apache的commons-net.jar做FTP上传下载功能,点击“上传”的时候报错,如下: java.lang.ClassNotFoundException: org.apache.or ...
- bitcoinj学习记录
一.密码学相关资料 使用Bouncy Castle生成数字签名.数字信封 ECDH and ECDSA(ECC椭圆曲线算法3) 数字签名算法RSA与 ECDSA的比较与分析 Java密码学 非对称加密 ...
- vim与程序员 vi/vim 的使用
vim与程序员 所有的 Unix Like 系统都会内建 vi 文书编辑器,其他的文书编辑器则不一定会存在. 但是目前我们使用比较多的是 vim 编辑器. vim 具有程序编辑的能力,可以主动的以 ...
- Linux之文档与目录结构 目录的相关操作 Linux的文件系统
Linux之文档与目录结构 Linux文件系统结构 Linux目录结构的组织形式和Windows有很大的不同.首先Linux没有“盘(C盘.D盘.E盘)”的概念.已经建立文件系统的硬盘分区被挂载到 ...
- [No0000182]Parallel Programming with .NET-Partitioning in PLINQ
Every PLINQ query that can be parallelized starts with the same step: partitioning. Some queries ma ...
- SHOW PROCESSLIST shows which threads are running 查看线程 解决瓶颈
小结: 1.查看全部线程: https://dev.mysql.com/doc/refman/8.0/en/show-processlist.html MySQL 8.0 Reference Manu ...
- 转:CSS设置HTML元素的高度与宽度的各种情况总结
1.元素不设宽度第一种情况:元素为文档流中元素<!-- 父元素宽度为100px --><div style="width:100px;"> < ...
- LeetCode 566 Reshape the Matrix 解题报告
题目要求 In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a ...
- 2018/05/02 每日一学Linux 之 .bash_profile和.bashrc的区别
最近一直在学习其他,导致博客就疏忽了,很不好(其实就是自己懒了......). -- 为什么要使用 .bash_profile和.bashrc ? 在平常的使用中,有些文件夹或者命令很长,在执行时需要 ...