当前位置:网站首页>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 !
边栏推荐
- If a programmer goes to prison, will he be assigned to write code?
- (原创)【MAUI】一步一步实现“悬浮操作按钮”(FAB,Floating Action Button)
- 加油站(贪心)
- Idea global search shortcut settings
- VPS是干嘛用的?有哪些知名牌子?与云服务器有什么区别?
- G : 最大流问题
- Is it safe to open an account on the flush
- PCB understand Wang, are you? I am not
- Kubernetes' in-depth understanding of kubernetes (II) declaring organizational objects
- 配置文件加密(Jasypt的简单使用)
猜你喜欢

Jupyter notebook中添加虚拟环境

Zhongang mining focuses on the fluorine chemical industry and lays out the new energy industry chain

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

Foreign trade SEO Webmaster Tools

Other domestic mobile phones failed to fill the vacancy of Huawei, and apple has no rival in the high-end mobile phone market

线程终止的 4 种方式

Work study management system based on ASP

欧拉恒等式:数学史上的真正完美公式!

If a programmer goes to prison, will he be assigned to write code?

腾讯云国际云服务器登录之后没有网络,如何排查?
随机推荐
智联招聘基于 Nebula Graph 的推荐实践分享
Build a learning environment
Navicat Premium 16 永久破解激活工具及安装教程(亲测可用)
Other domestic mobile phones failed to fill the vacancy of Huawei, and apple has no rival in the high-end mobile phone market
做一个墨水屏电子钟,炫酷!
Deveco studio 3.0 editor configuration tips
Zhongang mining focuses on the fluorine chemical industry and lays out the new energy industry chain
PC博物馆-熟悉又陌生的懵懂年代
线程终止的 4 种方式
How to design data visualization platform
Solving Hanoi Tower problem
A queue of two stacks
Explanation of sprintf function in C language
NFT digital collection system development (3D modeling economic model development case)
2022中式烹調師(高級)試題及在線模擬考試
推荐四款可视化工具,解决 99% 的可视化大屏项目!
sort
栅格矢量数据的裁剪
腾讯云国际云服务器登录之后没有网络,如何排查?
[codec] write H264 decoder (1) from scratch