当前位置:网站首页>模拟卷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,如果有错请留言,我看到了会修改的!谢谢!
边栏推荐
- Expose the serial fraudster Liu Qing in the currency circle, and default hundreds of millions of Cheng Laolai
- 全程实现单点登录功能和请求被取消报错“cancelToken“ of undefined的解决方法
- Nodejs realizes the third-party login of Weibo
- leaflet 地图
- 职场进阶指南:大厂人必看书籍推荐
- JWT-JSON WEB TOKEN
- 2022 software testing workflow to know
- Réflexions sur la sécurité des données (réimpression)
- E - 食物链
- Seven imperceptible truths in software testing
猜你喜欢
[web security] nodejs prototype chain pollution analysis
[eolink] PC client installation
Function of contenttype
【C语言】字符串左旋
Buuctf-[gxyctf2019] no dolls (xiaoyute detailed explanation)
Understanding of processes and threads
曼哈顿距离与曼哈顿矩形-打印回字型矩阵
D - How Many Answers Are Wrong
Hypothesis testing learning notes
进程和线程的理解
随机推荐
Cognitive introspection
Postman核心功能解析-参数化和测试报告
F - True Liars (种类并查集+DP)
[wechat applet] build a development tool environment
LeetCode 732. 我的日程安排表 III
LeetCode 1200. 最小绝对差
GTSAM中李群的運用
B - The Suspects
Company video accelerated playback
Manage configuration using Nacos
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
G - Supermarket
RestTemplate、Feign实现Token传递
全链路压测:构建三大模型
Gtest之TEST宏的用法
Pat (Grade B) 2022 summer exam
Basic knowledge of error
[postman] dynamic variable (also known as mock function)
JWT-JSON WEB TOKEN
Cannot create PoolableConnectionFactory (Could not create connection to database server. 错误