每日一题 2596. 检查骑士巡视方案

难度:中等
在这里插入图片描述
很简单,从第 0 步开始模拟即可,唯一sb的就是测试用例中如果(0,0)处不为0的话就直接false,而不是去找0在哪
我的代码:

class Solution:
    def checkValidGrid(self, grid: List[List[int]]) -> bool:
        nx, ny = 0, 0
        step = 0
        n = len(grid)

        if grid[0][0] != 0:
            return False

        def j(x, y):
            if x < 0 or x >= n or y < 0 or y >= n:
                return False
            if grid[x][y] == step:
                return True
            return False

        while step < len(grid) ** 2 - 1:
            step += 1
            if j(nx + 1, ny + 2):
                nx += 1
                ny += 2
                continue
            if j(nx + 2, ny + 1):
                nx += 2
                ny += 1
                continue
            if j(nx + 1, ny - 2):
                nx += 1
                ny -= 2
                continue
            if j(nx + 2, ny - 1):
                nx += 2
                ny -= 1
                continue
            if j(nx - 1, ny - 2):
                nx -= 1
                ny -= 2
                continue
            if j(nx - 2, ny - 1):
                nx -= 2
                ny -= 1
                continue
            if j(nx - 2, ny + 1):
                nx -= 2
                ny += 1
                continue
            if j(nx - 1, ny + 2):
                nx -= 1
                ny += 2
                continue
            return False
        
        return 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)):
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

eyvr

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值