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

《剑指offer》Java版--13.机器人的运动范围(BFS)

剑指offer原题13:机器人的运动范围
地上有一个m行n列的方格。一个机器人从坐标(0,0)的格子开始移动,它每次可以向左、右、上、下移动一格,但不能进入行坐标和列坐标的数位之和大于k的格子。例如,当k为18时,机器人能够进入方格(35,37),因为3+5+3+7=18。但它不能进入方格(35,38),因为3+5+3+8=19。请问该机器人能够到达多少个格子?
LeetCode原题:https://leetcode.cn/problems/ji-qi-ren-de-yun-dong-fan-wei-lcof/description/

class Solution {public int wardrobeFinishing(int m, int n, int cnt) {// ps:这是力扣题目有点不一样,方向只有两个。int[][] dir = new int[][]{{1, 0}, {0, 1}};boolean[][] vis = new boolean[m][n];LinkedList<Pair<Integer, Integer>> queue = new LinkedList<>();queue.push(new Pair<>(0, 0));int res = 0;while(queue.size() > 0) {Pair<Integer, Integer> pair = queue.pollFirst();vis[pair.getKey()][pair.getValue()] = true;res++;for(int i = 0; i < 2; ++i) {int nextX = pair.getKey() + dir[i][0];int nextY = pair.getValue() + dir[i][1];if(nextX >= 0 && nextX < m && nextY >= 0 && nextY < n&& !vis[nextX][nextY]&& digitalSum(nextX) + digitalSum(nextY) <= cnt) {queue.push(new Pair<>(nextX, nextY));}}}return res;}private int digitalSum(int x) {int res = 0;while(x > 0) {res += x % 10;x /= 10;}return res;}
}

时间复杂度O(NM)

空间复杂度O(NM)

http://www.lryc.cn/news/269259.html

相关文章:

  • 基于流程挖掘的保险理赔优化策略实践
  • Docker五 | DockerFile
  • 2023年度总结:技术旅程的杨帆远航⛵
  • SpringBoot+AOP+Redis 防止重复请求提交
  • 偷流量、端口占用、网络负载高、socket创建释放异常等Android高阶TCP/IP网络问题定位思路
  • 《人人都能用英语》学习笔记
  • NFC与ZigBee技术在智慧农业物联网监测系统中的应用
  • k8s-cni网络 10
  • 听GPT 讲Rust源代码--src/tools(27)
  • 经济危机下,我们普通人如何翻身?2024创业新风口,适合普通人的创业项目
  • 深入浅出图解C#堆与栈 C# Heap(ing) VS Stack(ing) 第五节 引用类型复制问题及用克隆接口ICloneable修复
  • python中基本元素的pop函数
  • MPLS动态协议LDP配置示例
  • JS调用栈:为何会栈溢出
  • 代码随想Day52 | 300.最长递增子序列、674. 最长连续递增序列、718. 最长重复子数组
  • 使用 pytest 相关特性重构 appium_helloworld
  • 猪目标检测数据集VOC格式600张
  • Pandas中concat的用法
  • 【C++】引用详解
  • 平时的一些思考内容
  • AIGC时代下,结合ChatGPT谈谈儿童教育
  • Java中的锁(一)
  • CSS-SVG-环形进度条
  • 英语中修饰头发的形容词顺序是怎么样的(加补充)
  • python的WebSocket编程详解,案例群聊系统实现
  • flutter学习-day22-使用GestureDetector识别手势事件
  • uni-app tabbar组件
  • 【Midjourney】Midjourney根据prompt提示词生成人物图片
  • Oracle 拼接字符串
  • 探究公有云中的巨人:深入分析大数据产品的架构设计