《剑指offer》第十三题(机器人的运动范围)
// 面试题:机器人的运动范围
// 题目:地上有一个m行n列的方格。一个机器人从坐标(0, 0)的格子开始移动,它
// 每一次可以向左、右、上、下移动一格,但不能进入行坐标和列坐标的数位之和
// 大于k的格子。例如,当k为18时,机器人能够进入方格(35, 37),因为3+5+3+7=18。
// 但它不能进入方格(35, 38),因为3+5+3+8=19。请问该机器人能够到达多少个格子?
#include <iostream> int movingCountCore(int threshold, int rows, int cols, int row, int col, bool* visited);
bool check(int threshold, int rows, int cols, int row, int col, bool* visited);
int getDigitSum(int number); int movingCount(int threshold, int rows, int cols)
{
if (threshold < || rows <= || cols <= )//每个值得检查的参数都应该被检查
return ; bool *visited = new bool[rows * cols];//用来检测这个点有没有走过
for (int i = ; i < rows * cols; ++i)
visited[i] = false; int count = movingCountCore(threshold, rows, cols, , , visited);//从(0,0)开始检测 delete[] visited; return count;
} int movingCountCore(int threshold, int rows, int cols, int row, int col, bool* visited)
{
int count = ;
if (check(threshold, rows, cols, row, col, visited))//如果让走
{
visited[row * cols + col] = true; count = + movingCountCore(threshold, rows, cols, row - , col, visited)
+ movingCountCore(threshold, rows, cols, row, col - , visited)
+ movingCountCore(threshold, rows, cols, row + , col, visited)
+ movingCountCore(threshold, rows, cols, row, col + , visited);//那就加上1并继续计算四邻点
} return count;
} bool check(int threshold, int rows, int cols, int row, int col, bool* visited)
{
if (row >= && row < rows && col >= && col < cols
&& getDigitSum(row) + getDigitSum(col) <= threshold
&& !visited[row* cols + col])//只有当满足边界条件,满足题设,并且没有走过的点才被允许
return true; return false;
} int getDigitSum(int number)
{
int sum = ;
while (number > )//让我纳闷的是,居然要求值大于零
{
sum += number % ;
number /= ;
} return sum;
} // ====================测试代码====================
void test(const char* testName, int threshold, int rows, int cols, int expected)
{
if (testName != nullptr)
printf("%s begins: ", testName); if (movingCount(threshold, rows, cols) == expected)
printf("Passed.\n");
else
printf("FAILED.\n");
} // 方格多行多列
void test1()
{
test("Test1", , , , );
} // 方格多行多列
void test2()
{
test("Test2", , , , );
} // 方格只有一行,机器人只能到达部分方格
void test3()
{
test("Test3", , , , );
} // 方格只有一行,机器人能到达所有方格
void test4()
{
test("Test4", , , , );
} // 方格只有一列,机器人只能到达部分方格
void test5()
{
test("Test5", , , , );
} // 方格只有一列,机器人能到达所有方格
void test6()
{
test("Test6", , , , );
} // 方格只有一行一列
void test7()
{
test("Test7", , , , );
} // 方格只有一行一列
void test8()
{
test("Test8", , , , );
} // 机器人不能进入任意一个方格
void test9()
{
test("Test9", -, , , );
} int main(int agrc, char* argv[])
{
test1();
test2();
test3();
test4();
test5();
test6();
test7();
test8();
test9();
system("pause");
return ;
}
《剑指offer》第十三题(机器人的运动范围)的更多相关文章
- 【剑指Offer】66、机器人的运动范围
题目描述: 地上有一个m行和n列的方格.一个机器人从坐标0,0的格子开始移动,每一次只能向左,右,上,下四个方向移动一格,但是不能进入行坐标和列坐标的数位之和大于k的格子. 例如,当k为18时 ...
- 剑指offer(66)机器人的运动范围
题目描述 地上有一个m行和n列的方格.一个机器人从坐标0,0的格子开始移动,每一次只能向左,右,上,下四个方向移动一格,但是不能进入行坐标和列坐标的数位之和大于k的格子. 例如,当k为18时,机器人能 ...
- 《剑指offer》刷题目录
<剑指offer>刷题目录 面试题03. 数组中重复的数字 面试题04. 二维数组中的查找 面试题05. 替换空格 面试题06. 从尾到头打印链表 面试题07. 重建二叉树 面试题09. ...
- 浅谈《剑指offer》原题:不使用条件、循环语句求1+2+……+n
转载自:浅谈<剑指offer>原题:求1+2+--+n 如侵犯您的版权,请联系:windeal12@qq.com <剑指offer>上的一道原题,求1+2+--+n,要求不能使 ...
- 《剑指offer》算法题第十二天
今天是<剑指offer>算法题系列的最后一天了,但是这个系列并没有包括书上的所有题目,因为正如第一天所说,这些代码是在牛客网上写并且测试的,但是牛客网上并没有涵盖书上所有的题目. 今日题目 ...
- 《剑指offer》刷题笔记
简介 此笔记为我在 leetcode 上的<剑指offer>专题刷题时的笔记整理. 在刷题时我尝试了 leetcode 上热门题解中的多种方法,这些不同方法的实现都列在了笔记中. leet ...
- 《剑指Offer》附加题_用两个队列实现一个栈_C++版
在<剑指Offer>中,在栈和队列习题中,作者留下来一道题目供读者自己实现,即"用两个队列实现一个栈". 在计算机数据结构中,栈的特点是后进先出,即最后被压入(push ...
- 剑指offer 面试5题
面试5题: 题目:请实现一个函数,将一个字符串中的空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. 方法一: # -*- co ...
- 剑指offer 面试8题
面试8题: 题目:二叉树的下一个节点 题目描述:给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回.注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针. 解题思路:详见剑 ...
- 剑指offer 面试10题
面试10题: 题目:大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项.n<=39 n=0时,f(n)=0 n=1时,f(n)=1 n>1时,f(n)=f(n-1 ...
随机推荐
- 简单的js动态显示当前时间
js中获取当前时间首先我们要new一个时间对象 var data = new Date(); 然后可以点出很多方法.获取不同的时间格式 自己可以去尝试
- c#及js实现将金融变成3位一逗号
1.c#用string.format ToString("#,###.00") 2.js方法 转自http://www.cnblogs.com/cssfirefly/p/35820 ...
- bootstrap 带有确定取消按钮的modal
</div><div class="modal fade" id="confirmModal" tabindex="-1" ...
- suiyi
<?php namespace app\controllers; use Yii;use app\models\Device;use app\models\DeviceSearch;use ap ...
- zw版【转发·台湾nvp系列Delphi例程】HALCON SetComprise2
zw版[转发·台湾nvp系列Delphi例程]HALCON SetComprise2 procedure TForm1.Button1Click(Sender: TObject);var op : H ...
- Python:键盘输入input
从键盘读入数据 >>> num=input('利润是:') 利润是:55 >>>
- Applying the Kappa architecture in the telco industry
https://www.oreilly.com/ideas/applying-the-kappa-architecture-in-the-telco-industry Kappa architectu ...
- Shell脚本实现检测某ip网络畅通情况,实战用例
Shell脚本实现检测某ip网络畅通情况,实战用例 环境准备,linux shell 发送email 邮件:1.安装sendmailyum -y install sendmail安装好sendmail ...
- MySQL数据库----视图
视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,可以将该结果集当做表来使用. 使用视图我们可以把查询过程中的临时 ...
- 计算概论(A)/基础编程练习1(8题)/6:判断闰年
#include<stdio.h> int isLeap(int year) { // 必须先判断是平年的情况 后判断闰年的情况 == && year%!=) || yea ...