• 1493. 模拟行走机器人


    1493. 模拟行走机器人

    中文English

    机器人在一个无限大小的网格上行走,从点 (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
    输入测试数据 (每行一个参数)如何理解测试数据?
    class Solution:
        """
        @param commands: type: List[int]
        @param obstacles: type: List[List[int]]
        @return: Return the square of the maximum Euclidean distance
        """
        '''
        大致思路:
        1.初始化p='top',dic = ['top','right','down','left'],c=[0,0]最开始的坐标,dic_x,dic_y分别代表往这个方向走则是加还是减。
        2.while commands  != [],循环,每次pop掉首个值,如果当前值== -1,则取后一个值。如果当前p为left and pop_num == -2,则置0。 -2一样
        3.如果当前值是大于0的话,则根据对应的p,进行该方向的行走,同时记录该坐标,如果该坐标的下一位存在障碍物列表里面的话,则continue
        4.最终返回x和y轴的平方和即可
        '''
        def robotSim(self, commands, obstacles):
            p = 'top'
            dic = ['top','right','down','left']
            dic_x = {'right':1,'left':-1}
            dic_y = {'top':1,'down':-1}
            c = [0,0]
    
            while commands != []:
                pop_num = commands.pop(0)
                
                #反向的转换
                if pop_num < 0:
                    #向右转
                    if pop_num == -1:
                        if p != 'left':
                            p = dic[dic.index(p)+1]
                        else:
                            p = 'top'
    
                    #向左转
                    if pop_num == -2:
                        if p != 'top':
                            p = dic[dic.index(p)-1]
                        else:
                            p = 'left'
    
                
                #该方向的行走
                else:
                    if p in ['left','right']:
                        for i in range(pop_num):
                            x = c[0] + dic_x[p]
                            y = c[1]
                            if [x,y] in obstacles:
                                break
                            c = [x,y]
                    
                    elif p in ['top','down']:
                        for i in range(pop_num):
                            x = c[0] 
                            y = c[1] + dic_y[p]
                            if [x,y] in obstacles:
                                break     
                            c = [x,y]  
            return c[0]**2 + c[1]**2
  • 相关阅读:
    操作系统之进程篇(3)
    指针和数组及内存管理
    进程篇(3: 基本进程控制:进程的退出)--请参照本博客“操作系统”专栏
    Java面向对象程序设计--泛型编程
    进程篇(1: 进程运行环境)--请参照本博客“操作系统”专栏
    操作系统之进程篇(1)
    分类器性能指标之ROC曲线、AUC值
    如何理解似然函数?
    sigmoid函数简介
    Hive分析窗口函数
  • 原文地址:https://www.cnblogs.com/yunxintryyoubest/p/12774077.html
Copyright © 2020-2023  润新知