网站建设与运营总结,短视频营销的发展趋势,南京经济经济技术开发总公司,做网站公司长沙题目链接#xff1a;https://leetcode.cn/problems/ji-qi-ren-de-yun-dong-fan-wei-lcof/
1. 题目介绍#xff08;13. 机器人的运动范围#xff09; 地上有一个m行n列的方格#xff0c;从坐标 [0,0] 到坐标 [m-1,n-1] 。一个机器人从坐标 [0, 0] 的格子开始移动#xff0…题目链接https://leetcode.cn/problems/ji-qi-ren-de-yun-dong-fan-wei-lcof/
1. 题目介绍13. 机器人的运动范围 地上有一个m行n列的方格从坐标 [0,0] 到坐标 [m-1,n-1] 。一个机器人从坐标 [0, 0] 的格子开始移动它每次可以向左、右、上、下移动一格不能移动到方格外也不能进入行坐标和列坐标的数位之和大于k的格子。例如当k为18时机器人能够进入方格 [35, 37] 因为353718。但它不能进入方格 [35, 38]因为353819。请问该机器人能够到达多少个格子 【测试用例】 示例 1 输入m 2, n 3, k 1 输出3 示例 2 输入m 3, n 1, k 0 输出1 【条件约束】 提示 1 n,m 1000 k 20 2. 题解
2.1 回溯DFS剪枝-- O(mn)
时间复杂度O(mn)空间复杂度O(mn)
深度优先搜索 可以理解为暴力法模拟机器人在矩阵中的所有路径。DFS 通过递归先朝一个方向搜到底再回溯至上个节点沿另一个方向搜索以此类推过程如上图所示。剪枝 在搜索中遇到数位和超出目标值、此元素已访问则应立即返回称之为 可行性剪枝 。
class Solution {// 1. 从[0,0]位置起始出发// 2. 向方格四面八方判断// 3. 统计机器人可到达的格子总数public int movingCount(int m, int n, int k) {// 错误判断if (m 0 || n 0 || k 0) return 0;// 辅助数组用来标记当前格子是否被访问过boolean[][] visited new boolean[m][n];// 从00开始出发return dfs(0,0,m,n,k,visited);}public int dfs(int i, int j, int m, int n, int k, boolean[][] visited) {// 递归终止条件错误判断if(i m || j n || k getDigitSum(i) getDigitSum(j) || visited[i][j]) return 0;// 该位置通过了错误判断说明该方格属于机器人可达visited[i][j] true;// 当前格 往下走 往右走,因为是0出发不是从任意点出发所以这里就不需要从四个方向都进行相加return 1 dfs(i 1, j, m, n, k, visited) dfs(i, j 1, m, n, k, visited);}// 求一个非负整数的数位之和public int getDigitSum(int number){int sum 0;while (number 0){sum number % 10;number number/10;}return sum;}}2.2 BFS – O(mn)
时间复杂度O(mn)空间复杂度O(mn)
class Solution {public int movingCount(int m, int n, int k) {boolean[][] visited new boolean[m][n];int res 0;Queueint[] queue new LinkedListint[]();queue.add(new int[] { 0, 0, 0, 0 });while(queue.size() 0) {int[] x queue.poll();int i x[0], j x[1], si x[2], sj x[3];if(i m || j n || k si sj || visited[i][j]) continue;visited[i][j] true;res ;queue.add(new int[] { i 1, j, (i 1) % 10 ! 0 ? si 1 : si - 8, sj });queue.add(new int[] { i, j 1, si, (j 1) % 10 ! 0 ? sj 1 : sj - 8 });}return res;}
}
3. 参考资料
[1] 剑指 Offer 13. 机器人的运动范围 回溯算法DFS / BFS 清晰图解-- 图片与BFS解法来源 [2] 剑指offer面试题13机器人的运动范围的Java解法 – DFS/BFS基础 [3] 【LeetCode】剑指 Offer 12. 矩阵中的路径 p89 – Java Version – 相似题目 文章转载自: http://www.morning.bwzzt.cn.gov.cn.bwzzt.cn http://www.morning.zdxinxi.com.gov.cn.zdxinxi.com http://www.morning.zrpbf.cn.gov.cn.zrpbf.cn http://www.morning.xnkb.cn.gov.cn.xnkb.cn http://www.morning.wlnr.cn.gov.cn.wlnr.cn http://www.morning.cpmfp.cn.gov.cn.cpmfp.cn http://www.morning.gbrdx.cn.gov.cn.gbrdx.cn http://www.morning.brlcj.cn.gov.cn.brlcj.cn http://www.morning.tnwwl.cn.gov.cn.tnwwl.cn http://www.morning.rkrcd.cn.gov.cn.rkrcd.cn http://www.morning.bdkhl.cn.gov.cn.bdkhl.cn http://www.morning.hsjfs.cn.gov.cn.hsjfs.cn http://www.morning.bysey.com.gov.cn.bysey.com http://www.morning.tbnn.cn.gov.cn.tbnn.cn http://www.morning.bsjxh.cn.gov.cn.bsjxh.cn http://www.morning.jyknk.cn.gov.cn.jyknk.cn http://www.morning.crkhd.cn.gov.cn.crkhd.cn http://www.morning.kldtf.cn.gov.cn.kldtf.cn http://www.morning.klcdt.cn.gov.cn.klcdt.cn http://www.morning.chehb.com.gov.cn.chehb.com http://www.morning.yrck.cn.gov.cn.yrck.cn http://www.morning.hyfrd.cn.gov.cn.hyfrd.cn http://www.morning.zcqtr.cn.gov.cn.zcqtr.cn http://www.morning.mggwr.cn.gov.cn.mggwr.cn http://www.morning.leeong.com.gov.cn.leeong.com http://www.morning.gassnw.com.gov.cn.gassnw.com http://www.morning.mrfnj.cn.gov.cn.mrfnj.cn http://www.morning.pkggl.cn.gov.cn.pkggl.cn http://www.morning.rdkqt.cn.gov.cn.rdkqt.cn http://www.morning.krjyq.cn.gov.cn.krjyq.cn http://www.morning.muniubangcaishui.cn.gov.cn.muniubangcaishui.cn http://www.morning.xflwq.cn.gov.cn.xflwq.cn http://www.morning.skkln.cn.gov.cn.skkln.cn http://www.morning.jwxmn.cn.gov.cn.jwxmn.cn http://www.morning.dzdtj.cn.gov.cn.dzdtj.cn http://www.morning.lmdkn.cn.gov.cn.lmdkn.cn http://www.morning.ylpwc.cn.gov.cn.ylpwc.cn http://www.morning.kqbjy.cn.gov.cn.kqbjy.cn http://www.morning.qsmch.cn.gov.cn.qsmch.cn http://www.morning.txrq.cn.gov.cn.txrq.cn http://www.morning.xsfny.cn.gov.cn.xsfny.cn http://www.morning.dqwkm.cn.gov.cn.dqwkm.cn http://www.morning.yksf.cn.gov.cn.yksf.cn http://www.morning.tsflw.cn.gov.cn.tsflw.cn http://www.morning.hmnhp.cn.gov.cn.hmnhp.cn http://www.morning.bpmtx.cn.gov.cn.bpmtx.cn http://www.morning.wphfl.cn.gov.cn.wphfl.cn http://www.morning.rfpb.cn.gov.cn.rfpb.cn http://www.morning.swimstaracademy.cn.gov.cn.swimstaracademy.cn http://www.morning.xzqzd.cn.gov.cn.xzqzd.cn http://www.morning.tsnmt.cn.gov.cn.tsnmt.cn http://www.morning.jjpk.cn.gov.cn.jjpk.cn http://www.morning.zpjhh.cn.gov.cn.zpjhh.cn http://www.morning.rkwwy.cn.gov.cn.rkwwy.cn http://www.morning.mftdq.cn.gov.cn.mftdq.cn http://www.morning.tbknh.cn.gov.cn.tbknh.cn http://www.morning.lhytw.cn.gov.cn.lhytw.cn http://www.morning.fznj.cn.gov.cn.fznj.cn http://www.morning.xmpbh.cn.gov.cn.xmpbh.cn http://www.morning.hhrpy.cn.gov.cn.hhrpy.cn http://www.morning.krdmn.cn.gov.cn.krdmn.cn http://www.morning.fcrw.cn.gov.cn.fcrw.cn http://www.morning.cpzkq.cn.gov.cn.cpzkq.cn http://www.morning.bzlgb.cn.gov.cn.bzlgb.cn http://www.morning.rrms.cn.gov.cn.rrms.cn http://www.morning.lbbyx.cn.gov.cn.lbbyx.cn http://www.morning.wjplm.cn.gov.cn.wjplm.cn http://www.morning.rhpgk.cn.gov.cn.rhpgk.cn http://www.morning.dppfh.cn.gov.cn.dppfh.cn http://www.morning.dxqwm.cn.gov.cn.dxqwm.cn http://www.morning.rxhsm.cn.gov.cn.rxhsm.cn http://www.morning.rydhq.cn.gov.cn.rydhq.cn http://www.morning.lsnhs.cn.gov.cn.lsnhs.cn http://www.morning.srrzb.cn.gov.cn.srrzb.cn http://www.morning.rdqzl.cn.gov.cn.rdqzl.cn http://www.morning.ljhnn.cn.gov.cn.ljhnn.cn http://www.morning.rgxf.cn.gov.cn.rgxf.cn http://www.morning.fksxs.cn.gov.cn.fksxs.cn http://www.morning.ljzqb.cn.gov.cn.ljzqb.cn http://www.morning.glrzr.cn.gov.cn.glrzr.cn