原文链接: https://leetcode-cn.com/problems/walking-robot-simulation
英文原文
A robot on an infinite XY-plane starts at point (0, 0)
facing north. The robot can receive a sequence of these three possible types of commands
:
-2
: Turn left90
degrees.-1
: Turn right90
degrees.1 <= k <= 9
: Move forwardk
units, one unit at a time.
Some of the grid squares are obstacles
. The ith
obstacle is at grid point obstacles[i] = (xi, yi)
. If the robot runs into an obstacle, then it will instead stay in its current location and move on to the next command.
Return the maximum Euclidean distance that the robot ever gets from the origin squared (i.e. if the distance is 5
, return 25
).
Note:
- North means +Y direction.
- East means +X direction.
- South means -Y direction.
- West means -X direction.
Example 1:
Input: commands = [4,-1,3], obstacles = [] Output: 25 Explanation: The robot starts at (0, 0): 1. Move north 4 units to (0, 4). 2. Turn right. 3. Move east 3 units to (3, 4). The furthest point the robot ever gets from the origin is (3, 4), which squared is 32 + 42 = 25 units away.
Example 2:
Input: commands = [4,-1,4,-2,4], obstacles = [[2,4]] Output: 65 Explanation: The robot starts at (0, 0): 1. Move north 4 units to (0, 4). 2. Turn right. 3. Move east 1 unit and get blocked by the obstacle at (2, 4), robot is at (1, 4). 4. Turn left. 5. Move north 4 units to (1, 8). The furthest point the robot ever gets from the origin is (1, 8), which squared is 12 + 82 = 65 units away.
Example 3:
Input: commands = [6,-1,-1,6], obstacles = [] Output: 36 Explanation: The robot starts at (0, 0): 1. Move north 6 units to (0, 6). 2. Turn right. 3. Turn right. 4. Move south 6 units to (0, 0). The furthest point the robot ever gets from the origin is (0, 6), which squared is 62 = 36 units away.
Constraints:
1 <= commands.length <= 104
commands[i]
is either-2
,-1
, or an integer in the range[1, 9]
.0 <= obstacles.length <= 104
-3 * 104 <= xi, yi <= 3 * 104
- The answer is guaranteed to be less than
231
.
中文题目
机器人在一个无限大小的 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
通过代码
官方题解
方法:情景模拟
思路
我们将一步步模拟机器人的路径。由于最多只有 90000 步,这足以通过给定的输入限制。
算法
我们将会存储机器人的位置和方向。如果机器人得到转弯的指令,我们就更新方向;否则就沿给定的方向走指定的步数。
必须注意使用 集合 Set
作为对障碍物使用的数据结构,以便我们可以有效地检查下一步是否受阻。如果不这样做,我们检查 该点是障碍点吗
可能会慢大约 10000 倍。
在某些语言中,我们需要将每个障碍物的坐标编码为 长整型 long
数据,以便它可以放入 集合 Set
数据结构中。或者,我们也可以将坐标编码为 字符串 string
。
class Solution {
public:
int robotSim(vector<int>& commands, vector<vector<int>>& obstacles) {
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int x = 0, y = 0, di = 0;
unordered_set<pair<int, int>> obstacleSet;
for (vector<int> obstacle: obstacles)
obstacleSet.insert(make_pair(obstacle[0], obstacle[1]));
int ans = 0;
for (int cmd: commands) {
if (cmd == -2)
di = (di + 3) % 4;
else if (cmd == -1)
di = (di + 1) % 4;
else {
for (int k = 0; k < cmd; ++k) {
int nx = x + dx[di];
int ny = y + dy[di];
if (obstacleSet.find(make_pair(nx, ny)) == obstacleSet.end()) {
x = nx;
y = ny;
ans = max(ans, x*x + y*y);
}
}
}
}
return ans;
}
};
class Solution {
public int robotSim(int[] commands, int[][] obstacles) {
int[] dx = new int[]{0, 1, 0, -1};
int[] dy = new int[]{1, 0, -1, 0};
int x = 0, y = 0, di = 0;
// Encode obstacles (x, y) as (x+30000) * (2^16) + (y+30000)
Set<Long> obstacleSet = new HashSet();
for (int[] obstacle: obstacles) {
long ox = (long) obstacle[0] + 30000;
long oy = (long) obstacle[1] + 30000;
obstacleSet.add((ox << 16) + oy);
}
int ans = 0;
for (int cmd: commands) {
if (cmd == -2) //left
di = (di + 3) % 4;
else if (cmd == -1) //right
di = (di + 1) % 4;
else {
for (int k = 0; k < cmd; ++k) {
int nx = x + dx[di];
int ny = y + dy[di];
long code = (((long) nx + 30000) << 16) + ((long) ny + 30000);
if (!obstacleSet.contains(code)) {
x = nx;
y = ny;
ans = Math.max(ans, x*x + y*y);
}
}
}
}
return ans;
}
}
class Solution(object):
def robotSim(self, commands, obstacles):
dx = [0, 1, 0, -1]
dy = [1, 0, -1, 0]
x = y = di = 0
obstacleSet = set(map(tuple, obstacles))
ans = 0
for cmd in commands:
if cmd == -2: #left
di = (di - 1) % 4
elif cmd == -1: #right
di = (di + 1) % 4
else:
for k in xrange(cmd):
if (x+dx[di], y+dy[di]) not in obstacleSet:
x += dx[di]
y += dy[di]
ans = max(ans, x*x + y*y)
return ans
复杂度分析
- 时间复杂度:$O(N + K)$,其中 $N, K$ 分别是
commands
和obstacles
的长度。 - 空间复杂度:$O(K)$,用于存储
obstacleSet
而使用的空间。
统计信息
通过次数 | 提交次数 | AC比率 |
---|---|---|
21369 | 51021 | 41.9% |
提交历史
提交时间 | 提交结果 | 执行时间 | 内存消耗 | 语言 |
---|