874.Walking Robot Simulation (list not to be hashed)

Source: Internet
Author: User

A robot on the infinite grid starts at point (0, 0) and faces north. The robot can receive one of three possible types of commands:

    • -2:turn left degrees
    • -1:turn Right Degrees
    • 1 <= x <= 9:move forward x units

Some of the grid squares is obstacles.

The i-th obstacle is at a 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 FOLLOWI Ng the rest of the route.)

Return the square of the maximum Euclidean distance that the robot would be is from the origin.

Example 1:

Input:commands = [4,-1,3], obstacles = []
Output:25
Explanation:robot'll go to (3, 4)

Example 2:

Input:commands = [4,-1,4,-2,4], obstacles = [[2,4]]
Output:65
Explanation:robot'll be stuck @ (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 being less than 2 ^ 31.

Solution1: (TLE)

Class Solution:def Robotsim (self, commands, obstacles): "" ": Type Commands:list[int]: type Obsta            Cles: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 Direc                    Tion==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) # PR Int (' POS: ', POS, "direction:", direction) res = MAX (pos[0] * pos[0] + pos[1] * pos[1], res) return res

There is a result can not, think of its solution, draw a picture control walked again also not wrong. Finally, it is found that the result of the return is the maximum from the origin at any point in the middle, not the final result, too much pit.

Solution2:

Class Solution:def Robotsim (self, commands, obstacles): "" ": Type Commands:list[int]: type Obsta         Cles: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) Els  E:direction = Turn (direction,i) # print (' pos: ', POS, "direction:", direction) res = Max (Pos[0] * pos[0] + pos[1] * pos[1], res) return res

Instead of a list, the set store obstacle instead of a time-out. The set lookup element is a hash method, requires O (1), and List is O (n).
Note that the list is not hashed, so a tuple is used to store it in the set.

874.Walking Robot Simulation (list not to be hashed)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.