机器人在一个无限大小的网格上行走,从点 (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. MapReduce 图解流程

    Anatomy of a MapReduce Job In MapReduce, a YARN application is called a Job. The implementation of t ...

  2. jeecmsv9-adminVue 打包出错

    F:\jeecms\jeecmsv9-adminVue>node build\build.js - building for production...Error processing file ...

  3. PAT甲级——A1055 The World's Richest

    Forbes magazine publishes every year its list of billionaires based on the annual ranking of the wor ...

  4. HttpComponents了解

    原文地址:http://blog.csdn.net/jdluojing/article/details/7300428 1 简介 超文本传输协议(http)是目前互联网上极其普遍的传输协议,它为构建功 ...

  5. Python实现十大经典排序算法(史上最简单)

    十大排序算法(Python实现)一. 算法介绍及相关概念解读 算法分类十种常见排序算法可以分为两大类: 非线性时间比较类排序:通过比较来决定元素间的相对次序,由于其时间复杂度不能突破O(nlogn), ...

  6. css3新特性概览

    一.特性 1.强大的选择器 2.半透明度效果的实现 3.多栏布局 4.多背景图 css3允许背景属性设置多个属性值,如:background-image,background-repeat,backg ...

  7. LA3029 City Game

    Bob is a strategy game programming specialist. In his new city building game the gaming environment ...

  8. 工控安全入门(一)—— Modbus协议

    modbus基础知识 modbus协议最初是由Modicon公司在1971年推出的全球第一款真正意义上用于工业现场的总线协议,最初是为了实现串行通信,运用在串口(如RS232.RS485等)传输上的, ...

  9. 使用Jedis操作Redis-使用Java语言在客户端操作---对key的操作

    //添加String类型的模拟数据. jedis.set("mykey", "2"); jedis.set("mykey2", " ...

  10. Nginx1.10.2安装配置

    以下操作均在当前用户目录操作(root的目录,截止2016.11.2最新版本) 1.安装常用工具及基础包:yum -y install wget git vim make gcc gcc-c++ op ...