当前位置:网站首页>The range of motion of the robot
The range of motion of the robot
2022-07-30 00:18:00 【Ryuzaki Liuhe】
题目:
地上有一个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.请问该机器人能够到达多少个格子?
分析:
If the robot wants to reach the bottom right corner,Just move right and down,At the same time, avoid encountering the same grid,Because there will be obstacles in the process,Moving left and moving up will go to the duplicate square,Repeated grids do not count,Therefore, it is best to use the depth-first traversal of the graph for this problem,由于k的限制,You need to create a function yourself,to calculate the grid coordinates into numbers and then with k比较,如果kLess than the sum of the function computed coordinates,那么就不能通过.
代码:
public class MovingCount {
//深度优先遍历
int m;
int n;
int k;
boolean[][] visited;
public int movingCount(int m, int n, int k) {
this.m = m;
this.n = n;
this.k = k;
visited = new boolean[m][n];
return dfs(0,0);
}
private int dfs(int i, int j) {
if (i <0 || j <0 || i >=m || j>= n || visited[i][j] || k < sum(i)+sum(j)){
return 0;
}
visited[i][j] = true;
return 1 + dfs(i+1,j) + dfs(i,j+1);
}
private int sum(int x){
int res = 0;
while (x !=0){
res = res + x%10;
x = x/10;
}
return res;
}
}
边栏推荐
- 557. 反转字符串中的单词 III
- CNN的粗浅理解
- 自媒体短视频怎么提高播放量?从这三个方面入手
- EA&UML日拱一卒-多任务编程超入门-(8)多任务安全的数据类
- Go language serialization and deserialization and how to change the uppercase of the json key value to lowercase if the json after serialization is empty
- servlet执行详解
- Worthington用于细胞收获的胰蛋白酶&细胞释放程序
- cp强制覆盖与不覆盖拷贝方法
- "The lighthouse factory" of China path: smart roll out from point to surface
- i.MX6U-driver development-3-new character driver
猜你喜欢
At the age of 29, I was fired from a functional test. Can't find a job after 2 months of interviews?
Introduction to Worthington Elastase & Hyaluronidase
【集训DAY16】KC‘s Can 【动态规划】
【集训DAY18】有趣的交换【模拟】【数学】
How do we-media people create explosive articles?These 3 types of articles are most likely to explode
"The lighthouse factory" of China path: smart roll out from point to surface
Chinese semantic matching
Towhee 每周模型
Genesis与Axis Ventures互动密切
【集训DAY16】KC ‘ s Stars【dfs】
随机推荐
外包干了五年,废了...
Filebeat如何保证在日志文件被切割(或滚动rolling)时依然正确读取文件
头条号自媒体运营:如何在今日头条涨500+粉丝?
第一范式、第二范式、第三范式
Paper Intensive Reading - YOLOv3: An Incremental Improvement
Go language serialization and deserialization and how to change the uppercase of the json key value to lowercase if the json after serialization is empty
窗口函数笔记
rk-boot framework combat (1)
自媒体短视频怎么提高播放量?从这三个方面入手
重写并自定义依赖的原生的Bean方法
Based on TNEWS 'today's headline news in Chinese short text classification
Laravel 预防 SQL 注入
谷歌浏览器(google)设置翻译中文,翻译选项不生效或没有弹出翻译选项
QTableWidget usage example
2022杭电多校第三场 Two Permutations (dp, 哈希)
Worthington Papain & Chymotrypsin & DNase I
Introduction to Worthington Elastase & Hyaluronidase
vim相关介绍(二)
【集训DAY16】KC‘s Can 【动态规划】
Reconstruction of binary tree