【leetcode 66题 之 13 机器人的运动范围】
题目描述
地上有一个m行和n列的方格。一个机器人从坐标0,0的格子开始移动,每一次只能向左,右,上,下四个方向移动一格,但是不能进入行坐标和列坐标的数位之和大于k的格子。 例如,当k为18时,机器人能够进入方格(35,37),因为3+5+3+7 = 18。但是,它不能进入方格(35,38),因为3+5+3+8 = 19。请问该机器人能够达到多少个格子?
题目解析:
典型的回溯问题,可借鉴前一题(矩阵路径)的思路。
分析一下题目,当行数和列数按位求和不大于阈值(threshold),那当前机器人是可以运动的,这个是重要的判断条件。
如何回溯?
假设机器人进入(0,0)点,首先要判断当前的点是否满足上述条件,若满足,则count + 1,并且,将走过的位置标记为已经走过,以免后面回溯回来的时候重复计算。当前的点访问完毕之后,就需要访问上、下、左、右四个方向的点,也就是row - 1、row + 1、col - 1、col + 1。最终机器人会访问所有的点,并给出符合条件的格子总数。
代码分析:
根据模块化的思想,首先,我需要一个计算给定整数按位求和的函数:getNumberSum,代码如下:
def getNumberSum(self, num):"""本函数用于处理:求出输入进来的数按位求和:param num::return:"""n = 0while num != 0:n += num % 10num = num // 10return n
其次,我还需要一个判断是否满足threshold的函数:allow,代码如下:
def allow(self, threshold, row, col):"""本函数用于判断: 行数按位求和值+列数按位求和值是否大于阈值:param threshold::param row::param col::return:"""sum_row = self.getNumberSum(row)sum_col = self.getNumberSum(col)if (sum_row + sum_col) > threshold:return Falseelse:return True
另外,还需要一个递归查找函数:find,代码如下:
def find(self, threshold, visited_matrix, rows, cols, row, col):"""本函数用于递归查找,并计算个数:param threshold::param visited_matrix::param rows::param cols::param row::param col::return:"""count = 0if row >= 0 and col >= 0 and row < rows and col < cols and self.allow(threshold,row,col) and visited_matrix[row][col] != 0:visited_matrix[row][col] = 0 # 如果访问过就设为0count += 1count += self.find(threshold, visited_matrix, rows, cols, row - 1, col) + \self.find(threshold, visited_matrix, rows, cols, row + 1, col) +\self.find(threshold, visited_matrix, rows, cols, row, col - 1) +\self.find(threshold, visited_matrix, rows, cols, row, col + 1)return count
最后,加一个函数统一封装入口:movingCount,代码如下:
def movingCount(self, threshold, rows, cols):# write code hereif (rows < 0 or cols < 0 or threshold < 0):return 0visited_matrix = [[1] * cols for i in range(rows)]return self.find(threshold,visited_matrix, rows, cols, 0, 0)
解释一下 :[[1] * cols for i in range(rows)]
rows = 4
cols = 3
这行代码的返回值是:
[[1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1]
]
很强大的功能,如果有时间可以研究一下其源码。PS:我们研究源码的目的不仅仅是为了看它是如何实现的,写代码也是写策略,很多代码策略就是实际生活中策略的抽象。
完整代码及测试用例和输出如下:
# -*- coding:utf-8 -*-
class Solution:def getNumberSum(self, num):"""本函数用于处理:求出输入进来的数按位求和:param num::return:"""n = 0while num != 0:n += num % 10num = num // 10return ndef allow(self, threshold, row, col):"""本函数用于判断: 行数按位求和值+列数按位求和值是否大于阈值:param threshold::param row::param col::return:"""sum_row = self.getNumberSum(row)sum_col = self.getNumberSum(col)if (sum_row + sum_col) > threshold:return Falseelse:return Truedef find(self, threshold, visited_matrix, rows, cols, row, col):"""本函数用于递归查找,并计算个数:param threshold::param visited_matrix::param rows::param cols::param row::param col::return:"""count = 0if row >= 0 and col >= 0 and row < rows and col < cols and self.allow(threshold,row,col) and visited_matrix[row][col] != 0:visited_matrix[row][col] = 0 # 如果访问过就设为0count += 1count += self.find(threshold, visited_matrix, rows, cols, row - 1, col) + \self.find(threshold, visited_matrix, rows, cols, row + 1, col) +\self.find(threshold, visited_matrix, rows, cols, row, col - 1) +\self.find(threshold, visited_matrix, rows, cols, row, col + 1)return countdef movingCount(self, threshold, rows, cols):# write code hereif (rows < 0 or cols < 0 or threshold < 0):return 0visited_matrix = [[1] * cols for i in range(rows)]return self.find(threshold,visited_matrix, rows, cols, 0, 0)if __name__ == '__main__':s = Solution()print s.movingCount(9,12,11)
本代码尚需优化,行数列数设置40+,就会报运行时错误,有心者可以谷歌,解决方案暂时不展示啦。过牛客网测试用例没问题。
【leetcode 66题 之 13 机器人的运动范围】相关推荐
- LeetCode 剑指Offer 13 机器人的运动范围
LeetCode 剑指Offer 13 机器人的运动范围 题目 解题 解题一:深度优先搜索 解题二:广度优先搜索 解题三:动态规划 题目 解题 深度优先和广度优先解题思路参考: 剑指 Offer 13 ...
- Leetcode 剑指 Offer 13. 机器人的运动范围 (每日一题 20210906)
地上有一个m行n列的方格,从坐标 [0,0] 到坐标 [m-1,n-1] .一个机器人从坐标 [0, 0] 的格子开始移动,它每次可以向左.右.上.下移动一格(不能移动到方格外),也不能进入行坐标和列 ...
- leetcode机器人运动范围Java_【LeetCode】面试题13. 机器人的运动范围
题目: 思路: 矩阵搜索,因为可以上下左右移动,判断机器人是否能够走到位置x,只需要判断机器人能否走到它的上下左右的位置,并且x点满足条件.但是判断x点时,它的上下左右如果还没有判断过呢?这时陷入了瓶 ...
- 每日一道Leetcode - 剑指 Offer 13. 机器人的运动范围【DFS|BFS】
DFS: Python版本 class Solution:def movingCount(self, m: int, n: int, k: int) -> int:def dfs(i,j,si, ...
- LeetCode 面试题13. 机器人的运动范围
我的LeetCode:https://leetcode-cn.com/u/ituring/ 我的LeetCode刷题源码[GitHub]:https://github.com/izhoujie/Alg ...
- 【LeetCode】剑指 Offer 13. 机器人的运动范围
[LeetCode]剑指 Offer 13. 机器人的运动范围 文章目录 [LeetCode]剑指 Offer 13. 机器人的运动范围 package offer;public class Solu ...
- LeetCode刷题记录13——705. Design HashSet(easy)
LeetCode刷题记录13--705. Design HashSet(easy) 目录 LeetCode刷题记录13--705. Design HashSet(easy) 前言 题目 语言 思路 源 ...
- LeetCode-剑指 Offer 13. 机器人的运动范围
剑指 Offer 13. 机器人的运动范围 思路一:利用dfs深度优先搜索 首先确定递归搜索的终止条件 1:i,j超出范围 2:数位之和大于k 3:(i,j)被访问过 接下来进行dfs 可以进行一个d ...
- 面试题13. 机器人的运动范围
面试题13. 机器人的运动范围 思路:dfs+判断 class Solution { public:int movingCount(int m, int n, int k) {res = 0;dfs( ...
最新文章
- PHP new self什么,php new self()是什么意思
- 线程和进程之间的联系----基本概念
- Linux 命令之 file 命令-识别文件类型
- LeetCode 1825. 求出 MK 平均值(set + queue)
- ios 支付验证 php,PHP验证IOS原生支付是否成功(代码全篇)
- ftp上传乱码_ftp上传与wordpres常规基本设置
- 聊城初中计算机考试分数线,2021年聊城中考最低录取线是多少,聊城历年中考分数线统计...
- mysql root账号_修改mysql root账号密码
- windows 运行banana
- 一次 HashSet 所引起的并发问题 1
- Java笔试题黑棋围白棋_2016恒生电子笔试题_软件测试笔试题100精讲_恒生电子笔试题目(2)...
- 前端开发IDE---VSCode前端开发环境配置
- 最短路算法——Floyd-Warshall
- 从零开始学android:环境搭建
- 软件工程师必须掌握的知识结构
- 浅层砂过滤器的原理是什么,滤料是什么,需要不需要定期?
- 飘飘微课计算机百度云,数学微课_百度云资源_盘多多如风搜_盘搜搜_哎哟喂啊...
- 【Linux】查看linux是centos还是ubuntu的方法
- 【真题分享】2021京东春招java开发面试
- 【smoj 1167】松果