当前位置:网站首页>剑指 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; }};边栏推荐
- Redis布隆过滤器和布谷鸟过滤器
- NAACL 2022 | 机器翻译SOTA模型的蒸馏
- 【现代信号处理第六次作业】
- 0基础自学STM32(野火)——寄存器点亮LED
- Development of freedom free agreement pledge mining system
- 【Oracle】基础知识面试题
- It is the same that robots can win gold medals without maintenance and out of bounds
- C comparison of the performance of dapper efcore sqlsugar FreeSQL hisql sqlserver, an ORM framework at home and abroad
- 微信小程序开发储备知识
- 机器人不需要保养和出界也能拿金牌是一样一样的
猜你喜欢

mysql支持外键吗

0基础自学STM32(野火)——寄存器点亮LED
Master slave replication of MySQL

0 basic self-study STM32 (wildfire) - register lit LED

Function calculation asynchronous task capability introduction - task trigger de duplication

腾讯云发布自动化交付和运维产品Orbit,推动企业应用全面云原生化

“授权同意”落地压力大?隐私计算提供一种可能的合规“技术解”

为什么信息化 ≠ 数字化?终于有人讲明白了

NAACL 2022 | 机器翻译SOTA模型的蒸馏

SLAM中的子图
随机推荐
Online sql to CSV tool
深圳内推 | 深圳计算科学研究院招聘机器学习助理工程师(校招)
使用 SSH 方式拉取代码
mysql查询视图命令是哪个
开源仓库贡献 —— 提交 PR
When MySQL RDS is collected using Flink CDC, the datetime type field will be compared with the source table after collection
What role does the supply chain management system play in the supply chain scenario?
C语言练习----指针字符串、链表
The R language inputs the distance matrix to the hclust function for hierarchical clustering analysis. The method parameter specifies the distance calculation method between two combined data points,
KUKA子程序/函数怎么建立和使用方法
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
Issue 42: is it necessary for MySQL to have multiple column partitions
【现代信号处理第六次作业】
[the sixth operation of modern signal processing]
Help MySQL data analysis with databend
反射
R语言使用MASS包的glm.nb函数建立负二项广义线性模型(negative binomial)、summary函数获取负二项广义线性模型模型汇总统计信息
epoll分析
NAACL 2022 | 机器翻译SOTA模型的蒸馏
0 basic self-study STM32 (wildfire) - register lit LED