Skip to content

Commit a10551b

Browse files
author
lucifer
committed
feat: #874
1 parent b0b69f8 commit a10551b

File tree

2 files changed

+120
-1
lines changed

2 files changed

+120
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ leetcode 题解,记录自己的 leetcode 解题之路。
144144
- [0371.sum-of-two-integers](./problems/371.sum-of-two-integers.md)
145145
- [0501.find-mode-in-binary-search-tree](./problems/501.Find-Mode-in-Binary-Search-Tree.md)🆕
146146
- [0575.distribute-candies](./problems/575.distribute-candies.md)
147-
- [1260.shift-2d-grid](./problems/1260.shift-2d-grid.md)🆕
147+
- [0874.walking-robot-simulation](./problems/874.walking-robot-simulation.md) 🆕
148+
- [1260.shift-2d-grid](./problems/1260.shift-2d-grid.md) 🆕
148149

149150
#### 中等难度
150151

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
## 题目地址(874. 模拟行走机器人)
2+
3+
https://leetcode-cn.com/problems/walking-robot-simulation/submissions/
4+
5+
## 题目描述
6+
7+
```
8+
机器人在一个无限大小的网格上行走,从点 (0, 0) 处开始出发,面向北方。该机器人可以接收以下三种类型的命令:
9+
10+
-2:向左转 90 度
11+
-1:向右转 90 度
12+
1 <= x <= 9:向前移动 x 个单位长度
13+
在网格上有一些格子被视为障碍物。
14+
15+
第 i 个障碍物位于网格点  (obstacles[i][0], obstacles[i][1])
16+
17+
如果机器人试图走到障碍物上方,那么它将停留在障碍物的前一个网格方块上,但仍然可以继续该路线的其余部分。
18+
19+
返回从原点到机器人的最大欧式距离的平方。
20+
21+
 
22+
23+
示例 1:
24+
25+
输入: commands = [4,-1,3], obstacles = []
26+
输出: 25
27+
解释: 机器人将会到达 (3, 4)
28+
示例 2:
29+
30+
输入: commands = [4,-1,4,-2,4], obstacles = [[2,4]]
31+
输出: 65
32+
解释: 机器人在左转走到 (1, 8) 之前将被困在 (1, 4) 处
33+
 
34+
35+
提示:
36+
37+
0 <= commands.length <= 10000
38+
0 <= obstacles.length <= 10000
39+
-30000 <= obstacle[i][0] <= 30000
40+
-30000 <= obstacle[i][1] <= 30000
41+
答案保证小于 2 ^ 31
42+
43+
44+
```
45+
46+
## 思路
47+
48+
这道题之所以是简单难度,是因为其没有什么技巧。你只需要看懂地图,然后把题目描述转化为代码即可。
49+
50+
唯一需要注意的是查找障碍物的时候如果你采用的是`线形查找`会很慢,很可能会超时。
51+
52+
> 我实际测试了一下,确实会超时
53+
54+
- 一种方式是使用排序,然后二分查找,如果采用基于比较的排序算法,那么这种算法的瓶颈在于排序本身,也就是$O(NlogN)$。
55+
- 另一种方式是使用集合,将 obstacles 放入集合,然后需要的时候进行查询,查询的时候的时间复杂度为$O(1)$。
56+
57+
这里我们采用第二种方式。
58+
59+
接下来我们来“翻译”一下题目。
60+
61+
- 由于机器人只能往前走。因此机器人往东西南北哪个方向走取决于它的`朝向`
62+
- 我们使用枚举来表示当前机器人的`朝向`
63+
- 题目只有两种方式改变`朝向`,一种是左转(-2),另一种是右转(-1)。
64+
- 题目要求的是机器人在`运动过程中距离原点的最大值`,而不是最终位置距离原点的距离。
65+
66+
为了代码书写简单,我建立了一个直角坐标系。用`机器人的朝向和 x 轴正方向的夹角度数`来作为枚举值,并且这个度数是 `0 <= deg < 360`。我们不难知道,其实这个取值就是`0`, `90`,`180`,`270` 四个值。那么当 0 度的时候,我们只需要不断地 x+1,90 度的时候我们不断地 y + 1 等等。
67+
68+
![](https://tva1.sinaimg.cn/large/006tNbRwgy1gbdnsywx97j31020r8gmt.jpg)
69+
70+
## 关键点解析
71+
72+
- 理解题意,这道题容易理解错题意,求解为`最终位置距离原点的距离`
73+
- 建立坐标系
74+
- 使用集合简化线形查找的时间复杂度。
75+
76+
## 代码
77+
78+
代码支持: Python3
79+
80+
Python3 Code:
81+
82+
```python
83+
class Solution:
84+
def robotSim(self, commands: List[int], obstacles: List[List[int]]) -> int:
85+
pos = [0, 0]
86+
deg = 90
87+
ans = 0
88+
obstaclesSet = set(map(tuple, obstacles))
89+
90+
for command in commands:
91+
if command == -1:
92+
deg = (deg + 270) % 360
93+
elif command == -2:
94+
deg = (deg + 90) % 360
95+
else:
96+
if deg == 0:
97+
i = 0
98+
while i < command and not (pos[0] + 1, pos[1]) in obstaclesSet:
99+
pos[0] += 1
100+
i += 1
101+
if deg == 90:
102+
i = 0
103+
while i < command and not (pos[0], pos[1] + 1) in obstaclesSet:
104+
pos[1] += 1
105+
i += 1
106+
if deg == 180:
107+
i = 0
108+
while i < command and not (pos[0] - 1, pos[1]) in obstaclesSet:
109+
pos[0] -= 1
110+
i += 1
111+
if deg == 270:
112+
i = 0
113+
while i < command and not (pos[0], pos[1] - 1) in obstaclesSet:
114+
pos[1] -= 1
115+
i += 1
116+
ans = max(ans, pos[0] ** 2 + pos[1] ** 2)
117+
return ans
118+
```

0 commit comments

Comments
 (0)