当前位置:网站首页>机器人的运动范围(DFS)
机器人的运动范围(DFS)
2022-06-28 14:08:00 【华为云】
title: 机器人的运动范围(DFS)
categories: LeetCode
tags:
- DFS
- 每天进步一点点系列
题目
难度 中等
地上有一个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
代码:
Java:
class Solution { //结果 int sum =0; public int movingCount(int m, int n, int k) { //初始化一个二维数组表示是否访问过 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){ //判断是否符合条件 if (i < 0 || i >= m || j < 0 || j >= n || visited[i][j] || getSum(i)+getSum(j) > k){ return; } //标记访问过了 visited[i][j] = true; //结果加1 sum++; //深度优先搜索 四个方向 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); } //计算数位和 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; }};以上就是机器人的运动范围(DFS)的全部内容
版权声明:
原创博主:牛哄哄的柯南
个人博客链接:https://www.keafmd.top/
看完如果对你有帮助,感谢点击下面的==一键三连==支持!
[哈哈][抱拳]

加油!
共同努力!
Keafmd
都看到这里了,下面的内容你懂得,让我们共同进步!
边栏推荐
- 外贸SEO 站长工具
- Other domestic mobile phones failed to fill the vacancy of Huawei, and apple has no rival in the high-end mobile phone market
- Kubernetes 深入理解Kubernetes(二) 声明组织对象
- Action interprets value. The chairman of chenglian Youpin Han attended the Guangdong Yingde flood fighting donation public welfare event
- [binary tree] allocate coins in the binary tree
- 药物发现新方法,阿斯利康团队通过课程学习改进从头分子设计
- 众昂矿业着眼氟化工产业,布局新能源产业链
- Prediction of red wine quality by decision tree
- Deveco studio 3.0 editor configuration tips
- Ruijie switch configuration SSH password login command [easy to understand]
猜你喜欢

Why can't Bert completely kill the BM25??

木兰开放作品许可证1.0面向社会公开征求意见

基于ASP的勤工俭学管理系统

How to count dimensions of foreign trade E-mail Promotion

Nature | mapping the interaction map of plant foliar flora to establish genotype phenotype relationship

Nature子刊 | 绘制植物叶际菌群互作图谱以建立基因型表型关系

Can your code talk? (upper)

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

Hematemesis recommends 17 "wheels" to improve development efficiency

排序
随机推荐
Special test for cold and hot start of app
Go array and slice, []byte to string[easy to understand]
PC Museum - familiar and strange ignorant age
@ControllerAdvice + @ExceptionHandler 全局处理 Controller 层异常
如何设计数据可视化平台
你的代碼會說話嗎?(上)
How to open an account of Huatai Securities? How to handle the account opening is the safest
正则匹配数字,英文以及英文符号
Operation and maintenance thinking | do you know the relationship between CMDB and monitoring?
sort
Idea global search shortcut settings
Play NAS home NAS server setup scheme "suggestions collection"
栅格矢量数据的裁剪
Design artificial intelligence products: technical possibility, user acceptability and commercial feasibility
ArcGIS vector center point generates rectangle and cuts TIF image for deep learning sample training
If a programmer goes to prison, will he be assigned to write code?
线程的生命周期以及其中的方法
Arcgis 矢量中心点生成矩形并裁剪tif图像进行深度学习样本训练
What is generics and how to use generics analysis
【二叉树】在二叉树中分配硬币