【leetcode】1091. Shortest Path in Binary Matrix
题目如下:
In an N by N square grid, each cell is either empty (0) or blocked (1).
A clear path from top-left to bottom-right has length
kif and only if it is composed of cellsC_1, C_2, ..., C_ksuch that:
- Adjacent cells
C_iandC_{i+1}are connected 8-directionally (ie., they are different and share an edge or corner)C_1is at location(0, 0)(ie. has valuegrid[0][0])C_kis at location(N-1, N-1)(ie. has valuegrid[N-1][N-1])- If
C_iis located at(r, c), thengrid[r][c]is empty (ie.grid[r][c] == 0).Return the length of the shortest such clear path from top-left to bottom-right. If such a path does not exist, return -1.
Example 1:
Input: [[0,1],[1,0]]
Output: 2Example 2:
Input: [[0,0,0],[1,1,0],[1,1,0]]
Output: 4Note:
1 <= grid.length == grid[0].length <= 100grid[r][c]is0or1
解题思路:典型的BFS问题。从起点开始依次计算每个方格到达的最小值即可。这里用visit[i][j]记录从起点开始到(i,j)的最短路径,在BFS的过程中,可能有多条路径都能到达(i,j),因此只要有更短的走法能到达(i,j),就需要把(i,j)重新加入到队列中。
代码如下:
class Solution(object):
def shortestPathBinaryMatrix(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
if grid[0][0] == 1:
return -1
visit = []
val = []
for i in grid:
visit.append([0] * len(i))
val.append([0] * len(i))
visit[0][0] = 1
queue = [(0,0)]
direction = [(0,1),(0,-1),(1,0),(1,-1),(-1,1),(-1,-1),(1,1),(1,-1)]
while len(queue) > 0:
x,y = queue.pop(0)
for (i,j) in direction:
if x + i >= 0 and x+i < len(grid) and y + j >= 0 and y + j < len(grid[0]) \
and grid[x + i][y+j] == 0 and (visit[x+i][y+j] == 0 or visit[x+i][y+j] - 1 > visit[x][y]):
queue.append((x+i,y+j))
visit[x+i][y+j] = visit[x][y] + 1
return visit[-1][-1] if visit[-1][-1] > 0 else -1
【leetcode】1091. Shortest Path in Binary Matrix的更多相关文章
- 【LeetCode】847. Shortest Path Visiting All Nodes 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/shortest ...
- 【leetcode】1129. Shortest Path with Alternating Colors
题目如下: Consider a directed graph, with nodes labelled 0, 1, ..., n-1. In this graph, each edge is ei ...
- 【leetcode】1253. Reconstruct a 2-Row Binary Matrix
题目如下: Given the following details of a matrix with n columns and 2 rows : The matrix is a binary mat ...
- LeetCode 1091. Shortest Path in Binary Matrix
原题链接在这里:https://leetcode.com/problems/shortest-path-in-binary-matrix/ 题目: In an N by N square grid, ...
- 【leetcode】1293 .Shortest Path in a Grid with Obstacles
You are given an m x n integer matrix grid where each cell is either 0 (empty) or 1 (obstacle). You ...
- 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)
[LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...
- 【LeetCode】71. Simplify Path 解题报告(Python)
[LeetCode]71. Simplify Path 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)
[LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...
- 【LeetCode】111. Minimum Depth of Binary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 [LeetCode] 题目地址 ...
随机推荐
- hive_action
w pdf469 [不直接MR访问数据的工具 查询间接转化为MR] https://en.wikipedia.org/wiki/Apache_Hive Apache Hive supports a ...
- PJSIP库设置Via地址
好记性不如烂笔头,解决项目问题,调试代码跟踪到PJSIP 设置Via地址位置,记录下来,以备后用. PJSIP库在方法stateless_send_transport_cb中设置Via地址值,该方法在 ...
- 无法绕开的cut, awk, sed命令
linux命令的选项和选项后面的值的方式: 如果用 短选项, 选项值就放在短选项的后面, 如果用长选项, 值就用等于的方式. 最重要的是, 短选项后面的值, 跟短选项之间, 可以用空格, 也可以紧接着 ...
- 【转】python---方法解析顺序MRO(Method Resolution Order)<以及解决类中super方法>
[转]python---方法解析顺序MRO(Method Resolution Order)<以及解决类中super方法> MRO了解: 对于支持继承的编程语言来说,其方法(属性)可能定义 ...
- Selenium WebDriver 数据驱动测试框架
Selenium WebDriver 数据驱动测试框架,以QQ邮箱添加联系人为示例,测试框架结构如下图,详细内容请阅读吴晓华编著<Selenium WebDiver 实战宝典>: Obje ...
- C++实现,拓展中国剩余定理——解同余方程组(理论证明和代码实现)
拓展中国剩余定理 前言 记得半年前还写过关于拓展中国剩余定理的博客...不过那时对其理解还不是比较深刻,写的也比较乱. 于是趁学校复习之机,再来重温一下拓展中国剩余定理(以下简称ExCRT) 记得半年 ...
- python基础-5 冒泡排序、递归
上节总结 一.上节内容补充回顾 1.lambda func = lambda x,y: 9+x 参数: x,y 函数体:9+x ==> return 9+x func: 函数名 def func ...
- 解决浏览器打开网页后提示“dns_probe_possible”的方法
使用浏览器浏览网页时偶尔会遇到无法上网且浏览器提示:DNS_PROBE_POSSIBLE 一般有三种情况会导致这样的故障: 1.网络协议出现故障,也就是常说的 DNS 设置问题 2.浏览器中设置问题, ...
- [Python3] 006 列表的常用方法
目录 一个篱笆三个桩,list 有--好多个桩 1. 列表的小伙伴们 (1) 召唤小伙伴 (2) 我给"他们"分了个组 2. 小伙伴们的"才艺展示" (1) & ...
- Python自学第二天学习之《元组与字典》
一. 元组:tuple类型,元组一级元素 不能修改 不能增加 不能删除,是有序的. 格式 :tu=(1,2,3,4,5,6) 1.类型转换: #字符串转换成元组 b=“123” c=tuple(b) ...