[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)是自动执行工作的机器装置.它既可以接受人类指挥,又可以运行预先编排的程序,也可以根 ...
随机推荐
- mongodb 导出制定的查询结果
1.mongo查询语句: db.quarkContext.find({"submitTime":{"$gt":ISODate("2019-07-13T ...
- MySQL for OPS 04:存储引擎
写在前面的话 在使用 Linux 的时候,可以经常听到有关文件系统 FS(File System)的东西,MySQL 也有属于自己类似的东西,那就是存储引擎.之前在创建数据表的时候,在 Create ...
- STorM32 BGC三轴云台控制板电机驱动电路设计(驱动芯片DRV8313)
1 序言 相信对云台有兴趣的小伙伴对STorM32 BGC这块云台控制板并不陌生,虽说这块控制板的软件已经不再开源,但是在GitHub上依旧可以找到两三个版本的代码,而硬件呢我们也可以从Olliw( ...
- nodejs-翻转算法
nodejs-翻转算法 /** * Created by moon on 2019/12/14. */ //程序运行完成时一定要有输出语句,本工具才能正确展示运行结果. function abc() ...
- IntelliJ IDEA 中使用 Lambok (注解无效问题的解决)
一,概述 Lambok可以说一个能很大提高开发效率的插件,只要在使用注解的方式就能实现很多常用的功能.如常用的@Data能在编译阶段自动生成toString方法,getter方法setter方法等. ...
- 我的第一次diy装机记录——小白的装机篇
接上一篇<我的第一次diy装机记录——小白的配置篇> 处理器 AMD Ryzen 5 2600X 六核主板 微星 B450M MORTAR (MS-7B89) ( AMD PCI 标准主机 ...
- 020.Dockerfile
docker-cli读取Dockerfile,根据指令生成定制的docker镜像. Dockerfile的指令根据作用可以分为两种,构建指令和设置指令. 构建指令:用于构建image,其指定的操作不会 ...
- RIP路由协议:基础设置/通信练习/兼容问题
RIP工作原理 首先路由器学习到直连网段 路由器开始运行RIP,当路由器的更新周期30秒到了的时候,会向邻居发送路由表 Metric:度量值,衡量一条路由好坏的值.发送路由表时Metric值会加1 学 ...
- 2-3 arrary数组的数值的计算
In [2]: import numpy as np tang_array=np.array([[1,2,3],[4,5,6]]) tang_array Out[2]: array([[1, 2, 3 ...
- Flask请求和应用上下文源码分析
flask的request和session设置方式比较新颖,如果没有这种方式,那么就只能通过参数的传递. flask是如何做的呢? 1:本地线程,保证即使是多个线程,自己的值也是互相隔离 1 im ...