当前位置:网站首页>模拟卷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,如果有错请留言,我看到了会修改的!谢谢!
边栏推荐
猜你喜欢
把el-tree选中的数组转换为数组对象
What are the test sites for tunnel engineering?
【Postman】Monitors 监测API可定时周期运行
单元测试的意义
MPLS test report
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
Win10 cannot operate (delete, cut) files
调用链监控Zipkin、sleuth搭建与整合
Significance of unit testing
How to extract login cookies when JMeter performs interface testing
随机推荐
曼哈顿距离与曼哈顿矩形-打印回字型矩阵
Embedded point test of app
功能安全之故障(fault),错误(error),失效(failure)
Linux regularly backs up MySQL database
D - How Many Answers Are Wrong
[eolink] PC client installation
GTSAM中李群的運用
E - food chain
全程实现单点登录功能和请求被取消报错“cancelToken“ of undefined的解决方法
Réflexions sur la sécurité des données (réimpression)
Thoughts on data security (Reprint)
Request forwarding and redirection
10m25dcf484c8g (FPGA) amy-6m-0002 BGA GPS module
The latest 2022 review of "graph classification research"
【Postman】Monitors 监测API可定时周期运行
Basic knowledge of error
php使用redis实现分布式锁
Still worrying about how to write web automation test cases? Senior test engineers teach you selenium test case writing hand in hand
误差的基本知识
Accélération de la lecture vidéo de l'entreprise