当前位置:网站首页>模拟卷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,如果有错请留言,我看到了会修改的!谢谢!
边栏推荐
- Web界面元素的测试
- G - Supermarket
- Eigen稀疏矩阵操作
- Buuctf-[[gwctf 2019] I have a database (xiaoyute detailed explanation)
- Manhattan distance and Manhattan rectangle - print back font matrix
- 【Tera Term】黑猫带你学TTL脚本——嵌入式开发中串口自动化神技能
- 黑猫带你学UFS协议第8篇:UFS初始化详解(Boot Operation)
- Testing and debugging of multithreaded applications
- [wechat applet] build a development tool environment
- CoordinatorLayout+NestedScrollView+RecyclerView 上拉底部显示不全
猜你喜欢

Caused by:org.gradle.api.internal.plugins . PluginApplicationException: Failed to apply plugin

Isam2 operation process

Database isolation level

win10无法操作(删除、剪切)文件

Left matching principle of joint index

Basic knowledge of error

What are the test sites for tunnel engineering?

Usage of test macro of GTEST

【Postman】Collections-运行配置之导入数据文件

(中)苹果有开源,但又怎样呢?
随机推荐
[leetcode] day96 - the first unique character & ransom letter & letter ectopic word
【Postman】动态变量(也称Mock函数)
Isam2 and incrementalfixedlagsmooth instructions in gtsam
Commodity price visualization
Win10 cannot operate (delete, cut) files
【API接口工具】postman-界面使用介绍
在线问题与离线问题
LeetCode 731. 我的日程安排表 II
Linux regularly backs up MySQL database
Luogu p1460 [usaco2.1] healthy Holstein cows
Isam2 operation process
CoordinatorLayout+NestedScrollView+RecyclerView 上拉底部显示不全
[wechat applet] build a development tool environment
Testing of web interface elements
数学三大核心领域概述:代数
Construction and integration of Zipkin and sleuth for call chain monitoring
properties文件
黑猫带你学UFS协议第4篇:UFS协议栈详解
對數據安全的思考(轉載)
Cannot create PoolableConnectionFactory (Could not create connection to database server. 错误