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:
- 0 <= commands.length <= 10000
- 0 <= obstacles.length <= 10000
- -30000 <= obstacle[i][0] <= 30000
- -30000 <= obstacle[i][1] <= 30000
- The answer is guaranteed to be less than 2 ^ 31.
Solution1:(TLE)
class Solution:
def robotSim(self, commands, obstacles):
"""
:type commands: List[int]
:type obstacles: List[List[int]]
:rtype: int
"""
pos = [0,0]
direction = 0
def turn(pre,new):
if new == -1:
now = (pre + 1)%4
else:
now = (pre - 1)%4
return now
def move(direction,distance):
if direction==0:
while distance>0:
distance -= 1
pos[1] += 1
if pos in obstacles:
pos[1] -= 1
break
if direction==1:
while distance>0:
distance -= 1
pos[0] += 1
if pos in obstacles:
pos[0] -= 1
break
if direction==2:
while distance>0:
distance -= 1
pos[1] -= 1
if pos in obstacles:
pos[1] += 1
break
if direction==3:
while distance>0:
distance -= 1
pos[0] -= 1
if pos in obstacles:
pos[0] += 1
break
return
res = 0
for i in commands:
# print(i)
if i>0:
move(direction,i)
# print(‘pos:‘,pos,"direction:",direction)
else:
direction = turn(direction,i)
# print(‘pos:‘,pos,"direction:",direction)
res = max(pos[0] * pos[0] + pos[1] * pos[1], res)
return res
有一个结果过不了,百思不得其解,画了图对照走了一遍也没有错。最后发现返回的结果是中间任意时刻离原点的最大值,而不是最终的结果,太坑了。
Solution2:
class Solution:
def robotSim(self, commands, obstacles):
"""
:type commands: List[int]
:type obstacles: List[List[int]]
:rtype: int
"""
s = set()
for i in obstacles:
s.add(tuple(i))
pos = [0,0]
direction = 0
def turn(pre,new):
if new == -1:
now = (pre + 1)%4
else:
now = (pre - 1)%4
return now
def move(direction,distance):
if direction==0:
while distance>0:
distance -= 1
pos[1] += 1
if tuple(pos) in s:
pos[1] -= 1
break
if direction==1:
while distance>0:
distance -= 1
pos[0] += 1
if tuple(pos) in s:
pos[0] -= 1
break
if direction==2:
while distance>0:
distance -= 1
pos[1] -= 1
if tuple(pos) in s:
pos[1] += 1
break
if direction==3:
while distance>0:
distance -= 1
pos[0] -= 1
if tuple(pos) in s:
pos[0] += 1
break
return
res = 0
for i in commands:
# print(i)
if i>0:
move(direction,i)
# print(‘pos:‘,pos,"direction:",direction)
else:
direction = turn(direction,i)
# print(‘pos:‘,pos,"direction:",direction)
res = max(pos[0] * pos[0] + pos[1] * pos[1], res)
return res
改用set存储obstacle而不是list,不再超时了。set查找元素是哈希的方法,需要O(1),而list则是O(n)。
注意list是不可以被哈希的,故改用tuple来存储到set中。
原文地址:https://www.cnblogs.com/bernieloveslife/p/9751086.html