当前位置: 首页 > news >正文

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

难度:中等
在这里插入图片描述
很简单,从第 0 步开始模拟即可,唯一sb的就是测试用例中如果(0,0)处不为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)):
http://www.lryc.cn/news/165795.html

相关文章:

  • 第二章 进程与线程 三、进程控制
  • 【云原生进阶之PaaS中间件】第二章Zookeeper-3.2架构详解
  • K8S:kubectl陈述式、声明式资源管理及金丝雀部署
  • docker容器日志管理
  • Oracel ORA-22992 错误的解决方法
  • CrossOver 23 正式发布:可在 Mac 上运行部分 DX12 游戏
  • 一、Mediasoup源码介绍
  • ⑧ 嵌套路由配置
  • 【ppt技巧】将幻灯片里的图片背景设置为透明
  • rrweb入门
  • OSCP系列靶场-Esay-Vegeta1保姆级
  • AJAX学习笔记9 搜索联想自动补全
  • Docker启动失败报错Failed to start Docker Application Container Engine解决方案
  • 安装React脚手架
  • 想要精通算法和SQL的成长之路 - 受限条件下可到达节点的数目
  • 加快项目开发进度常用5种方法
  • leetcode做题笔记136. 只出现一次的数字
  • vuex 模拟异步调用
  • error:Failed building wheel for XXX
  • 【linux命令讲解大全】112.Linux 系统管理工具:dpkg-statoverride 和 dstat 的使用介绍
  • ffmpeg草稿
  • 熵 | 无线通信知识
  • 黑马JVM总结(七)
  • Vue3核心语法一
  • 5.11.Webrtc接口的设计原理
  • 2022年09月 C/C++(八级)真题解析#中国电子学会#全国青少年软件编程等级考试
  • Vue3 监听属性-watch
  • JWT安全
  • LabVIEW利用人工神经网络辅助进行结冰检测
  • Linux安装MySQL8.0