当前位置:网站首页>【每日算法Day 94】经典面试题:机器人的运动范围
【每日算法Day 94】经典面试题:机器人的运动范围
2022-07-27 08:59:00 【51CTO】
最近在忙面试,本来今天都不想更了,但是看基础知识看的太累了,于是写道题排解一下压力。
题目链接
LeetCode 面试题13. 机器人的运动范围[1]
题目描述
地上有一个 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
示例2
说明:
-
1 <= n,m <= 100 -
0 <= k <= 20
题解
这道题没有什么算法,比较简单,主要考察你的代码实现能力,这里我写了两个方法,一个 BFS,一个 DFS。
BFS
BFS 的思路就是用一个队列来保存即将要访问的结点,然后不断出队,将当前结点的四周的结点满足要求的入队。为了避免重复访问,可以用一个 vis 数组来标记已经访问过的结点位置。
DFS
DFS 思路就更加清晰简单了,对于一个结点来说,从它出发可以访问到的结点总数就等于从它四周的结点出发可以访问到的结点总数加一。同样需要用一个 vis 数组来标记已经访问过的结点位置。
代码
BFS(c++)
DFS(c++)
参考资料
[1]
LeetCode 面试题13. 机器人的运动范围: https://leetcode-cn.com/problems/ji-qi-ren-de-yun-dong-fan-wei-lcof/
边栏推荐
- Can "Gulangyu yuancosmos" become an "upgraded sample" of China's cultural tourism industry
- Activation functions commonly used in deep learning
- 4278. Summit
- Matlab画图技巧与实例:堆叠图stackedplot
- The wechat installation package has soared from 0.5m to 260m. Why are our programs getting bigger and bigger?
- Interface test tool - JMeter pressure test use
- Explain cache consistency and memory barrier
- [nonebot2] several simple robot modules (Yiyan + rainbow fart + 60s per day)
- 2034: [Blue Bridge Cup 2022 preliminary] pruning shrubs
- 杭州电子商务研究院发布“数字化存在”新名词解释
猜你喜欢

5G没能拉动行业发展,不仅运营商失望了,手机企业也失望了

CUDA programming-04: CUDA memory model

低成本、低门槛、易部署,4800+万户中小企业数字化转型新选择

Unity3D 2021软件安装包下载及安装教程

NiO example

一些实用、常用、效率越来越高的 Kubernetes 别名

【微服务~Sentinel】Sentinel之dashboard控制面板

Rewrite the tensorrt version deployment code of yolox

Understand various IOU loss functions in target detection

Explain cache consistency and memory barrier
随机推荐
async/await的执行顺序以及宏任务和微任务
被三星和台积电挤压的Intel终放下身段,为中国芯片定制芯片工艺
【Flutter -- GetX】准备篇
Arm undefined instruction exception assembly
CUDA programming-02: first knowledge of CUDA Programming
Sliding conflict of view
User management - restrictions
JS detects whether the client software is installed
Unity3D 2021软件安装包下载及安装教程
NIO this.selector.select()
redis 网络IO
【微服务~Sentinel】Sentinel之dashboard控制面板
Unity3d 2021 software installation package download and installation tutorial
flex布局 (实战小米官网)
MySQL基础知识学习(一)
The wechat installation package has soared from 0.5m to 260m. Why are our programs getting bigger and bigger?
JS检测客户端软件是否安装
4274. 后缀表达式
Blueprint class view method
MySQL Express