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. LG4783 【模板】矩阵求逆

    P4783 [模板]矩阵求逆 题目描述 求一个$N\times N$的矩阵的逆矩阵.答案对$10^9+7$取模. 输入输出格式 输入格式: 第一行有一个整数$N$,代表矩阵的大小: 从第$2$行到第$ ...

  2. Eclipse中安装Spring IDE

    ====>在线安装 1.寻找Spring IDE插件更新地址:http://marketplace.eclipse.org/content/spring-ide 2.复制对应Eclipse版本的 ...

  3. c++ templat乱测

    该上机实验环境 linux mint  IDE:qt5.11   代码复制到windows下vs2017报错,提示char* 类型不能直接赋值字符串 在linux mint下可以运行,测试目的:检验复 ...

  4. 使用fastjson 进行jsonObject转实体类对象

    使用fastjson 进行jsonObject转实体类对象   1 <dependency> 2 <groupId>com.alibaba</groupId> 3 ...

  5. P4462 [CQOI2018]异或序列 莫队

    题意:给定数列 \(a\) 和 \(k\) ,询问区间 \([l,r]\) 中有多少子区间满足异或和为 \(k\). 莫队.我们可以记录前缀异或值 \(a_i\),修改时,贡献为 \(c[a_i\bi ...

  6. 【洛谷P4245】 【模板】任意模数NTT

    三模数 NTT,感觉不是很难写 $?$ 代码借鉴的 https://www.cnblogs.com/Mychael/p/9297652.html code: #include <bits/std ...

  7. learning java AWT 常见组件

    import javax.swing.*; import java.awt.*; public class CommonComponent { Frame f = new Frame("te ...

  8. CSS滑动门

    如下图所示,每个导航栏目的文字个数不一样多,有的长,有的短,如何用一张背景图片,适应不同字数”的导航条,“滑动门”设计的非常巧妙,采用两个相同的背景图像,一左一右,一个负责提供左边框,一个负责提供右边 ...

  9. [分享]Hidden Start - NTWind Software

    https://bbs.pediy.com/thread-229336.htm   [[other]] [分享]Hidden Start - NTWind Software 2018-6-29 09: ...

  10. shell脚本之文件操作

    文件测试运算符 文件测试运算符用于检测 Unix 文件的各种属性. 下面最常用的是 -f 参数 来检查是否是文件 #!/bin/bash #start nginx php-fpm list1=&quo ...