【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] 题目地址 ...
随机推荐
- python 浮点运算
print(format(float(a)/float(b),'.2f'))
- 移动端调试 — iPhone +Safari
1 开启iOS设备的调试功能 打开“设置”程序,进入“Safari”->“高级”页面开启“Web检查器”: 2 mac safari浏览器设置开发者工具 safari ->偏好设置 ...
- Emmet - 自动补全 JSX
VSCode 中设置"emmet.syntaxProfiles": {"javascript": "jsx"} reactjs - JSX ...
- (转)Intellij IDEA 自动生成 serialVersionUID
转自 : https://blog.csdn.net/tiantiandjava/article/details/8781776 Setting->Inspections->Seriali ...
- kafka consumer 自动提交 offset
org.apache.kafka.clients.consumer.KafkaConsumer#pollOnce private Map<TopicPartition, List<Cons ...
- lua-resty-kafka erro xxxx could not be resolved (3: Host not found)
问题:使用 lua-resty-kafka 向 kafka 发送数据失败,报错如下: slave6 could not be resolved (: Host not found) 配置信息: lua ...
- mybatis 如何关闭connection
1.前言 最开始操作数据库是使用jdbc操作数据库,每次创建一个连接都需要关闭连接,避免占用资源.比如 Class.forName("com.jdbc.mysql.Driver") ...
- 删除C:\Program Files (x86)\Common Files\baidu 等误装软件且正常模式删不掉的文件夹
---------方法一-------- C:\Program Files\Common Files\Baidu\BaiduProtect\1.1.0.26打开以上路径找到反注册程序uninst.ex ...
- .net日志的用法
public class Logs { private static Logger logger = LogManager.GetCurrentClassLogger(); //初始化日志类 /// ...
- windows10驱动精灵装完驱动后重启一直诊断修复中。。。完美解决
给公司电脑重装完系统后安装惯例开始打驱动,用的是驱动精灵,一切顺利,安装完成后重启,结果出问题,正在诊断我的电脑,无法诊断 然后我进入疑难解答 高级选项--启动设置 然后按7禁用驱动签名,成功进入系统 ...