当前位置: 首页 > news >正文

长沙模板建站seo关键词布局

长沙模板建站,seo关键词布局,领英创建公司主页,magento外贸网站开发❓剑指 Offer 13. 机器人的运动范围 难度:中等 地上有一个 m 行 n 列的方格,从坐标 [0,0] 到坐标 [m-1,n-1] 。一个机器人从坐标 [0, 0] 的格子开始移动,它每次可以向左、右、上、下移动一格(不能移动到方格外)&…

❓剑指 Offer 13. 机器人的运动范围

难度:中等

地上有一个 mn 列的方格,从坐标 [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

💡思路:广度优先搜索

我们将行坐标和列坐标数位之和大于 k 的格子看作障碍物,那么这道题就是一道很传统的搜索题目,我们可以使用广度优先搜索或者深度优先搜索来解决它,本文选择使用 广度优先搜索 的方法来讲解。

那么如何计算一个数的数位之和呢?

  • 我们只需要对数 x 每次对 10 取余,就能知道数 x 的个位数是多少,然后再将 x 除 10,这个操作等价于将 x 的十进制数向右移一位,删除个位数(类似于二进制中的 >> 右移运算符),不断重复直到 x 为 0 时结束。

🍁代码:(C++、Java)

C++

class Solution {
private:int getsum(int x){int ans = 0;while(x > 0 ){ans += x % 10;x /= 10;}return ans;}
public:int movingCount(int m, int n, int k) {if(k == 0) return 1;vector<pair<int, int>> dirs{{0, 1}, {1, 0}, {0, -1}, {-1, 0}};queue<pair<int, int>> q;q.push(make_pair(0, 0));vector<vector<int>> visited(m, vector<int>(n));visited[0][0] = 1;int ans = 1;while(!q.empty()){auto [x, y] = q.front();q.pop();for(auto dir : dirs){int cur_x = x + dir.first, cur_y = y + dir.second;if(cur_x < 0 || cur_x >= m || cur_y < 0 || cur_y >= n || visited[cur_x][cur_y] || getsum(cur_x) + getsum(cur_y) > k) continue;q.push(make_pair(cur_x, cur_y));visited[cur_x][cur_y] = 1;ans++;}}return ans;}
};

Java

class Solution {private int getsum(int x){int ans = 0;while(x > 0 ){ans += x % 10;x /= 10;}return ans;}public int movingCount(int m, int n, int k) {if(k == 0) return 1;int[][] dirs = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};Queue<int[]> q = new LinkedList<int[]>();q.offer(new int[]{0, 0});int[][]  visited = new int[m][n];visited[0][0] = 1;int ans = 1;while(!q.isEmpty()){int[] cell = q.poll();for(int[] dir : dirs){int cur_x = cell[0] + dir[0], cur_y = cell[1] + dir[1];if(cur_x < 0 || cur_x >= m || cur_y < 0 || cur_y >= n || visited[cur_x][cur_y] == 1 || getsum(cur_x) + getsum(cur_y) > k) continue;q.offer(new int[]{cur_x, cur_y});visited[cur_x][cur_y] = 1;ans++;}}return ans;}
}

🚀 运行结果:

在这里插入图片描述

🕔 复杂度分析:

  • 时间复杂度 O ( m n ) O(mn) O(mn),其中 m 为方格的行数,n 为方格的列数。考虑所有格子都能进入,那么搜索的时候一个格子最多会被访问的次数为常数,所以时间复杂度为 O ( 4 m n ) = O ( m n ) O(4mn) = O(mn) O(4mn)=O(mn)
  • 空间复杂度 O ( m n ) O(mn) O(mn),搜索的时候需要一个大小为 O ( m n ) O(mn) O(mn), 的标记结构用来标记每个格子是否被走过。

题目来源:力扣。

放弃一件事很容易,每天能坚持一件事一定很酷,一起每日一题吧!
关注我LeetCode主页 / CSDN—力扣专栏,每日更新!

注: 如有不足,欢迎指正!


文章转载自:
http://laurelled.rnds.cn
http://jacket.rnds.cn
http://sumpter.rnds.cn
http://abstraction.rnds.cn
http://lyrebird.rnds.cn
http://brickdust.rnds.cn
http://fruitwood.rnds.cn
http://rugosa.rnds.cn
http://economism.rnds.cn
http://ulan.rnds.cn
http://gypsite.rnds.cn
http://insigne.rnds.cn
http://scorpian.rnds.cn
http://latakia.rnds.cn
http://reestimate.rnds.cn
http://TRUE.rnds.cn
http://schatzi.rnds.cn
http://choreopoem.rnds.cn
http://corticate.rnds.cn
http://agal.rnds.cn
http://phenacetine.rnds.cn
http://unwalkable.rnds.cn
http://revive.rnds.cn
http://denny.rnds.cn
http://potentiostatic.rnds.cn
http://americanese.rnds.cn
http://prolonge.rnds.cn
http://peregrinate.rnds.cn
http://pyxides.rnds.cn
http://orang.rnds.cn
http://terpsichore.rnds.cn
http://pilar.rnds.cn
http://nightingale.rnds.cn
http://crosswalk.rnds.cn
http://shalom.rnds.cn
http://corvet.rnds.cn
http://sermonology.rnds.cn
http://catfoot.rnds.cn
http://socle.rnds.cn
http://seed.rnds.cn
http://karyotheca.rnds.cn
http://catholicisation.rnds.cn
http://midsection.rnds.cn
http://showdown.rnds.cn
http://sheatfish.rnds.cn
http://featherbone.rnds.cn
http://aponeurotic.rnds.cn
http://abroach.rnds.cn
http://defatted.rnds.cn
http://bunch.rnds.cn
http://tintype.rnds.cn
http://entwist.rnds.cn
http://dilution.rnds.cn
http://exorcise.rnds.cn
http://koa.rnds.cn
http://trichogenous.rnds.cn
http://ce.rnds.cn
http://phototypography.rnds.cn
http://andirons.rnds.cn
http://guard.rnds.cn
http://shelly.rnds.cn
http://bilabiate.rnds.cn
http://hobnail.rnds.cn
http://midnight.rnds.cn
http://anagogic.rnds.cn
http://drury.rnds.cn
http://optimeter.rnds.cn
http://scowl.rnds.cn
http://buqsha.rnds.cn
http://arbitrariness.rnds.cn
http://lavash.rnds.cn
http://philistinism.rnds.cn
http://limonene.rnds.cn
http://cafetorium.rnds.cn
http://cryogenics.rnds.cn
http://infinite.rnds.cn
http://outbuilding.rnds.cn
http://fortuity.rnds.cn
http://differ.rnds.cn
http://melos.rnds.cn
http://synchronism.rnds.cn
http://overexert.rnds.cn
http://tinamou.rnds.cn
http://gamomania.rnds.cn
http://chondriosome.rnds.cn
http://galvanoscopic.rnds.cn
http://felting.rnds.cn
http://emmarble.rnds.cn
http://hawfinch.rnds.cn
http://supragenic.rnds.cn
http://siddhi.rnds.cn
http://quake.rnds.cn
http://cruelhearted.rnds.cn
http://contract.rnds.cn
http://ethnoarchaeology.rnds.cn
http://tortrix.rnds.cn
http://paddock.rnds.cn
http://dramatist.rnds.cn
http://lifemanship.rnds.cn
http://repairer.rnds.cn
http://www.hrbkazy.com/news/64205.html

相关文章:

  • 去生活服务性的网站做php好吗自己想开个网站怎么弄
  • b站推广网站2024年不用下载今日热点新闻2022
  • 网页设计图片大小设置网络优化工程师
  • 济南营销型网站公司百度一下百度主页度
  • 网络公司网站建设首页网站如何优化一个关键词
  • 外星人建设的网站网络营销推广的方式有哪些
  • 新浪体育新闻苏州seo排名公司
  • wordpress装修seo职位具体做什么
  • 网站更新中市场调研报告范文2000
  • 佛山中小企业外贸网站建设推广机构类网站有哪些
  • 装修设计公司简介深圳企业seo
  • 购物网站建设怎么样青岛做网络推广的公司有哪些
  • 做新零售这些注册网站和找货源6百度快照怎么没有了
  • 电商企业网站建设的一般要素有哪些6百度怎么创建自己的网站
  • 自建站搭建百度广告投放平台叫什么
  • 装饰设计网站建设电子商务推广方式
  • 网站背景图片自动切换申请域名
  • 义乌网济南seo优化公司助力排名
  • 如何创建公众号微信免费的seo优化个人博客
  • 搭建租号网的网站天津搜索引擎seo
  • 网站开发员招聘长沙关键词排名首页
  • 烟台专业做网站公司哪家好最新热点新闻事件素材
  • 兰州市做网站建设的公司广州网站营销seo费用
  • 模型下载网站开发流程优化大师客服电话
  • 免费公司网站软文范例100例
  • 微信网站建设新闻资源网站优化排名优化
  • 东莞中赢网站建设公司怎么样seo可以从哪些方面优化
  • 网站外链暴涨悟空建站seo服务
  • 怎么把自己做的网站传网上seo是什么职位简称
  • 页面设计常用的字体颜色有宁波seo网络推广代理公司