[LeetCode] 874. Walking Robot Simulation 走路机器人仿真
A robot on an infinite grid starts at point (0, 0) and faces north. The robot can receive one of three possible types of commands:
-2: turn left 90 degrees-1: turn right 90 degrees1 <= x <= 9: move forwardxunits
Some of the grid squares are obstacles.
The i-th obstacle is at grid point (obstacles[i][0], obstacles[i][1])
If the robot would try to move onto them, the robot stays on the previous grid square instead (but still continues following the rest of the route.)
Return the square of the maximum Euclidean distance that the robot will be from the origin.
Example 1:
Input: commands = [4,-1,3], obstacles = []
Output: 25
Explanation: robot will go to (3, 4)
Example 2:
Input: commands = [4,-1,4,-2,4], obstacles = [[2,4]]
Output: 65
Explanation: robot will be stuck at (1, 4) before turning left and going to (1, 8)
Note:
0 <= commands.length <= 100000 <= obstacles.length <= 10000-30000 <= obstacle[i][0] <= 30000-30000 <= obstacle[i][1] <= 30000- The answer is guaranteed to be less than
2 ^ 31.
这道题说在一个无限大的方格中,原点位置有一个面朝北方的机器人,可以接受三种不同的指令,-2 表示左转 90 度,-1 表示右转 90 度,正数表示沿当前方向前进该正数步,然后给了一个障碍数组,就是机器人无法通过的地方,比如当前机器人面前有个障碍物,此时机器人接到的指令是前进x步,但是由于障碍物的因素,该指令无法执行,机器人呆在原地不动。只有接到转向的命令,才有可能离开当前位置。好,理解了题意之后,来思考如何解题,说到底还是一道迷宫遍历的题目,但是跟以往不同的是,这里我们并不知道迷宫的大小,而且也没有目标点需要到达,唯一需要做的就是执行指令,当指令执行完了之后,搜索也就停止了,需要找出的是机器人能到达的距离原点最远的距离。首先对于障碍物,由于肯定要经常的查找下一个位置是否有障碍物,那么就用一个 HashSet 将所有的障碍物位置存进去,由于坐标是二维的,可以进行降维处理,之前我们都是进行 i*n+j 的降维处理,这里由于不知道迷宫的大小,所以只能换一种方式,可以将横纵坐标都转为字符串,然后在中间加个短横杆隔开,这样就可以把二维坐标变为一个字符串,放到 HashSet 中了。然后就是处理所有的命令了,在传统的迷宫遍历中,使用了方向数组,来控制遍历的方向,这里也是同样需要的,但稍有不同的是,这里方向的顺序也有讲究,因为机器人的初始状态是朝北的,所以方向数组的第一个应该是朝北走,上北下南左西右东,这样方向数组的顺序应该是上右下左。用一个变量 idx 来表示方向数组中的当前坐标,那么当遇到 -1,即右转时,idx 自增1即可,为了防止越界,需要对4取余。同理,当遇到 -2,即左转时,idx 自减1即可,同样为了防止越界,先加4,再对4取余。当遇到正数命令时,此时就该前进了,用两个变量x和y分别表示当前位置的横纵坐标,均初始化为0,分别加上方向数组中对应位置的值,就是下一个位置的坐标了,在经典的迷宫遍历问题中,我们都会检验新位置是否越界,以及是否访问过,而这里不存在越界的问题,访没访问过也无所谓,重要是看有没有障碍物,到 HashSet 中去查找一下,若没有障碍物,则可以到达,更新x和y为新的位置,继续 while 循环即可。当每个命令执行完了之后,都用当前的x和y距离原点的距离更新一个结果 res 即可,参见代码如下:
解法一:
class Solution {
public:
int robotSim(vector<int>& commands, vector<vector<int>>& obstacles) {
int res = 0, x = 0, y = 0, idx = 0;
unordered_set<string> obs;
for (auto a : obstacles) obs.insert(to_string(a[0]) + "-" + to_string(a[1]));
vector<int> dirX{0, 1, 0, -1}, dirY{1, 0, -1, 0};
for (int command : commands) {
if (command == -1) idx = (idx + 1) % 4;
else if (command == -2) idx = (idx - 1 + 4) % 4;
else {
while (command-- > 0 && !obs.count(to_string(x + dirX[idx]) + "-" + to_string(y + dirY[idx]))) {
x += dirX[idx];
y += dirY[idx];
}
}
res = max(res, x * x + y * y);
}
return res;
}
};
下面这种解法跟上面没有什么区别,就是集合的保存类型稍有不同,上面的解法中我们对二维坐标压缩成了字符串,这里直接用个 pair 对儿来保存横纵坐标,需要注意的是,使用 pair 对儿的话就不能用 HashSet 了,只能用 TreeSet,其余地方基本不变,参见代码如下:
解法二:
class Solution {
public:
int robotSim(vector<int>& commands, vector<vector<int>>& obstacles) {
int res = 0, x = 0, y = 0, idx = 0;
set<pair<int, int>> obs;
for (auto a : obstacles) obs.insert({a[0], a[1]});
vector<int> dirX{0, 1, 0, -1}, dirY{1, 0, -1, 0};
for (int command : commands) {
if (command == -1) idx = (idx + 1) % 4;
else if (command == -2) idx = (idx - 1 + 4) % 4;
else {
while (command-- > 0 && !obs.count({x + dirX[idx], y + dirY[idx]})) {
x += dirX[idx];
y += dirY[idx];
}
}
res = max(res, x * x + y * y);
}
return res;
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/874
类似题目:
参考资料:
https://leetcode.com/problems/walking-robot-simulation/
https://leetcode.com/problems/walking-robot-simulation/discuss/152322/Maximum!-This-is-crazy!
[LeetCode All in One 题目讲解汇总(持续更新中...)](https://www.cnblogs.com/grandyang/p/4606334.html)
[LeetCode] 874. Walking Robot Simulation 走路机器人仿真的更多相关文章
- leetcode 874. Walking Robot Simulation
874. Walking Robot Simulation https://www.cnblogs.com/grandyang/p/10800993.html 每走一步(不是没走commands里的一 ...
- 【Leetcode_easy】874. Walking Robot Simulation
problem 874. Walking Robot Simulation solution1: 思路:1)如何表示移动的方向以及移动的位置坐标; 2)障碍物坐标如何检查;3)求解的是最大距离; cl ...
- 【LeetCode】874. Walking Robot Simulation 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 模拟 日期 题目地址:https://leetcod ...
- 874. Walking Robot Simulation
A robot on an infinite grid starts at point (0, 0) and faces north. The robot can receive one of th ...
- LeetCode.874-走路机器人模拟(Walking Robot Simulation)
这是悦乐书的第335次更新,第360篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第205题(顺位题号是874).网格上的机器人从点(0,0)开始并朝北.机器人可以接收三 ...
- C#LeetCode刷题之#874-模拟行走机器人(Walking Robot Simulation)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4038 访问. 机器人在一个无限大小的网格上行走,从点 (0, 0 ...
- [Swift]LeetCode874. 模拟行走机器人 | Walking Robot Simulation
A robot on an infinite grid starts at point (0, 0) and faces north. The robot can receive one of th ...
- Leetcode874.Walking Robot Simulation模拟行走的机器人
机器人在一个无限大小的网格上行走,从点 (0, 0) 处开始出发,面向北方.该机器人可以接收以下三种类型的命令: -2:向左转 90 度 -1:向右转 90 度 1 <= x <= 9:向 ...
- 机器人与机器人仿真技术(zz)
http://www.viblue.com/archives/5587.htm 一.机器人简介: 机器人(Robot)是自动执行工作的机器装置.它既可以接受人类指挥,又可以运行预先编排的程序,也可以根 ...
随机推荐
- vue自定义事件---拖拽
margin布局拖拽 Vue.directive('drag', { bind(el, binding, vnode, oldVnode) { const dialogHeaderEl = el.qu ...
- 项目整合SpringDataRedis
1:准备工作 先导入redis和jedis依赖,在配置redis-config.properties 和applicationContext-redis.xml (详细配置信息及入门demo见我上一篇 ...
- idea中的后缀补全
IDEA有个很牛逼的功能,那就是后缀补全(Postfix Completion),这个功能可以通过后缀来使用代码补全进行模板式地补全语句,如遍历循环语句(for.foreach).使用 String. ...
- IDEA设置外部比对工具Beyond Compare
设置IDEA使用外部的比对工具,比如Beyond Compare,其实很简单,但是可能好几年才会设置一次,比如换工作的时候,所以记录下来 可以通过菜单File-Settings 或者直接快捷键ctrl ...
- JVM的监控工具之jvisual
VisualVM(All-in-One Java Trouble shootingTool)是到目前为止随JDK发布的功能最强大的运行监视和故障处理程序,并且可以预见在未来一段时间内都是官方主力发展的 ...
- Beats Elastic中的Auditbeat使用介绍
Auditbeat使用介绍 Auditbeat是一种轻量级的数据收集器,您可以将其安装在服务器上,以审核系统上用户和进程的活动. 例如,您可以使用Auditbeat从Linux Audit Frame ...
- Django+xadmin打造在线教育平台(十一)
十.首页模块 1.首页展示 (1).视图函数 def index(request): all_banners = BannerInfo.objects.all().order_by('-add_tim ...
- element-ui Upload 上传组件源码分析整理笔记(十四)
简单写了部分注释,upload-dragger.vue(拖拽上传时显示此组件).upload-list.vue(已上传文件列表)源码暂未添加多少注释,等有空再补充,先记下来... index.vue ...
- ios 点击webview获取图片url (js交互)
加手势 -(void)handleSingleTap:(UITapGestureRecognizer *)sender { CGPoint pt = [sender locationInView:_c ...
- Hadoop 从节点的 NodeManager 无法启动
一.问题描述 日志文件信息如下: -- ::, INFO nodemanager.NodeManager (LogAdapter.java:info()) - registered UNIX sign ...