当前位置:网站首页>Simulation volume leetcode [general] 1091 The shortest path in binary matrix
Simulation volume leetcode [general] 1091 The shortest path in binary matrix
2022-07-06 06:17:00 【Encounter simulation volume】
1091. Shortest path in binary matrix
To give you one n x n The binary matrix of grid in , Returns the shortest in the matrix Clear path The length of . If there is no such path , return -1 .
In a binary matrix Clear path It's a road from top left corner Cell ( namely ,(0, 0)) To The lower right corner Cell ( namely ,(n - 1, n - 1)) The path of , The path also meets the following requirements :
The value of all cells in the path is 0 .
All adjacent cells in the path should be in 8 One of the directions Upper connection ( namely , Two adjacent cells are different from each other and share an edge or corner ).
The length of the unblocked path Is the total number of cells that the path passes through .
Example 1:
Input :grid = [[0,1],[1,0]]
Output :2
Example 2:
Input :grid = [[0,0,0],[1,1,0],[1,1,0]]
Output :4
Example 3:
Input :grid = [[1,0,0],[1,1,0],[1,1,0]]
Output :-1
Tips :
n == grid.length
n == grid[i].length
1 <= n <= 100
grid[i][j] by 0 or 1
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/shortest-path-in-binary-matrix
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Code :
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 turn 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')
remarks :
GitHub:https://github.com/monijuan/leetcode_python
CSDN Summary : Simulation volume Leetcode Summary of questions _ Paper blog -CSDN Blog
You can add QQ Group communication :1092754609
leetcode_python.utils See the description on the summary page for details
First brush questions , Then generated by script blog, If there is any mistake, please leave a message , I see it will be revised ! thank you !
边栏推荐
- 10m25dcf484c8g (FPGA) amy-6m-0002 BGA GPS module
- [postman] collections configuration running process
- 通过修改style设置打印页样式
- P问题、NP问题、NPC问题、NP-hard问题详解
- 【微信小程序】搭建开发工具环境
- Expose the serial fraudster Liu Qing in the currency circle, and default hundreds of millions of Cheng Laolai
- 模拟卷Leetcode【普通】1296. 划分数组为连续数字的集合
- 多线程应用的测试与调试
- 黑猫带你学UFS协议第8篇:UFS初始化详解(Boot Operation)
- win10无法操作(删除、剪切)文件
猜你喜欢

Application du Groupe Li dans gtsam

【eolink】PC客户端安装

Hypothesis testing learning notes

Database isolation level

PAT(乙级)2022年夏季考试

全程实现单点登录功能和请求被取消报错“cancelToken“ of undefined的解决方法

【API接口工具】postman-界面使用介绍

Customize the gateway filter factory on the specified route

Manage configuration using Nacos

二维码的前世今生 与 六大测试点梳理
随机推荐
Expose the serial fraudster Liu Qing in the currency circle, and default hundreds of millions of Cheng Laolai
isam2运行流程
E - 食物链
Career advancement Guide: recommended books for people in big factories
Testing of web interface elements
【微信小程序】搭建开发工具环境
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
Leaflet map
[postman] collections - run the imported data file of the configuration
【Postman】测试(Tests)脚本编写和断言详解
[web security] nodejs prototype chain pollution analysis
Reading notes of effective managers
Caused by:org. gradle. api. internal. plugins . PluginApplicationException: Failed to apply plugin
Basic knowledge of error
还在为如何编写Web自动化测试用例而烦恼嘛?资深测试工程师手把手教你Selenium 测试用例编写
Idea new UI usage
模拟卷Leetcode【普通】1414. 和为 K 的最少斐波那契数字数目
Database - current read and snapshot read
LeetCode 731. 我的日程安排表 II
黑猫带你学UFS协议第4篇:UFS协议栈详解