当前位置:网站首页>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 !
边栏推荐
- 30 sets of JSP website source code collection "suggestions collection"
- 2021计算机三级数据库大题总结
- 2022金属非金属矿山(地下矿山)主要负责人考试模拟100题模拟考试平台操作
- CVPR disputes again: IBM's Chinese draft papers were accused of copying idea, who won the second place in the competition
- 开闭原则
- 几百行代码实现一个 JSON 解析器
- Votre Code parle? (1)
- Double buffer drawing
- Idea global search shortcut settings
- Prediction of red wine quality by decision tree
猜你喜欢

线程的生命周期以及其中的方法

Connected to rainwater series problems

荐书丨《大脑通信员》:如果爱情只是化学反应,那还能相信爱情吗?

Research and Simulation of chaotic digital image encryption technology based on MATLAB

外贸邮件推广怎么统计维度

推荐四款可视化工具,解决 99% 的可视化大屏项目!

Kubernetes' in-depth understanding of kubernetes (II) declaring organizational objects

Websocket automatically disconnects in 1 minute

How fragrant! The most complete list of common shortcut keys for pychar!

药物发现新方法,阿斯利康团队通过课程学习改进从头分子设计
随机推荐
《蛤蟆先生去看心里医生》阅读笔记
为什么新的5G标准将为技术栈带来更低的 TCO
SPI interface introduction -piyu dhaker
外贸SEO 站长工具
Prediction of red wine quality by decision tree
Summary of 2021 computer level III database
Nature | mapping the interaction map of plant foliar flora to establish genotype phenotype relationship
Black apple installation tutorial OC boot "suggestions collection"
Nature子刊 | 绘制植物叶际菌群互作图谱以建立基因型表型关系
[experience sharing] summary of database operations commonly used in Django development
go数组与切片,[]byte转string[通俗易懂]
Only four breakthrough Lenovo smart Summer Palace in mainland China won the "IDC Asia Pacific Smart City Award in 2022"
Euler equation: a truly perfect formula in the history of mathematics!
[codec] write H264 decoder (1) from scratch
欧拉恒等式:数学史上的真正完美公式!
Npoi export excel and download to client
Kubernetes' in-depth understanding of kubernetes (II) declaring organizational objects
机器人的运动范围(DFS)
药物发现新方法,阿斯利康团队通过课程学习改进从头分子设计
iNFTnews | 科技巨头加快进军Web3和元宇宙