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 degrees
  • 1 <= x <= 9: move forward x units

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:

  1. 0 <= commands.length <= 10000
  2. 0 <= obstacles.length <= 10000
  3. -30000 <= obstacle[i][0] <= 30000
  4. -30000 <= obstacle[i][1] <= 30000
  5. 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

类似题目:

Robot Room Cleaner

参考资料:

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 走路机器人仿真的更多相关文章

  1. leetcode 874. Walking Robot Simulation

    874. Walking Robot Simulation https://www.cnblogs.com/grandyang/p/10800993.html 每走一步(不是没走commands里的一 ...

  2. 【Leetcode_easy】874. Walking Robot Simulation

    problem 874. Walking Robot Simulation solution1: 思路:1)如何表示移动的方向以及移动的位置坐标; 2)障碍物坐标如何检查;3)求解的是最大距离; cl ...

  3. 【LeetCode】874. Walking Robot Simulation 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 模拟 日期 题目地址:https://leetcod ...

  4. 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 ...

  5. LeetCode.874-走路机器人模拟(Walking Robot Simulation)

    这是悦乐书的第335次更新,第360篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第205题(顺位题号是874).网格上的机器人从点(0,0)开始并朝北.机器人可以接收三 ...

  6. C#LeetCode刷题之#874-模拟行走机器人​​​​​​​(Walking Robot Simulation)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4038 访问. 机器人在一个无限大小的网格上行走,从点 (0, 0 ...

  7. [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 ...

  8. Leetcode874.Walking Robot Simulation模拟行走的机器人

    机器人在一个无限大小的网格上行走,从点 (0, 0) 处开始出发,面向北方.该机器人可以接收以下三种类型的命令: -2:向左转 90 度 -1:向右转 90 度 1 <= x <= 9:向 ...

  9. 机器人与机器人仿真技术(zz)

    http://www.viblue.com/archives/5587.htm 一.机器人简介: 机器人(Robot)是自动执行工作的机器装置.它既可以接受人类指挥,又可以运行预先编排的程序,也可以根 ...

随机推荐

  1. R语言填充空缺值

    在R语言中, imputeMissings包的特点是,如果空值是数值型,则使用median代替,如果使用的是character类型,则使用mode值代替. imputeMissing中,需要的包是im ...

  2. linux安装redis步骤

    1.安装gcc  redis是c语言编写的 -- 安装命令 yum install gcc-c++ -- 检查gcc 是否安装 gcc -v 2.下载redis安装包,在root目录下执行 wget ...

  3. 成都,我们来啦 | Dubbo 社区开发者日

    [关注 阿里巴巴云原生 公众号,回复关键词"报名",即可参与抽奖!] 活动时间:10 月 26 日 13:00 - 18:00 活动地点:成都市高新区交子大道中海国际中心 233 ...

  4. maplotlib画柱状图并添加标签

    import json from collections import Counter import matplotlib.pyplot as plt import matplotlib as mpl ...

  5. mysql只显示表名和备注

    查看某个数据下的表及其备注: select table_name,table_comment from information_schema.tables where table_schema='db ...

  6. easyui datagird 解决行高不一致问题!

    <style>.datagrid-btable .datagrid-cell {padding: 6px 4px;overflow: hidden;text-overflow: ellip ...

  7. canvas的常用功能(电脑版)

    前言: canvas可以单独算为前端的一大知识模块, 今天就研究一下. 先做下前文铺垫: ①创建canvas <canvas id="myCanvas" width=&quo ...

  8. Android探索之百度地图开发

    目录 前言 地图图层介绍 地图覆盖物介绍 地图事件 POI检索 公交线路查询 线路规划 地理编码 @ 前言 之前自己在做一个小项目时涉及到了百度地图的一些内容,当时因为对百度地图的开发流程不是很了解, ...

  9. ES6的export与Nodejs的module.exports比较

    首先我们要明白一个前提,CommonJS模块规范和ES6模块规范完全是两种不同的概念. CommonJS模块规范 Node应用由模块组成,采用CommonJS模块规范. 根据这个规范,每个文件就是一个 ...

  10. Python 安装第三方库,pip install 安装慢,安装不上的解决办法

    今天来说一下,有些刚刚接触python的朋友,在使用pip install安装python 第三方库的过程中 会出现网速很慢,或者是安装下载到中途,停止,卡主,或者是下载报错等问题.如下图: 还有一些 ...