当前位置:网站首页>Sword finger offer 13 Robot range of motion (BFS)
Sword finger offer 13 Robot range of motion (BFS)
2022-06-29 17:35:00 【Hua Weiyun】
Unlock BFS
subject :
There is one on the ground m That's ok n Grid of columns , From coordinates [0,0] To coordinates [m-1,n-1] . A robot from coordinates [0, 0] The grid starts to move , It can go left every time 、 Right 、 On 、 Move down one space ( Can't move out of the box ), The sum of the digits of row coordinates and column coordinates is greater than k Lattice of . for example , When k by 18 when , Robots can enter the grid [35, 37] , because 3+5+3+7=18. But it can't get into the grid [35, 38], because 3+5+3+8=19. How many squares can the robot reach ?
Input :m = 2, n = 3, k = 1
Output :3
1 <= n,m <= 100
0 <= k <= 20
Code
class Solution {public: int cnt=1; int k; int visit[100][100]; int d[4][2] = {{0,1}, {0,-1}, {1,0}, {-1,0}}; // Four directions int m,n; int getNumsSum(int x,int y){ // Calculate the sum of digits of row coordinates and column coordinates 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){ // From i,j Start with a grid 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++) { // Expand (tx,ty) Of 4 A neighbor 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; // Be careful : This sentence is indispensable 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; }};边栏推荐
- R language uses user-defined functions to write deep learning linear activation functions and visualize linear activation functions
- PancakeSwap技术:夹子机器人系统开发原理
- Browser large screen capture
- The soft youth under the blessing of devcloud makes education "smart" in the cloud
- Use SSH to pull codes
- 浏览器大尺寸截屏
- mysql. What is the concept of sock
- 2020版KALI安装教程
- 数字孪生能源系统,打造低碳时代“透视”眼
- 关于KALI使用xshell连接
猜你喜欢

人脸识别4-百度商用方案调研

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

The soft youth under the blessing of devcloud makes education "smart" in the cloud

PCB板框的绘制——AD19

0基础自学STM32(野火)——使用寄存器点亮LED——GPIO功能框图讲解

迈动互联中标大家保险集团

自动收售报机
![分割回文串[dp + dfs组合]](/img/7b/221b000984977508f849e19802c2c2.png)
分割回文串[dp + dfs组合]

Online sql to CSV tool
Help MySQL data analysis with databend
随机推荐
Browser large screen capture
剑指 Offer 13. 机器人的运动范围 (BFS)
sequential detector
正则表达式
SCM系统是什么?供应链管理系统有哪些优势?
Understanding adapter mode from home office | community essay solicitation
首批!腾讯云通过中国信通院政务协同平台解决方案能力评估
关于日期相加减问题
Kubernetes deployment dashboard (Web UI management interface)
What is the function of MySQL cursors
mysql查询视图命令是哪个
mysql在linux中2003错误如何解决
迈动互联中标大家保险集团
What role does the supply chain management system play in the supply chain scenario?
Mysql高可用集群–MHA
How to create a virtual image
【WebDriver】使用AutoIt上传文件
卷妹带你学jdbc—2天冲刺Day1
育润多维发力慈善领域,勇抗企业公益大旗
【现代信号处理第六次作业】