当前位置:网站首页>Range of motion of leecode robot
Range of motion of leecode robot
2022-07-18 08:35:00 【Java Genie】
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
Two ways of traversal DFS/BFS
// Depth-first search 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
// Breadth first search
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;
}
}
边栏推荐
- Trial version of several common methods of database table query in SAP ABAP system
- Refute 'all management without assessment is nonsense'
- pytorch替换numpy中的一些组件 //转载请注明来源
- 但斌投资峰会:论资产管理的重要性!
- 如何从HoloLens中拍摄出满意的照片/视频
- Selenium使用之解决‘chromedriver‘ executable needs to be in PATH.报错信息
- Parent, child, and shared cursors
- Software architecture and design (VI) -- hierarchy
- 浩辰CAD建筑版 2022软件安装包下载及安装教程
- PolarDB for PostgreSQL的HTAP跨机并行查询的执行方法是什么?
猜你喜欢

Software architecture and design (I) -- key principles

阿里最新总结2022年大厂面试真题+核心知识点全面覆盖+答案详解

利用蜜罐反制蓝队

mysql 报错 mysqld:sort aborted:Server shutdown in progress 原因

Li Mu hands on deep learning V2 target detection data set

Torch in pytoch Unsqueeze() and torch Squeeze() function parsing

SAP ABAP 系统进行数据库表查询的几种常用方法

事件4624是登录成功!?!真的如此吗?

浩辰CAD建筑版 2022软件安装包下载及安装教程

Vscode default new directory overlap
随机推荐
YesDev:轻松协作每一个项目
PolarDB for PostgreSQL的HTAP跨机并行查询的执行方法是什么?
OpenCV 教程 01:简介与安装,图片与视频的基本操作
对接企业微信,客户关系管理也可以很简单!
From March to June, after summary, more than 200 pages of true question notes and detailed explanations (including core test sites and 6 major factories)
Signification physique de la transformation de Fourier
5 practices of ITSM to ensure the agility of IT service desk
档案的逻辑 | 档案收集工作
i. Mx6ull driver development | 30 - use EC20 4G network card (migrate gobinet driver)
事件4624是登录成功!?!真的如此吗?
Daily question 1: the minimum value of the sum of the largest number pairs in the array (leecode)
Software architecture and design (x) -- Architecture Technology
木马病毒清除方式
SQL多表查询
开发命令行工具
微软这个系统,90% 的人都没用过!
MySQL 关于 zip安装包 的安装过程
Swift 自定义Subscript
Torch in pytoch repeat_ Analysis of interleave() function
This year, how many war investment departments have become "decorations"