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

Leetcode874.Walking Robot Simulation模拟行走的机器人的更多相关文章

  1. leetcode 874. Walking Robot Simulation

    874. Walking Robot Simulation https://www.cnblogs.com/grandyang/p/10800993.html 每走一步(不是没走commands里的一 ...

  2. 【Leetcode_easy】874. Walking Robot Simulation

    problem 874. Walking Robot Simulation solution1: 思路:1)如何表示移动的方向以及移动的位置坐标; 2)障碍物坐标如何检查;3)求解的是最大距离; cl ...

  3. [Swift]LeetCode874. 模拟行走机器人 | Walking Robot Simulation

    A robot on an infinite grid starts at point (0, 0) and faces north.  The robot can receive one of th ...

  4. C#LeetCode刷题之#874-模拟行走机器人​​​​​​​(Walking Robot Simulation)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4038 访问. 机器人在一个无限大小的网格上行走,从点 (0, 0 ...

  5. LeetCode.874-走路机器人模拟(Walking Robot Simulation)

    这是悦乐书的第335次更新,第360篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第205题(顺位题号是874).网格上的机器人从点(0,0)开始并朝北.机器人可以接收三 ...

  6. [LeetCode] 874. Walking Robot Simulation 走路机器人仿真

    A robot on an infinite grid starts at point (0, 0) and faces north.  The robot can receive one of th ...

  7. 【LeetCode】874. Walking Robot Simulation 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 模拟 日期 题目地址:https://leetcod ...

  8. 874. Walking Robot Simulation

    A robot on an infinite grid starts at point (0, 0) and faces north.  The robot can receive one of th ...

  9. [ACM] hdu 1035 Robot Motion (模拟或DFS)

    Robot Motion Problem Description A robot has been programmed to follow the instructions in its path. ...

随机推荐

  1. Konig定理及证明

    Konig定理 由匈牙利数学家柯尼希(D.Konig)于1913年首先陈述的定理. 定理的内容:在0-1矩阵中,1的最大独立集合最小覆盖包含的元素个数相同,等价地,二分图中的最大匹配数等于这个图中的最 ...

  2. Luogu P4933 大师(dp)

    P4933 大师 题意 题目背景 建筑大师最近在跟着数学大师ljt12138学数学,今天他学了等差数列,ljt12138决定给他留一道练习题. 题目描述 ljt12138首先建了\(n\)个特斯拉电磁 ...

  3. Django项目:CMDB(服务器硬件资产自动采集系统)--09--06CMDB测试Linux系统采集硬件数据的命令04

    root 123456 ip addr init 0 root 123456 ip addr root 123456 python3 yum -y install zlib-devel bzip2-d ...

  4. 深入浅出 Java Concurrency (7): 锁机制 part 2 AQS[转]

    在理解J.U.C原理以及锁机制之前,我们来介绍J.U.C框架最核心也是最复杂的一个基础类:java.util.concurrent.locks.AbstractQueuedSynchronizer. ...

  5. Django-rest Framework(六)

    不懂使用机制的直接看源码就好了,也不是很难,能够看得懂 视图家族 1. View:将请求方式与视图类的同名方法建立映射,完成请求响应(原生django) from django.views impor ...

  6. Leetcode589.N-ary Tree Preorder TraversalN叉树的前序遍历

    给定一个 N 叉树,返回其节点值的前序遍历. class Node { public: int val; vector<Node*> children; Node() {} Node(in ...

  7. 百度地图JavaScript API申请密钥注意要点

    1.应用类型:浏览器端 2.启用服务:Javascript API要勾选 3.IP白名单:*即可

  8. Web应用托管服务(Web+)隐藏的十个上云最佳姿势

    随着云计算浪潮的推进,技术架构云化已经成为大势所趋.特别是最近由CNCF推动的云原生概念,将符合云原生标准的各种开源技术方案推向了前所未有的高度.在这一波浪潮的推动下,越来越多的企业开始了自身的数字化 ...

  9. LuceneNet 搜索一

    1.引用读取PDF文件组件 FontBox-0.1.0-dev.dll IKVM.GNU.Classpath.dll IKVM.Runtime.dll PDFBox-0.7.3.dll 2.添加off ...

  10. 转var,let,const,js严格模式的详解

    最近看微信公众账号/知乎网上的文章说,现在的前端的人都注重用什么框架,一问原生js感觉都没有用到工作中.用不到的,学这些意义没有.上午我刚面试了一个前端,工作4年吧.最初是北大青鸟培训的,做后端.ne ...