874. Walking Robot Simulation

https://www.cnblogs.com/grandyang/p/10800993.html

每走一步(不是没走commands里的一个数字)计算到原点的距离,每走一步都可能遇到障碍物,需要将障碍物的坐标进行存储,以判断是否停止行走。

左转90度,右转90度转换成在x、y的坐标变换上来。左转前进一个index,右转前进一个index,对4取余确保不越界

不知道为什么用unordered_set就编译出错。

class Solution {
public:
int robotSim(vector<int>& commands, vector<vector<int>>& obstacles) {
int x = ,y = ,res = ,index = ;
set<pair<int,int> > s;
for(int i = ;i < obstacles.size();i++)
s.insert({obstacles[i][],obstacles[i][]});
vector<int> x_move{,,,-},y_move{,,-,};
for(int command : commands){
if(command == -)
index = (index + )%;
else if(command == -)
index = (index - + )%;
else{
while(command-- > && !s.count(make_pair(x + x_move[index],y + y_move[index]))){
x += x_move[index];
y += y_move[index];
}
}
res = max(res,x*x + y*y);
}
return res;
}
};

leetcode 874. Walking Robot Simulation的更多相关文章

  1. [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 ...

  2. 【Leetcode_easy】874. Walking Robot Simulation

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

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

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

  4. 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 ...

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

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

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

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

  7. [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 ...

  8. Leetcode874.Walking Robot Simulation模拟行走的机器人

    机器人在一个无限大小的网格上行走,从点 (0, 0) 处开始出发,面向北方.该机器人可以接收以下三种类型的命令: -2:向左转 90 度 -1:向右转 90 度 1 <= x <= 9:向 ...

  9. 【leetcode】657. Robot Return to Origin

    Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...

随机推荐

  1. Mysql开启GTID后遇到错误跳过方法

    处理方法如下: 一:跳过错误 这个GTID_NEXT的4,是在master 上正常执行的最大id + 1,即Executed_Gtid_Set里面master uuid执行过的最大值 3+ 1 STO ...

  2. substr()用法

    知识点链接:http://www.cplusplus.com/reference/string/string/substr/ 注意: std::string str2 = str.substr (po ...

  3. Luogu P1627 中位数

    Luogu P1627 中位数 先记录目标数的位置,并且把数组映射为: $$a[i]=\begin{cases}-1,a[i]<b\0,a[i]=b\1,a[i]>b\end{cases} ...

  4. LeetCode - 206、反转链表

    反转一个单链表. 示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3->2->1->NULL /** * 列表定 ...

  5. springboot学习笔记(一)

    springboot案例(一) Application.java package com.xdr.spring; import org.springframework.boot.SpringAppli ...

  6. Kotlin枚举与委托深入详解

    枚举: 基本上跟Java的差不多,这里就过一遍既可,如下: 还可以接收参数,如下: 枚举还可以定义方法,如下: 看下错误提示: 所以可以这样: 然后咱们再冒号之前定义对象,如下: 下面来使用一下: 当 ...

  7. python算法与数据结构-插入排序算法(34)

    一.插入排序的介绍 插入排序的工作方式非常像人们排序一手扑克牌一样.开始时,我们的左手为空并且桌子上的牌面朝下.然后,我们每次从桌子上拿走一张牌并将它插入左手中正确的位置.为了找到一张牌的正确位置,我 ...

  8. Route all trafic for specific ip over specific network interface

    15 I have a linux server that needs to get some routing. I'm fairly new at this and i don't find any ...

  9. java中List集合

    List集合是一个元素有序可以重复的集合,集合中每个元素都有其对应的顺序索引.List集合允许使用重复集合,前面博客写到Set不允许有重复集合.List集合可以通过索引来访问指定位置的集合元素. Li ...

  10. GITHUB添加SSH内容

    首先,你需要注册一个 github账号,最好取一个有意义的名字,比如姓名全拼,昵称全拼,如果被占用,可以加上有意义的数字. 本文中假设用户名为 chuaaqiCSDN(我的博客名的全拼) 一.gihu ...