zoukankan      html  css  js  c++  java
  • Leetcode874.Walking Robot Simulation模拟行走的机器人

    机器人在一个无限大小的网格上行走,从点 (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) 处

    提示:

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

    题目感觉没有说清楚,应该说是过程中最大的欧式平方

    class Solution {
    public:
        int robotSim(vector<int>& commands, vector<vector<int> >& obstacles) {
            map<pair<int, int>, bool> check;
            for(int i = 0; i < obstacles.size(); i++)
            {
                check[make_pair(obstacles[i][0], obstacles[i][1])] = true;
            }
            //北东南西
            int dir = 0;
            int res = 0;
            int currentx = 0;
            int currenty = 0;
            int dx[4] = {0, 1, 0, -1};
            int dy[4] = {1, 0, -1, 0};
            for(int i = 0; i < commands.size(); i++)
            {
                if(commands[i] == -1)
                {
                    dir = (dir + 1) % 4;
                }
                else if(commands[i] == -2)
                {
                    dir = (dir + 4 - 1) % 4;
                }
                else
                {
                    int step = commands[i];
                    for(int i = 0; i < step; i++)
                    {
                        int newx = currentx + dx[dir];
                        int newy = currenty + dy[dir];
                        if(check[make_pair(newx, newy)] == false)
                        {
                            currentx = newx;
                            currenty = newy;
                            res = max(res, currentx * currentx + currenty * currenty);
                        }
                    }
                }
            }
            return res;
        }
    };
  • 相关阅读:
    Windows Embedded CE 中断结构分析
    linux framebuff驱动总结
    Linux assemblers: A comparison of GAS and NASM
    使用C#编写ICE分布式应用程序
    makefile的写法
    在客户端中如何定位服务器(即如何寻找代理)
    番茄花园洪磊: 微软很早给我发过律师函
    利用ICE编写程序的几个注意点
    ICE架构
    AT&T汇编语言与GCC内嵌汇编简介
  • 原文地址:https://www.cnblogs.com/lMonster81/p/10433920.html
Copyright © 2011-2022 走看看