当前位置:网站首页>剑指 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);
}
}
边栏推荐
- 双亲委派机制
- ThreadLocal local thread
- Basic principles of spanning tree protocol
- seven
- Nuscenes -- remedies for missing image files or 0-size images encountered during dataset configuration
- Learn more about redis' eight data types and application scenario analysis
- vulnhub Vegeta: 1
- 【Mongodb】READ_ ME_ TO_ RECOVER_ YOUR_ Data, the database is deleted maliciously
- Fanuc robot_ Introduction to Karel programming (1)
- Visitor tweets tell you which groups are consuming blind boxes
猜你喜欢
Database transaction Transanction
See how sparksql supports enterprise level data warehouse
Genesis public chain and a group of encryption investors in the United States gathered in consensus 2022
Technology inventory: past, present and future of Message Oriented Middleware
ACL (access control list) basic chapter - Super interesting learning network
See how sparksql supports enterprise data warehouse
Idea close global search box
【Laravel系列7.9】测试
vulnhub Vegeta: 1
HTTP的缓存控制
随机推荐
环境配置 | VS2017配置OpenMesh源码和环境
Cat write multiline content to file
双亲委派机制
Learning bit segment (1)
Talk about GC mechanism often asked in interview
See how sparksql supports enterprise data warehouse
Problèmes de concurrence dans l'allocation de mémoire en tas
Fanuc robot_ Introduction to Karel programming (1)
AttacKG: Constructing Technique Knowledge Graph from Cyber Threat Intelligence Reports代码复现
Beijiafu (p+f) R2000 modified radar IP
Relationnet++: a representation of fusion of multiple detection targets based on transformer | neurips 2020
Wechat side: what is consistent hash? In what scenario? What problems have been solved?
AQS source code analysis
动态菜单,自动对齐
In the era of full programming, should I give up this road?
[WSL] SSH Remote Connection and host port forwarding configuration
Technology Review: what is the evolution route of container technology? What imagination space is there in the future?
Are you afraid of being asked MySQL related questions during the interview? This 30000 word essence summary + 100 interview questions, and it's enough to hang the interviewer
Leetcode: push domino (domino simulation)
[Software Engineering] key points at the end of the period