机器人的运动范围 剑指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, ...
随机推荐
- nginx 隐藏index.php 并开启rewrite日志调试(apache也有)
开启rewrite 日志 error_log /data/log/nginx/error.log notice; 位于最外层,大约在文件的前几行 再在http{}括号里增加一行:rewri ...
- 现在就可以使用的5个 ES6 特性
小编推荐:掘金是一个高质量的技术社区,从 ECMAScript 6 到 Vue.js,性能优化到开源类库,让你不错过前端开发的每一个技术干货.各大应用市场搜索「掘金」即可下载APP,技术干货尽在掌握. ...
- 使用apt-cache search搜索想要的软件包
环境: Ubuntu14.10 我在编译u-boot代码的时候遇到了如下问题: LD test/dm/built-in.o CC examples/standalone/stubs.o LD exam ...
- WebService authentication
http://blog.csdn.net/largestone_187/article/details/5734632 通过SoapHeader对用户口令进行验证,只有授权的用户才可以使用接口.确保了 ...
- VSM(Virtual Storage Manager For Ceph)安装教程
转载注明出处,陈小跑 http://www.cnblogs.com/chenxianpao/p/5770271.html 一.安装环境 OS:CentOS7.2 VSM:v2.1 released 二 ...
- 使用Python开发SQLite代理服务器(转载)
转载:https://mp.weixin.qq.com/s?timestamp=1498531736&src=3&ver=1&signature=Eq6DPvkuGJi*G5s ...
- Python 获取图片文件大小并转换为base64编码
import os import base64 fileSize = os.path.getsize(文件路径) with open(文件路径, 'rb') as f: data = base64.b ...
- Java 关于容器集合等数据结构详情图解,一目了然
建议把图片下载下来保存之,网页展示不开... watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY3NtX3F6/font/5a6L5L2T/fontsize/ ...
- 用C#封装的ServiceStack.redis操作类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- driver: Linux设备模型之input子系统具体解释
本节从总体上解说了输入子系统的框架结构.有助于读者从总体上认识linux的输入子系统.在陷入代码分析的过程中,通过本节的知识可以找准方向,明确原理. 本节重点: 输入子系统的框架结构 各层相应内核中的 ...