地上有一个m行和n列的方格。一个机器人从坐标0,0的格子开始移动,每一次只能向左,右,上,下四个方向移动一格,但是不能进入行坐标和列坐标的数位之和大于k的格子。 例如,当k为18时,机器人能够进入方格(35,37),因为3+5+3+7 = 18。但是,它不能进入方格(35,38),因为3+5+3+8 = 19。请问该机器人能够达到多少个格子?

class Solution {
private:
int sumOfNumber(int x){
int sum = ;
while(x){
sum += x % ;
x = x / ;
}
return sum;
}
int movingCountDFSHelper(int threshold, int rows, int cols, int r, int c,
vector<vector<bool>>& visited) {
if (r < || r >= rows || c < || c >= cols || visited[r][c]
|| threshold < sumOfNumber(r) + sumOfNumber(c)) {
return ;
}
visited[r][c] = true;
return + movingCountDFSHelper(threshold, rows, cols, r + , c, visited)
+ movingCountDFSHelper(threshold, rows, cols, r - , c, visited)
+ movingCountDFSHelper(threshold, rows, cols, r, c - , visited)
+ movingCountDFSHelper(threshold, rows, cols, r, c + , visited);
}
public:
int movingCount(int threshold, int rows, int cols) {
if (rows <= || cols <= )
return ;
vector<vector<bool>> visited(rows, vector<bool>(cols, false));
return movingCountDFSHelper(threshold, rows, cols, , , visited); }
};

剑指offer66:机器人的活动范围的更多相关文章

  1. 剑指Offer66题的总结、目录

    原文链接 剑指Offer每日6题系列终于在今天全部完成了,从2017年12月27日到2018年2月27日,历时两个月的写作,其中绝大部分的时间不是花在做题上,而是花在写作上,这个系列不适合大神,大牛, ...

  2. 剑指offer66:机器人的运动范围

    1 题目描述 地上有一个m行和n列的方格.一个机器人从坐标0,0的格子开始移动,每一次只能向左,右,上,下四个方向移动一格,但是不能进入行坐标和列坐标的数位之和大于k的格子. 例如,当k为18时,机器 ...

  3. 机器人的运动范围 剑指offer66题

    include "stdafx.h" #include<vector> #include<algorithm> #include<string> ...

  4. 剑指Offer——机器人的运动范围

    题目描述: 地上有一个m行和n列的方格.一个机器人从坐标0,0的格子开始移动,每一次只能向左,右,上,下四个方向移动一格,但是不能进入行坐标和列坐标的数位之和大于k的格子. 例如,当k为18时,机器人 ...

  5. 剑指offer--48.机器人的运动范围

    这道题不是要求走一趟最多走多少,而是最多走多少,WA几次才想通. ------------------------------------------------------------------- ...

  6. 剑指offer——2

    剑指offer 机器人的运动范围 数组的应用和递归 package com.wang.test; public class Myso { /** * 题目描述 * 地上有一个m行和n列的方格.一个机器 ...

  7. 【Java】 剑指offer(12) 机器人的运动范围

    本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集   题目 地上有一个m行n列的方格.一个机器人从坐标(0, 0)的格子开始移 ...

  8. Go语言实现:【剑指offer】机器人的运动范围

    该题目来源于牛客网<剑指offer>专题. 地上有一个m行和n列的方格.一个机器人从坐标0,0的格子开始移动,每一次只能向左,右,上,下四个方向移动一格,但是不能进入行坐标和列坐标的数位之 ...

  9. 剑指 Offer 13. 机器人的运动范围 + 深搜 + 递归

    剑指 Offer 13. 机器人的运动范围 题目链接 package com.walegarrett.offer; /** * @Author WaleGarrett * @Date 2020/12/ ...

随机推荐

  1. JdbcTemplate详解

    1.JdbcTemplate操作数据库 Spring对数据库的操作在jdbc上面做了深层次的封装,使用spring的注入功能,可以把DataSource注册到JdbcTemplate之中.同时,为了支 ...

  2. Even and Odd Functions

    \subsection{Even and Odd Functions} For a function $f$ in the form $y=f(x)$, we describe its type of ...

  3. Moving XML/BI Publisher Components Between Instances

    As it is well known fact that XMLPublisher stores the metadata and physical files for templates and ...

  4. LeetCode149:Max Points on a Line

    题目: Given n points on a 2D plane, find the maximum number of points that lie on the same straight li ...

  5. GitHub设置使用SSH Key,用TortoiseGit进行Clone仓库

    GitHub设置使用SSH Key的好处就是可以使用SSH连接,并且提交代码的时候可以不用输入密码,免密提交. 生成SSH Key 这里我们使用PuTTYgen来生成公钥(Public Key),私钥 ...

  6. (php)实现万年历

    <?php //修改页面编码 header("content-type:text/html;charset=utf-8"); //获取当前年 $year=$_GET['y'] ...

  7. c# 锁 Interlocked 操作

    //定义原子变量 ; //原子级别+1值,如果>=0,说明当前锁为空,可以执行,避免重复执行 ) { if (_serverThread == null || (_serverThread.Is ...

  8. .net core初试 --- 控制台程序

    .net core这个名字对.net程序员来说都不陌生了,但貌似圈子里真正有开发经验的并不多,关键是公司的项目没需求. 今天我就趁着不忙上手玩了玩,搞明白了一些东西,心中也有了十万个为什么.那么现在与 ...

  9. 基于C#语言MVC框架NPOI控件导出Excel表数据

    控件bin文件下载地址:https://download.csdn.net/download/u012949335/10610726@{ ViewBag.Title = "dcxx" ...

  10. Visual Studio在Win10中以管理员方式运行

    在Win10中运行VS不是默认以管理员程序运行的,需要手动设置 第一步:将VS快捷方式设置为以管理员身份运行 第二部 在C:\Program Files\Microsoft Visual Studio ...