当前位置:网站首页>High frequency leetcode deep search part: Sword finger offer 13 Range of motion of robot
High frequency leetcode deep search part: Sword finger offer 13 Range of motion of robot
2022-07-22 09:28:00 【Hiccup~~~~】
The finger of the sword Offer 13. The range of motion of the robot
Medium difficulty 447
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 ?
Example 1:
Input :m = 2, n = 3, k = 1 Output :3
Example 2:
Input :m = 3, n = 1, k = 0 Output :1
Tips :
- 1 <= n,m <= 100
- 0 <= k <= 20
Ideas
- Walk in four directions + Discriminant conditions
Code
class Solution {
public:
int split_num(int i){
// Returns the sum of the digits of a number
int ix=0;
while(i)
{
ix+=i%10;
i/=10;
}
return ix;
}
vector<vector<int>> vis=vector<vector<int>>(100, vector<int>(100, 0));
void dfs(int m,int n,int k,int i,int j){
// Go in four directions
if(vis[i][j]!=0) return;
if((split_num(i)+split_num(j))<=k)
vis[i][j]=1;
else
vis[i][j]=2;
if(vis[i][j]==1){
if(i+1<m) dfs(m,n,k,i+1,j);
if(j+1<n) dfs(m,n,k,i,j+1);
if(i-1>=0) dfs(m,n,k,i-1,j);
if(j-1>=0) dfs(m,n,k,i,j-1);
}
}
int movingCount(int m, int n, int k) {
int count=0;
dfs(m,n,k,0,0);
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(vis[i][j]==1)
{
// printf("%d,%d\t",i,j);
count++;
}
}
// printf("\n");
}
return count;
}
};
边栏推荐
- C语言之位操作和整形的补位
- 文献:案例《电影排行榜》
- 【ACM/二分】二分清晰入门级讲解
- The multi indicator data of 4000+ listed companies' provinces, cities and operations have been updated to 2022.1
- Pointer of C language (2)
- Review background management system practice: two packaging styles of request parameters
- Laravel installs the debugbar toolbar and configures the virtual host
- Exception class
- c语言之指针(二)
- 聊天室项目
猜你喜欢
Tens of millions of data: from 2000 to 2019, enterprise registration data longitude and latitude, registration number and other multi indicator information of provinces, cities and counties in China
Pointer of C language (3)
Pointer of C language (1)
Docker系列 六. Docker 安装 Redis
Luoyang comprehensive bonded zone was officially approved by the State Council to be established
MySQL安裝配置-8.0版-Windows
数组的反转(逆序输出)(定义一个数组并赋值按逆序输出这个数组)
修改Hosts文件自定义本机IP域名
MySQL installation configuration -version 8.0 -windows
核心技术
随机推荐
Mvcc multi version concurrency control for MySQL learning
c语言之数组
hdparm
程序员那些年的斗智斗勇!!!
electron
各地級市-進出口與貿易差額(2000-2020)
空转工具推荐 | 10款空间转录组去卷积工具的综合比较
嵌入式之SD卡异常问题分析
各地级市-进出口与贸易差额(2000-2020)
Bitmap of redis principle
odoo-js-doAction详解
c语言之指针(二)
Cross border trade terms
Leetcode1486: array XOR operation
文献:案例《电影排行榜》
Switch virtual environment in Jupiter notebook
单片机入门知识
Axure repeater
Array of C language
EasyCVR平台设备分组新增以及编辑操作异常的问题修复