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

  1. Input 1: a maze represented by a 2D array
  2.  
  3. 0 0 1 0 0
  4. 0 0 0 0 0
  5. 0 0 0 1 0
  6. 1 1 0 1 1
  7. 0 0 0 0 0
  8.  
  9. Input 2: start coordinate (rowStart, colStart) = (0, 4)
  10. Input 3: destination coordinate (rowDest, colDest) = (4, 4)
  11.  
  12. Output: true
  13. Explanation: One possible way is : left -> down -> left -> down -> right -> down -> right.

Example 2

  1. Input 1: a maze represented by a 2D array
  2.  
  3. 0 0 1 0 0
  4. 0 0 0 0 0
  5. 0 0 0 1 0
  6. 1 1 0 1 1
  7. 0 0 0 0 0
  8.  
  9. Input 2: start coordinate (rowStart, colStart) = (0, 4)
  10. Input 3: destination coordinate (rowDest, colDest) = (3, 2)
  11.  
  12. Output: false
  13. 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/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

  1. class Solution:
  2. def maze(self, maze, start, destination):
  3. lr, lc, visited = len(maze), len(maze[0]), set([tuple(start)])
  4. queue = collections.deque([tuple(start)])
  5. # stack = [tuple(start)] # DFS, stack.pop() 去代替queue.popleft()即可
  6. dirs = [(0,-1), (0,1), (-1,0), (1,0)]
  7. while queue:
  8. pr, pc = queue.popleft()
  9. if [pr,pc] == destination:
  10. return True
  11. for c1, c2 in dirs:
  12. nr, nc = pr + c1, pc + c2
  13. while 0 <= nr <lr and 0 <= nc < lc and maze[nr][nc] != 1:
  14. nr += c1
  15. nc += c2
  16. nr -= c1
  17. nc -= c2
  18. if maze[nr][nc] == 0 and (nr, nc) not in visited:
  19. queue.append((nr, nc))
  20. visited.add((nr, nc))
  21. return False

[LeetCode] 490. The Maze_Medium tag: BFS/DFS的更多相关文章

  1. [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 ...

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

  3. [LeetCode] 529. Minesweeper_ Medium_ tag: BFS

    Let's play the minesweeper game (Wikipedia, online game)! You are given a 2D char matrix representin ...

  4. [LeetCode] 690. Employee Importance_Easy tag: BFS

    You are given a data structure of employee information, which includes the employee's unique id, his ...

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

  6. [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 ...

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

  8. [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 ...

  9. [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 ...

随机推荐

  1. html表格的基本用法

    表格的基本用法 1.<!DOCTYPE html><html><head lang="en"> <meta charset="U ...

  2. 拓展 NLog 优雅的输送日志到 Logstash

    在上上篇博客通过对aspnetcore启动前配置做了一些更改,以及对nlog进行了自定义字段,可以把请求记录输送到mysql,正式情况可能不会这么部署.因为近期也在学习elk,所以就打算做一个实例,结 ...

  3. df

    hdu 1052 Tian Ji -- The Horse Racing (2011-08-26 08:32:51) 转载▼ 标签: 杂谈 分类: acm杂谈 Tian Ji -- The Horse ...

  4. bytes和str的区别与转换

    bytes和str的区别 1.英文 b'alex'的表现形式与str没什么两样 2.中文 b'\xe4\xb8\xad'这是一个汉字在utf-8的bytes表现形式 3.中文 b'\xce\xd2'这 ...

  5. org.hibernate.HibernateException: connnection proxy not usable after transaction completion

    今天yuan男神的程序报了这个错, getHibernateTemplate().saveOrUpdate(obj); getHibernateTemplate().flush(); getHiber ...

  6. 访问Google的办法

    访问Google的办法 http://www.liu16.com/g.html http://ac.scmor.com/ https://www.elastic.co/guide/en/elastic ...

  7. 【凸包板题】Gym - 101484E E. Double Fence

    http://codeforces.com/gym/101484/problem/E 题解 凸包板题 #define _CRT_SECURE_NO_WARNINGS #include<cmath ...

  8. 安装MAC的ReactNative环境

    brew install node brew install watchman npm config set registry https://registry.npm.taobao.org --gl ...

  9. Srt字幕文件解析

    // // ViewController.m // 字幕解析 // // Created by admin on 2018/8/30. // Copyright © 2018年 admin. All ...

  10. JavaScript面向对象之函数构造器的理解

    1,在使用函数创建类时,函数本身也被称为该类的构造器,该类的构造器方法,该类的构造方法,该类的构造函数等等. 2,注意构造器方法是没有返回值的,当创建该类的实例时,必须调用该类的构造方法. 3,获取构 ...