当前位置:网站首页>Robot range of motion (DFS)
Robot range of motion (DFS)
2022-06-28 14:13:00 【Hua Weiyun】
title: The range of motion of the robot (DFS)
categories: LeetCode
tags:
- DFS
- Make a little progress every day
subject
The range of motion of the robot
difficulty secondary
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 :3Example 2:
Input :m = 3, n = 1, k = 0
Output :1
Tips :
1 <= n,m <= 100
0 <= k <= 20
Code :
Java:
class Solution { // result int sum =0; public int movingCount(int m, int n, int k) { // Initializes a two-dimensional array to indicate whether it has accessed boolean[][] visited = new boolean[m][n]; dfs(0,0,m,n,k,visited); return sum; } private void dfs(int i,int j,int m,int n,int k,boolean[][] visited){ // Judge whether the conditions are met if (i < 0 || i >= m || j < 0 || j >= n || visited[i][j] || getSum(i)+getSum(j) > k){ return; } // Mark visited visited[i][j] = true; // Result plus 1 sum++; // Depth-first search Four directions dfs(i-1,j,m,n,k,visited); dfs(i+1,j,m,n,k,visited); dfs(i,j-1,m,n,k,visited); dfs(i,j+1,m,n,k,visited); } // Calculate digit sum private int getSum(int num){ int sum =0; while(num>0){ sum+=num%10; num/=10; } return sum; }}
C++:
class Solution {public: int res = 0;public: int movingCount(int m, int n, int k) { vector<vector<bool>> visited(m, vector<bool>(n, 0)); dfs(0, 0, m, n, k, visited); return res; }public: void dfs(int i, int j, int m, int n, int k, vector<vector<bool>> &visited) { if (i < 0 || i >= m || j < 0 || j >= n || visited[i][j] || getSum(i) + getSum(j) > k) { return; } visited[i][j] = true; res++; dfs(i + 1, j, m, n, k, visited); dfs(i - 1, j, m, n, k, visited); dfs(i, j + 1, m, n, k, visited); dfs(i, j - 1, m, n, k, visited); }public: int getSum(int n) { int sum = 0; while (n) { sum += n % 10; n /= 10; } return sum; }};
The above is the range of motion of the robot (DFS) The whole content of
Copyright notice :
Original Blogger : Cowherd Conan
Personal blog links :https://www.keafmd.top/
If it helps you , Thank you for clicking on == One key, three links == Support !
[ ha-ha ][ Huai Quan ]
come on. !
Joint efforts !
Keafmd
You can see it here , You know the following , Let's make progress together !
边栏推荐
- Kubernetes in-depth understanding of kubernetes (I)
- 【二叉树】在二叉树中分配硬币
- Thread life cycle and its methods
- DevEco Studio 3.0编辑器配置技巧篇
- Operation and maintenance thinking | do you know the relationship between CMDB and monitoring?
- 你的代码会说话吗?(上)
- Foreign trade SEO Webmaster Tools
- Navicat premium 16 permanent crack activation tool and installation tutorial (available for personal test)
- 机器人的运动范围(DFS)
- Research and Simulation of chaotic digital image encryption technology based on MATLAB
猜你喜欢
欧拉恒等式:数学史上的真正完美公式!
Design artificial intelligence products: technical possibility, user acceptability and commercial feasibility
基于 Nebula Graph 构建百亿关系知识图谱实践
CVPR再起争议:IBM中稿论文被指照搬自己承办竞赛第二名的idea
CVPR disputes again: IBM's Chinese draft papers were accused of copying idea, who won the second place in the competition
Work study management system based on ASP
Action interprets value. The chairman of chenglian Youpin Han attended the Guangdong Yingde flood fighting donation public welfare event
Kubernetes in-depth understanding of kubernetes (I)
PC博物馆-熟悉又陌生的懵懂年代
Nature | mapping the interaction map of plant foliar flora to establish genotype phenotype relationship
随机推荐
RSLO:自监督激光雷达里程计(实时+高精度,ICRA2022)
3、项目的整体UI架构
中国内地仅四家突围 联想智慧颐和园荣获 “2022年IDC亚太区智慧城市大奖”
Jupyter notebook中添加虚拟环境
ThreadLocal的简单理解
Operation and maintenance thinking | do you know the relationship between CMDB and monitoring?
G: maximum flow problem
Summary of 2021 computer level III database
Rslo: self supervised lidar odometer (real time + high precision, icra2022)
sort
【二叉树】在二叉树中分配硬币
同花顺上开户是安全的吗
What is generics and how to use generics analysis
智联招聘基于 Nebula Graph 的推荐实践分享
go数组与切片,[]byte转string[通俗易懂]
《蛤蟆先生去看心里医生》阅读笔记
Websocket automatically disconnects in 1 minute
i++ , ++i
Luogu_ P1303 A*B Problem_ High precision calculation
Is it safe to open an account on the flush