当前位置:网站首页>剑指 Offer 13. 机器人的运动范围 (BFS)
剑指 Offer 13. 机器人的运动范围 (BFS)
2022-06-29 17:26:00 【华为云】
解锁BFS
题目:
地上有一个m行n列的方格,从坐标 [0,0] 到坐标 [m-1,n-1] 。一个机器人从坐标 [0, 0] 的格子开始移动,它每次可以向左、右、上、下移动一格(不能移动到方格外),也不能进入行坐标和列坐标的数位之和大于k的格子。例如,当k为18时,机器人能够进入方格 [35, 37] ,因为3+5+3+7=18。但它不能进入方格 [35, 38],因为3+5+3+8=19。请问该机器人能够到达多少个格子?
输入:m = 2, n = 3, k = 1
输出:3
1 <= n,m <= 100
0 <= k <= 20
代码
class Solution {public: int cnt=1; int k; int visit[100][100]; int d[4][2] = {{0,1}, {0,-1}, {1,0}, {-1,0}}; //四个方向 int m,n; int getNumsSum(int x,int y){ //计算行坐标和列坐标的数位之和 int sum=0; while(x){ sum+=(x%10); x/=10; } while(y){ sum+=(y%10); y/=10; } return sum; } void bfs(int i,int j){ //从第i,j个格子开始 queue<pair<int,int> > q; q.push({i,j}); visit[i][j]=1; while(q.size()){ pair<int,int> t=q.front(); q.pop(); int tx=t.first,ty=t.second; for (int i = 0; i < 4; i++) { //扩展(tx,ty)的4个邻居 int nx = tx + d[i][0], ny = ty + d[i][1]; if(nx< m && nx>=0 && ny>=0 && ny<n && visit[nx][ny]==0 && getNumsSum(nx,ny)<=k) { visit[nx][ny] = 1; //注意:这一句必不可少 cnt++; q.push({nx, ny}); } } } } int movingCount(int m, int n, int k) { this->k=k; this->m=m; this->n=n; bfs(0,0); return cnt; }};边栏推荐
- The fixed assets management system enables enterprises to dynamically master assets
- Help MySQL data analysis with databend
- 2022年软件评测师考试大纲
- How to create and delete MySQL triggers
- 固定资产管理系统让企业动态掌握资产情况
- OpenFeign使用步骤 轮询策略与权重 log4j使用 openFeign拦截器的配置
- The R language uses the KAP function (kap.2.raters function) of epidisplay package to calculate the value of kappa statistics (total consistency, expected consistency), analyze the consistency of the
- Error:Connection refused: connect
- PancakeSwap技术:夹子机器人系统开发原理
- 反射
猜你喜欢

疫情居家外包项目之协作开发| 社区征文

High landing pressure of "authorization and consent"? Privacy computing provides a possible compliance "technical solution"

mysql查询视图命令是哪个

@Component与@Configuration区别

函数计算异步任务能力介绍 - 任务触发去重
![[the sixth operation of modern signal processing]](/img/49/7844a00077e56fd4d73e3ba515f8a6.png)
[the sixth operation of modern signal processing]

手把手教你在windows上安装mysql8.0最新版本数据库,保姆级教学

What are the project management systems suitable for small and medium-sized enterprises?

mysql如何查询表的字符集编码

适合中小企业的项目管理系统有哪些?
随机推荐
基于C语言开发实现的一个用户级线程库
R language uses GLM of mass package The Nb function establishes the negative binomial generalized linear model, and the summary function obtains the summary statistical information of the negative bin
2020版KALI安装教程
Development of freedom free agreement pledge mining system
What are the advantages of SaaS services
R language uses user-defined functions to write deep learning linear activation functions and visualize linear activation functions
@Component与@Configuration区别
Bags of Binary Words for Fast Place Recognition in Image Sequenc
自定义HandlerInterceptor拦截器实现用户鉴权
sequential detector
Issue 42: is it necessary for MySQL to have multiple column partitions
R语言ggplot2可视化:使用patchwork包(直接使用加号+)将两个ggplot2可视化结果横向组合、接着再和第三个图像横向组合起来(三幅图各占比例为50%、25%、25%)
Viewing splitchunks code segmentation from MPX resource construction optimization
Collaborative development of epidemic home outsourcing project 𞓜 community essay solicitation
Automatic vending machine
ICML 2022 | transferable imitation learning method based on decoupling gradient optimization
自动收售报机
C comparison of the performance of dapper efcore sqlsugar FreeSQL hisql sqlserver, an ORM framework at home and abroad
为什么信息化 ≠ 数字化?终于有人讲明白了
自学结构体(小甲鱼c语言)