当前位置:网站首页>剑指 Offer 13. 机器人的运动范围
剑指 Offer 13. 机器人的运动范围
2022-06-24 19:40:00 【大梦谁先觉i】
地上有一个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。请问该机器人能够到达多少个格子?
示例 1:
输入:m = 2, n = 3, k = 1
输出:3
示例 2:
输入:m = 3, n = 1, k = 0
输出:1
提示:
1 <= n,m <= 100
0 <= k <= 20
class Solution {
int res=0;
public int movingCount(int m, int n, int k) {
boolean[][] arr = new boolean[m][n];//用来做标记数组
dfs(0,0,m,n,k,arr);
return res;
}
private void dfs(int i, int j, int m, int n,int k,boolean[][] arr) {
//基本判断+判断是否走过这条路
if (i>=m||i<0||j>=n||j<0||arr[i][j]){
return;
}
//m没有走过。先标记,在判断是否符合题意
arr[i][j]=true;
//求和
int sum =i%10+j%10+i/10+j/10;
//判断是否满足条件
if (sum>k) return;
res++;
//直接大范围撒网
dfs(i+1,j,m,n,k,arr);
dfs(i-1,j,m,n,k,arr);
dfs(i,j+1,m,n,k,arr);
dfs(i,j-1,m,n,k,arr);
}
}
边栏推荐
- [ingénierie logicielle] points clés à la fin de la période
- Virtual private network foundation
- 【软件工程】期末重点
- Technology inventory: Technology Evolution and Future Trend Outlook of cloud native Middleware
- How to solve the problem that the computer suddenly can't connect to WiFi
- Tetris
- Genesis public chain and a group of encryption investors in the United States gathered in consensus 2022
- Leetcode: calculate the number of elements less than the current element on the right (sortedlist+bisect\u left)
- Problem solving - nested lists
- 详细了解Redis的八种数据类型及应用场景分析
猜你喜欢

Power system | IEEE paper submission process

Annotation
![[QT] QT event handling](/img/48/14a5491307fee1c99434d6cb308337.jpg)
[QT] QT event handling

Database transaction Transanction

O (n) complexity hand tear sorting interview questions | an article will help you understand counting sorting

envoy获取客户端真实IP

JMM 最最最核心的概念:Happens-before 原则
CA Zhouji - the first lesson in 2022 rust

【个人实验报告】

Talk about GC mechanism often asked in interview
随机推荐
Beijiafu (p+f) R2000 modified radar IP
CSRF and SSRF for web attacks
The usage difference between isempty and isblank is so different that so many people can't answer it
Technology inventory: past, present and future of Message Oriented Middleware
Source code reading | the process of reading text format STL by openmesh
【Mongodb】READ_ME_TO_RECOVER_YOUR_DATA,数据库被恶意删除
Envoy obtain the real IP address of the client
是否需要提高代码阅读能力?这有技巧
Data communication foundation - Ethernet port mirroring and link aggregation
Selection and comparison of message oriented middleware MQ
The ktp900f mobile download program of the fail safe mobile panel prompts that the download cannot be performed, and the target device is running or not in the transmission mode
Problem solving - nested lists
Annotation
Pinduoduo updates the merchant live broadcast service agreement and strictly punishes the illegal merchants
Web security XSS foundation 06
Technology Review: what is the evolution route of container technology? What imagination space is there in the future?
envoy获取客户端真实IP
In the multi network card environment, the service IP registered by Nacos is incorrect, resulting in inaccessible services
[Software Engineering] key points at the end of the period
Yyds dry goods inventory junit5 learning II: assumptions class