泉州企业免费建站,中国建筑网官网电工证证件查询,如何建设网站盈利,网站专题页面设计欣赏难度#xff1a;中等 很简单#xff0c;从第 0 步开始模拟即可#xff0c;唯一sb的就是测试用例中如果#xff08;0#xff0c;0#xff09;处不为0的话就直接false#xff0c;而不是去找0在哪 我的代码#xff1a;
class Solution:def checkValidGrid(self, grid: L…难度中等 很简单从第 0 步开始模拟即可唯一sb的就是测试用例中如果00处不为0的话就直接false而不是去找0在哪 我的代码
class Solution:def checkValidGrid(self, grid: List[List[int]]) - bool:nx, ny 0, 0step 0n len(grid)if grid[0][0] ! 0:return Falsedef j(x, y):if x 0 or x n or y 0 or y n:return Falseif grid[x][y] step:return Truereturn Falsewhile step len(grid) ** 2 - 1:step 1if j(nx 1, ny 2):nx 1ny 2continueif j(nx 2, ny 1):nx 2ny 1continueif j(nx 1, ny - 2):nx 1ny - 2continueif j(nx 2, ny - 1):nx 2ny - 1continueif j(nx - 1, ny - 2):nx - 1ny - 2continueif j(nx - 2, ny - 1):nx - 2ny - 1continueif j(nx - 2, ny 1):nx - 2ny 1continueif j(nx - 1, ny 2):nx - 1ny 2continuereturn Falsereturn True最好还是单独列出行动方式而不是写多个if判断
directions [[-2, 1],[-2, -1],[2, 1],[2, -1],[1, 2],[1, -2],[-1, 2],[-1, -2]
]
#或者这样
for j in ((-2,-1),(-2,1),(-1,2),(-1,-2),(1,2),(1,-2),(2,1),(2,-1)):