zoukankan      html  css  js  c++  java
  • leetcode874. 模拟行走机器人

    机器人在一个无限大小的网格上行走,从点 (0, 0) 处开始出发,面向北方。该机器人可以接收以下三种类型的命令:

    -2:向左转 90 度
    -1:向右转 90 度
    1 <= x <= 9:向前移动 x 个单位长度
    在网格上有一些格子被视为障碍物。

    第 i 个障碍物位于网格点  (obstacles[i][0], obstacles[i][1])

    机器人无法走到障碍物上,它将会停留在障碍物的前一个网格方块上,但仍然可以继续该路线的其余部分。

    返回从原点到机器人所有经过的路径点(坐标为整数)的最大欧式距离的平方。

    示例 1:

    输入: commands = [4,-1,3], obstacles = []
    输出: 25
    解释: 机器人将会到达 (3, 4)
    示例 2:

    输入: commands = [4,-1,4,-2,4], obstacles = [[2,4]]
    输出: 65
    解释: 机器人在左转走到 (1, 8) 之前将被困在 (1, 4) 处
     

    提示:

    0 <= commands.length <= 10000
    0 <= obstacles.length <= 10000
    -30000 <= obstacle[i][0] <= 30000
    -30000 <= obstacle[i][1] <= 30000
    答案保证小于 2 ^ 31

    ******************************************

    无脑模拟会tle,就用个set

    建个直角坐标系,和x正轴所成角表示方向

    ******************************************

    class Solution:
        def robotSim(self, commands: List[int], obstacles: List[List[int]]) -> int:
            pos=[0,0]
            deg=90
            ans=0
            obstaclesSet=set(map(tuple,obstacles))
            for command in commands:
                if command==-1:
                    deg=(deg+270)%360
                elif command==-2:
                    deg=(deg+90)%360
                else:
                    if deg==0:
                        i=0
                        while i<command and not (pos[0]+1,pos[1])in obstaclesSet:
                            pos[0]+=1
                            i+=1
                    if deg==90:
                        i=0
                        while i<command and not (pos[0],pos[1]+1)in obstaclesSet:
                            pos[1]+=1
                            i+=1
                    if deg==180:
                        i=0
                        while i<command and not (pos[0]-1,pos[1])in obstaclesSet:
                            pos[0]-=1
                            i+=1
                    if deg==270:
                        i=0
                        while i<command and not (pos[0],pos[1]-1)in obstaclesSet:
                            pos[1]-=1
                            i+=1
                    ans=max(ans,pos[0]**2+pos[1]**2)
            return ans
  • 相关阅读:
    C语言_航模社第四节
    C语言_航模社第三节
    C语言交换两个变量的值
    C语言表达分段函数
    c语言_2017.10.22
    stm32_配置GPIO点亮led灯
    prteus8安装教程
    安装keil_5步骤
    nginx配置实现https的配置文件方法
    TortoiseGit 代码版本回退及返回
  • 原文地址:https://www.cnblogs.com/xxxsans/p/12986593.html
Copyright © 2011-2022 走看看