【LetMeFly】874.模拟行走机器人:哈希表模拟
力扣题目链接:https://leetcode.cn/problems/walking-robot-simulation/
机器人在一个无限大小的 XY 网格平面上行走,从点 (0, 0)
处开始出发,面向北方。该机器人可以接收以下三种类型的命令 commands
:
-2
:向左转90
度-1
:向右转90
度1 <= x <= 9
:向前移动x
个单位长度
在网格上有一些格子被视为障碍物 obstacles
。第 i
个障碍物位于网格点 obstacles[i] = (xi, yi)
。
机器人无法走到障碍物上,它将会停留在障碍物的前一个网格方块上,但仍然可以继续尝试进行该路线的其余部分。
返回从原点到机器人所有经过的路径点(坐标为整数)的最大欧式距离的平方。(即,如果距离为 5
,则返回 25
)
注意:
- 北表示
+Y
方向。 - 东表示
+X
方向。 - 南表示
-Y
方向。 - 西表示
-X
方向。
示例 1:
输入:commands = [4,-1,3], obstacles = [] 输出:25 解释: 机器人开始位于 (0, 0): 1. 向北移动 4 个单位,到达 (0, 4) 2. 右转 3. 向东移动 3 个单位,到达 (3, 4) 距离原点最远的是 (3, 4) ,距离为 32 + 42 = 25
示例 2:
输入:commands = [4,-1,4,-2,4], obstacles = [[2,4]] 输出:65 解释:机器人开始位于 (0, 0): 1. 向北移动 4 个单位,到达 (0, 4) 2. 右转 3. 向东移动 1 个单位,然后被位于 (2, 4) 的障碍物阻挡,机器人停在 (1, 4) 4. 左转 5. 向北走 4 个单位,到达 (1, 8) 距离原点最远的是 (1, 8) ,距离为 12 + 82 = 65
提示:
1 <= commands.length <= 104
commands[i]
is one of the values in the list[-2,-1,1,2,3,4,5,6,7,8,9]
.0 <= obstacles.length <= 104
-3 * 104 <= xi, yi <= 3 * 104
- 答案保证小于
231
方法一:哈希表模拟
先来看看数据量:最多移动 1 0 4 10^4 104次,每次最多移动 9 9 9步,完全可以一步一步地模拟。
因此,我们只需要建立一个哈希表,将所有的障碍物存入哈希表中;接下来模拟机器人的每一步,遇到障碍就停下,否则就执行命令并更新答案最大值。
细节处理:
若所选编程语言不支持直接将“障碍物”放入哈希表中,则可以将 障碍物横坐标 × 60001 + 障碍物纵坐标 障碍物横坐标 \times 60001 + 障碍物纵坐标 障碍物横坐标×60001+障碍物纵坐标放入哈希表中。这是因为地图中 x x x的范围是 60001 60001 60001
可以建立一个方向数组 d i r e c t i o n s directions directions,使用一个下标 d d d表示方向,横纵坐标分别加上 d i r e c t i o n s [ d ] directions[d] directions[d]的 [ 0 ] [0] [0]和 [ 1 ] [1] [1]即为前进一步。
- 时间复杂度 O ( l e n ( o b s t a c l e s ) + l e n ( c o m m a n d s ) ) O(len(obstacles) + len(commands)) O(len(obstacles)+len(commands)),其中每个command最多走9步,可以视为常数。
- 空间复杂度 O ( l e n ( o b s t a c l e s ) ) O(len(obstacles)) O(len(obstacles))。
AC代码
C++
const int directions[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; // ↑→↓←class Solution {
public:int robotSim(vector<int>& commands, vector<vector<int>>& obstacles) {unordered_set<int> se;for (auto& obstacle : obstacles) {se.insert(obstacle[0] * 60001 + obstacle[1]); // 30001不可,会无法通过最后一组数据,因为存在负数}int nowDirection = 0, x = 0, y = 0;int ans = 0;for (int t : commands) {if (t == -2) {nowDirection = (nowDirection + 3) % 4;}else if (t == -1) {nowDirection = (nowDirection + 1) % 4;}else {for (int i = 0; i < t; i++) {int tx = x + directions[nowDirection][0], ty = y + directions[nowDirection][1];if (se.count(tx * 60001 + ty)) {break;}x = tx, y = ty;ans = max(ans, x * x + y * y);}}}return ans;}
};
Python
# from typing import Listdirections = [[0, 1], [1, 0], [0, -1], [-1, 0]] # ↑→↓←class Solution:def robotSim(self, commands: List[int], obstacles: List[List[int]]) -> int:se = set()for obstacle in obstacles:se.add(tuple(obstacle))x, y, nowDirection = 0, 0, 0ans = 0for command in commands:if command == -2:nowDirection = (nowDirection + 3) % 4elif command == -1:nowDirection = (nowDirection + 1) % 4else:for i in range(command):tx, ty = x + directions[nowDirection][0], y + directions[nowDirection][1]if (tx, ty) in se:breakx, y = tx, tyans = max(ans, x * x + y * y)return ans
同步发文于CSDN,原创不易,转载请附上原文链接哦~
Tisfy:https://letmefly.blog.csdn.net/article/details/131800691