当前位置:网站首页>模拟卷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,如果有错请留言,我看到了会修改的!谢谢!
边栏推荐
- Buuctf-[[gwctf 2019] I have a database (xiaoyute detailed explanation)
- 公司視頻加速播放
- Left matching principle of joint index
- 使用Nacos管理配置
- Réflexions sur la sécurité des données (réimpression)
- Embedded point test of app
- (中)苹果有开源,但又怎样呢?
- Overview of three core areas of Mathematics: algebra
- 全链路压测:构建三大模型
- selenium源码通读·9 |DesiredCapabilities类分析
猜你喜欢
P问题、NP问题、NPC问题、NP-hard问题详解
MPLS test report
IDEA 新UI使用
曼哈顿距离和-打印菱形
VINS-Mono: A Robust and Versatile Monocular Visual-Inertial State Estimator
【Postman】Monitors 监测API可定时周期运行
Database - current read and snapshot read
【Postman】Collections-运行配置之导入数据文件
Seven imperceptible truths in software testing
Digital triangle model acwing 1015 Picking flowers
随机推荐
[postman] dynamic variable (also known as mock function)
LeetCode 1200. 最小绝对差
Isam2 operation process
E - 食物链
【API接口工具】postman-界面使用介绍
黑猫带你学UFS协议第4篇:UFS协议栈详解
MFC 动态创建的对话框及改变控件的大小和位置
[postman] collections - run the imported data file of the configuration
多线程应用的测试与调试
自定义指定路由上的Gateway过滤器工厂
Accélération de la lecture vidéo de l'entreprise
数学三大核心领域概述:几何
Fault, error, failure of functional safety
On weak network test of special test
LeetCode 732. 我的日程安排表 III
G - Supermarket
Isam2 and incrementalfixedlagsmooth instructions in gtsam
调用链监控Zipkin、sleuth搭建与整合
Eigen sparse matrix operation
10M25DCF484C8G(FPGA) AMY-6M-0002 BGA GPS模块