当前位置:网站首页>模拟卷Leetcode【普通】1091. 二进制矩阵中的最短路径
模拟卷Leetcode【普通】1091. 二进制矩阵中的最短路径
2022-07-06 06:15:00 【邂逅模拟卷】
1091. 二进制矩阵中的最短路径
给你一个 n x n 的二进制矩阵 grid 中,返回矩阵中最短 畅通路径 的长度。如果不存在这样的路径,返回 -1 。
二进制矩阵中的 畅通路径 是一条从 左上角 单元格(即,(0, 0))到 右下角 单元格(即,(n - 1, n - 1))的路径,该路径同时满足下述要求:
路径途经的所有单元格都的值都是 0 。
路径中所有相邻的单元格应当在 8 个方向之一 上连通(即,相邻两单元之间彼此不同且共享一条边或者一个角)。
畅通路径的长度 是该路径途经的单元格总数。
示例 1:
输入:grid = [[0,1],[1,0]]
输出:2
示例 2:
输入:grid = [[0,0,0],[1,1,0],[1,1,0]]
输出:4
示例 3:
输入:grid = [[1,0,0],[1,1,0],[1,1,0]]
输出:-1
提示:
n == grid.length
n == grid[i].length
1 <= n <= 100
grid[i][j] 为 0 或 1
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/shortest-path-in-binary-matrix
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
代码:
from leetcode_python.utils import *
class Solution:
def __init__(self):
pass
@lru_cache(None)
def next(self,rowid,colid):
res = []
for row_id, col_id in ((rowid - 1, colid), (rowid, colid - 1), (rowid + 1, colid), (rowid, colid + 1),
(rowid - 1, colid-1), (rowid+1, colid - 1), (rowid + 1, colid+1), (rowid-1, colid + 1)):
if 0 <= row_id < self.height and 0 <= col_id < self.width and 0==self.grid[row_id][col_id]:
res.append([row_id, col_id])
return res
def shortestPathBinaryMatrix(self, grid: List[List[int]]) -> int:
if grid[0][0] or grid[-1][-1]:return -1
self.height = self.width = length = len(grid)
self.grid = grid
vis = set()
step = 1
now = [(0,0)]
while now:
next = []
for i,j in now:
if i==j==length-1:
return step
for x,y in self.next(i,j):
if (x,y) not in vis:
vis.add((x,y))
next.append((x,y))
step+=1
now=next
return -1
def test(data_test):
s = Solution()
data = data_test # normal
# data = [list2node(data_test[0])] # list转node
return s.shortestPathBinaryMatrix(*data)
def test_obj(data_test):
result = [None]
obj = Solution(*data_test[1][0])
for fun, data in zip(data_test[0][1::], data_test[1][1::]):
if data:
res = obj.__getattribute__(fun)(*data)
else:
res = obj.__getattribute__(fun)()
result.append(res)
return result
if __name__ == '__main__':
datas = [
[[[0,0,0],[1,1,0],[1,1,0]]],
]
for data_test in datas:
t0 = time.time()
print('-' * 50)
print('input:', data_test)
print('output:', test(data_test))
print(f'use time:{
time.time() - t0}s')
备注:
GitHub:https://github.com/monijuan/leetcode_python
CSDN汇总:模拟卷Leetcode 题解汇总_卷子的博客-CSDN博客
可以加QQ群交流:1092754609
leetcode_python.utils详见汇总页说明
先刷的题,之后用脚本生成的blog,如果有错请留言,我看到了会修改的!谢谢!
边栏推荐
- Company video accelerated playback
- 全程实现单点登录功能和请求被取消报错“cancelToken“ of undefined的解决方法
- isam2运行流程
- 黑猫带你学eMMC协议第10篇:eMMC读写操作详解(read & write)
- 曼哈顿距离和-打印菱形
- Basic knowledge of error
- [postman] dynamic variable (also known as mock function)
- [postman] collections - run the imported data file of the configuration
- 「 WEB测试工程师 」岗位一面总结
- Isam2 operation process
猜你喜欢

Application du Groupe Li dans gtsam
![Buuctf-[bjdctf2020]zjctf, but so (xiaoyute detailed explanation)](/img/c9/56fba6054c91f090de31463a8b4705.jpg)
Buuctf-[bjdctf2020]zjctf, but so (xiaoyute detailed explanation)

功能安全之故障(fault),错误(error),失效(failure)

黑猫带你学eMMC协议第10篇:eMMC读写操作详解(read & write)
![[postman] the monitors monitoring API can run periodically](/img/9e/3f6150290b868fc1160b6b01d0857e.png)
[postman] the monitors monitoring API can run periodically

VINS-Mono: A Robust and Versatile Monocular Visual-Inertial State Estimator

Application of Lie group in gtsam

PAT(乙级)2022年夏季考试

【Postman】Collections-运行配置之导入数据文件
![[wechat applet] build a development tool environment](/img/f6/51f97b1c927337b34c5b3a4207abb4.png)
[wechat applet] build a development tool environment
随机推荐
二维码的前世今生 与 六大测试点梳理
Buuctf-[[gwctf 2019] I have a database (xiaoyute detailed explanation)
LeetCode 739. 每日温度
D - How Many Answers Are Wrong
数据库隔离级别
D - How Many Answers Are Wrong
JWT-JSON WEB TOKEN
CoordinatorLayout+NestedScrollView+RecyclerView 上拉底部显示不全
Understanding of processes and threads
Nodejs realizes the third-party login of Weibo
【无App Push 通用测试方案
数字三角形模型 AcWing 1015. 摘花生
GTSAM中ISAM2和IncrementalFixedLagSmoother说明
Pat (Grade B) 2022 summer exam
What are the test sites for tunnel engineering?
误差的基本知识
Manage configuration using Nacos
VINS-Mono: A Robust and Versatile Monocular Visual-Inertial State Estimator
LeetCode 729. 我的日程安排表 I
[C language] qsort function