机器人的运动范围 剑指offer66题
include "stdafx.h"
#include<vector>
#include<algorithm>
#include<string>
#include<iostream>
#include<stack>
using namespace std;
class Solution {
public:
int getSum(int rows, int cols)
{
int sum = 0;
while (rows!=0)
{
sum += rows % 10;
rows = rows / 10;
}
while (cols != 0)
{
sum += cols % 10;
cols = cols / 10;
}
return sum;
}
int count = 0;
int movingCount(int threshold, int rows, int cols)
{
vector<vector<bool>> visited(rows,vector<bool>(cols,false));
getCount(threshold, 0, 0, visited, rows, cols);
return count;
}
void getCount(int threshold, int rows, int cols, vector<vector<bool>> &visited,int m,int n)
{
if (getSum(rows, cols) <= threshold)
{
count++;
// cout << count << endl;
visited[rows][cols] = true;
if (cols - 1 >= 0&&visited[rows][cols-1]==false)
{
getCount(threshold, rows, cols - 1, visited,m,n);
}
if (cols + 1 < n && visited[rows][cols + 1]==false)
{
getCount(threshold, rows, cols + 1, visited,m,n);
}
if (rows - 1 >= 0 && visited[rows-1][cols]==false)
{
getCount(threshold, rows-1, cols , visited,m,n);
}
if (rows + 1 < m && visited[rows + 1][cols]==false)
{
getCount(threshold, rows + 1, cols, visited,m,n);
}
// visited[rows][cols] = false;
//count--;
}
}
};
int main()
{
Solution s;
cout << s.movingCount(5, 10, 10) << endl;
return 0;
}
机器人的运动范围 剑指offer66题的更多相关文章
- 剑指Offer66题的总结、目录
原文链接 剑指Offer每日6题系列终于在今天全部完成了,从2017年12月27日到2018年2月27日,历时两个月的写作,其中绝大部分的时间不是花在做题上,而是花在写作上,这个系列不适合大神,大牛, ...
- 剑指offer35题:第一个只出现一次的字符+剑指offer55题:字符流中第一个不重复的字符+剑指offer51题:数组中重复的数字
在看剑指offer的时候,感觉这三个题目很像,都是用哈希表可以解决,所以把这三个题整理出来,以供复习. 剑指offer35题:第一个只出现一次的字符 题目描述:在字符串中找出第一个只出现一次的字符.如 ...
- 学会从后往前遍历,例 [LeetCode] Pascal's Triangle II,剑指Offer 题4
当我们需要改变数组的值时,如果从前往后遍历,有时会带来很多麻烦,比如需要插入值,导致数组平移,或者新的值覆盖了旧有的值,但旧有的值依然需要被使用.这种情况下,有时仅仅改变一下数组的遍历方向,就会避免这 ...
- 剑指offer66:机器人的运动范围
1 题目描述 地上有一个m行和n列的方格.一个机器人从坐标0,0的格子开始移动,每一次只能向左,右,上,下四个方向移动一格,但是不能进入行坐标和列坐标的数位之和大于k的格子. 例如,当k为18时,机器 ...
- 剑指offer66:机器人的活动范围
地上有一个m行和n列的方格.一个机器人从坐标0,0的格子开始移动,每一次只能向左,右,上,下四个方向移动一格,但是不能进入行坐标和列坐标的数位之和大于k的格子. 例如,当k为18时,机器人能够进入方格 ...
- 剑指offer--7题
*题目:输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变. *句子中单词以空格符隔开.为简单起见,标点符号和普通字母一样处理. *例如输入“I am a student.”,则输出“st ...
- 剑指offer--21题
#include "stdafx.h" #include<iostream>using namespace std; void LeftRotateString(cha ...
- 剑指offer20题表示数值的字符串:这题实在是太优雅了
目录 前言 一.憨憨初解 1.思路 2.代码 3.战绩 4.反思 二.看懂再解 1.思路 2.代码 3.C++版战绩 总结 前言 题目来源:https://leetcode.cn/problems/b ...
- 剑指offer--3题
题目:输入一个整形数组,数组里有正数也有负数.数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和.求所有子数组的和的最大值.要求时间复杂度为O(n). 例如输入的数组为1, -2, 3, ...
随机推荐
- Problem J: 求方程的解——C语言初学者百题大战之十五
#include<stdio.h> #include<math.h> int main() { float a,b,c,x1,x2,delta; scanf("%f ...
- BUG YII2.0 cURL error 6: Could not resolve host:
BUG描述:登录直接显示 原因:服务器设置端口权限,或者DNS毛病 解决方案:只能去服务器端设置,配置端口 DNS: 修改dns 114.114.114.114 或者 8.8.8.8
- iOS开发——使用Autolayout弹出键盘
参考: http://segmentfault.com/q/1010000002420050 http://blog.csdn.net/qq448631961/article/details/4034 ...
- html5:localStorage储存
实例:刷新值会增长,关掉浏览器,再打开,值会在原基础上增长 if(localStorage.pagecount){ localStorage.pagecount=Number(localStorage ...
- Shader and Program编程基本概念 - 转
原地址:http://blog.csdn.net/myarrow/article/details/7737313 一.本文关注的问题: • Shader and program 对象介绍• 创建并编译 ...
- What's New In DevTools (Chrome 59)来看看最新Chrome 59的开发者工具又有哪些新功能
原文:https://developers.google.com/web/updates/2017/04/devtools-release-notes#command-menu 参考:https:// ...
- cs-Panination
ylbtech-Unitity: cs-Panination Pager.cs IPagingOption.cs IPagedList.cs PagingOption.cs PagedList.cs ...
- Microsoft-PetSop4.0(宠物商店)-数据库设计-Sql
ylbtech-DatabaseDesgin:Microsoft-PetSop4.0(宠物商店)-数据库设计-Sql DatabaseName:PetShop(宠物商店) Model:宠物商店网站 T ...
- char p[]与char *p的区别
#include <iostream> using namespace std; int main(void) { char *pp = "abc";//*pp指向的是 ...
- 【Hadoop】用 Ganglia 监控hadoop集群
随着数据中心的增长和管理人员的缩减,对计算资源使用有效监视工具的需求变得比以往更加迫切.术语监视 在应用到数据中心时可能会让人混淆,因为它的含义会根据具体的说话者和听众而有所不同.例如: 在集群中运行 ...