题目如下:

In an N by N square grid, each cell is either empty (0) or blocked (1).

clear path from top-left to bottom-right has length k if and only if it is composed of cells C_1, C_2, ..., C_k such that:

  • Adjacent cells C_i and C_{i+1} are connected 8-directionally (ie., they are different and share an edge or corner)
  • C_1 is at location (0, 0) (ie. has value grid[0][0])
  • C_k is at location (N-1, N-1) (ie. has value grid[N-1][N-1])
  • If C_i is located at (r, c), then grid[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: 2

Example 2:

Input: [[0,0,0],[1,1,0],[1,1,0]]
Output: 4

Note:

  1. 1 <= grid.length == grid[0].length <= 100
  2. grid[r][c] is 0 or 1

解题思路:典型的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的更多相关文章

  1. 【LeetCode】847. Shortest Path Visiting All Nodes 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/shortest ...

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

  3. 【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 ...

  4. LeetCode 1091. Shortest Path in Binary Matrix

    原题链接在这里:https://leetcode.com/problems/shortest-path-in-binary-matrix/ 题目: In an N by N square grid, ...

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

  6. 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)

    [LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...

  7. 【LeetCode】71. Simplify Path 解题报告(Python)

    [LeetCode]71. Simplify Path 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  8. 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)

    [LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...

  9. 【LeetCode】111. Minimum Depth of Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 [LeetCode] 题目地址 ...

随机推荐

  1. C3P0 详解

    定义: C3P0是一个开源的JDBC连接池,目前使用它的开源项目有Hibernate,Spring等. 数据库连接池的基本思想就是为数据库连接建立一个“缓冲池”.预先在缓冲池中放入一定数量的连接,当需 ...

  2. buildroot

    http://buildroot.uclibc.org/downloads/snapshots/buildroot-snapshot.tar.bz2 简介 buildroot是一个Makefiles和 ...

  3. Linux内核调试方法总结之死锁问题分析

    死锁问题分析 死锁就是多个进程(线程)因为等待别的进程已占有的自己所需要的资源而陷入阻塞的一种状态,死锁状态一旦形成,进程本身是解决不了的,需要外在的推动,才能解决,最重要的是死锁不仅仅影响进程业务, ...

  4. jenkins打包ios 报错rror: No signing certificate "iOS Distribution" found: No "iOS Distribution...

    错误提示如图: error: No signing certificate "iOS Distribution" found: No "iOS Distribution& ...

  5. error: exportArchive: You don’t have permission to save the file “HelloWorld.ipa” in the folder “HelloWorld”.

    成功clean环境和生成archive文件之后,最后一步导出ipa包,遇到了权限问题: you don’t have permission to save the file “HelloWorld.i ...

  6. day41—JavaScript运动的停止条件

    转行学开发,代码100天——2018-04-26 前面学过了JavaScript运动的两种常用情形:匀速运动与缓冲运动.在这两种运动的处理过程中最大的区别在于速度的处理和到达目标点的处理. 即本文需要 ...

  7. hbase的API

    import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.*; import org.apache.had ...

  8. nginx windows安装基础

    nginx在 window上运行需要1.17.3以上. 官方文件https://nginx.org/en/docs/windows.html nginx启动: 1:进入安装目录,双击nginx.exe ...

  9. 【HANA系列】SAP HANA DB 和SAP HANA studio version查看

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA DB 和SAP ...

  10. 安全运维 - Linux系统攻击回溯

    入侵排查思路 (1)- 日志分析 日志分析 默认日志路径: /var/log 查看日志配置情况: more /etc/rsyslog.conf 重要日志: 登录失败记录: /var/log/btmp ...