当前位置:网站首页>LeeCode机器人的运动范围
LeeCode机器人的运动范围
2022-07-15 16:57:00 【Java精灵儿】
地上有一个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
两种遍历方式DFS/BFS
//深度优先搜索dfs
class Solution {
int m,n;
boolean [][]visited;
public int movingCount(int m, int n, int k) {
this.m=m;
this.n=n;
visited=new boolean[m][n];
return dfs(0,0,k);
}
public int dfs(int i,int j,int k){
if(i>=m||j>=n
|| visited[i][j]
|| sum(i,j)>k
)return 0;
visited[i][j]=true;
return 1+dfs(i+1,j,k)+dfs(i,j+1,k);
}
public int sum(int i,int j){
int sum = 0;
while (i != 0) {
sum += i % 10;
i /= 10;
}
while (j != 0) {
sum += j % 10;
j /= 10;
}
return sum;
}
}
BFS
//广度优先搜索
class Solution {
public int movingCount(int m, int n, int k) {
boolean[][] visited = new boolean[m][n];
int[][] direct = {
{
1, 0}, {
0, 1}};
int res = 0;
Queue<AbstractMap.SimpleEntry<Integer, Integer>> queue = new LinkedList<>();
queue.add(new AbstractMap.SimpleEntry<>(0, 0));
while (!queue.isEmpty()) {
AbstractMap.SimpleEntry<Integer, Integer> cor = queue.poll();
Integer x = cor.getKey();
Integer y = cor.getValue();
int sum = x / 10 + x % 10 + y / 10 + y % 10;
if (x < 0 || y < 0 || x >= m || y >= n || visited[x][y] || sum > k) {
continue;
}
visited[x][y] = true;
res++;
for (int i = 0; i < 2; i++) {
queue.add(new AbstractMap.SimpleEntry<>(x + direct[i][0], y + direct[i][1]));
}
}
return res;
}
}
边栏推荐
- 7. MySQL -- 基础语法(三) DCL
- PD QC leurre application IC ledry ldr6328s est largement utilisé dans les appareils ménagers de toutes tailles
- String 长度有限制吗?是多少?
- ClickHouse SQL 的十項優化規則
- 免驱 USB 转串口 Billboard 芯片(LDR2001)
- 2021年全国职业院校技能大赛(中职组)网络安全竞赛试题(9)思路
- MySQL pymysql module
- Type-c边充边传数据应用OTG功能(LDR6028S)
- When the decimal places are reserved, there may be a simple solution without rounding
- Drive free USB to serial port billboard chip (ldr2001)
猜你喜欢

Lifecycle:生命周期感知型组件的基础 —— Jetpack 系列(1)

LDR9201音频数字解码DAC加LDR6023C数字加PD快充方案

*链表逆转

Enumeration, do you know it?

(一)MATLAB基础知识

C语言 第九章 字符串

7. MySQL -- basic syntax (III) DCL
![[problems of dft/fft - solutions to fence effect]](/img/e2/7bd81b9bee18768757660f9c1c0d35.png)
[problems of dft/fft - solutions to fence effect]

The game console type-C scheme supports data transmission while charging

2021年全国职业院校技能大赛(中职组)网络安全竞赛试题(9)思路
随机推荐
Deep learning loss function
Switching with file system in OTA upgrade
(一)MATLAB基础知识
Sensor principle article
The most important diagram of machine learning, how to select the model sklearn structure diagram
初学者怎么快速学会SQL
OTA 升级中的跟文件系统切换
Type-c充电OTG芯片(LDR6028S)
[cloud native] 3.4 ruoyi cloud deployment practice (Part 1)
Resnet50 structure diagram
Exploration of yolact model structure
布尔代数值
C语言 第六章 函数
ClickHouse SQL 的十项优化规则
Typec display solution Daquan ldr6290 single C-Port desktop display solution
Tensorflow image data enhancement preprocessing
产品-Axure9英文版,使用Repeater中继器实现下拉多选框
在数组中指定位置插入任意一个元素及删除数组中值为x的元素
8. MySQL -- trigger
PD QC誘騙取電應用IC《樂得瑞LDR6328S》廣泛應用於各大小家電