leetcode 874. Walking Robot Simulation
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的更多相关文章
- [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 ...
- 【Leetcode_easy】874. Walking Robot Simulation
problem 874. Walking Robot Simulation solution1: 思路:1)如何表示移动的方向以及移动的位置坐标; 2)障碍物坐标如何检查;3)求解的是最大距离; cl ...
- 【LeetCode】874. Walking Robot Simulation 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 模拟 日期 题目地址:https://leetcod ...
- 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 ...
- LeetCode.874-走路机器人模拟(Walking Robot Simulation)
这是悦乐书的第335次更新,第360篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第205题(顺位题号是874).网格上的机器人从点(0,0)开始并朝北.机器人可以接收三 ...
- C#LeetCode刷题之#874-模拟行走机器人(Walking Robot Simulation)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4038 访问. 机器人在一个无限大小的网格上行走,从点 (0, 0 ...
- [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 ...
- Leetcode874.Walking Robot Simulation模拟行走的机器人
机器人在一个无限大小的网格上行走,从点 (0, 0) 处开始出发,面向北方.该机器人可以接收以下三种类型的命令: -2:向左转 90 度 -1:向右转 90 度 1 <= x <= 9:向 ...
- 【leetcode】657. Robot Return to Origin
Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...
随机推荐
- 漫谈五种IO模型(主讲IO多路复用)
首先引用levin的回答让我们理清楚五种IO模型 1.阻塞I/O模型 老李去火车站买票,排队三天买到一张退票. 耗费:在车站吃喝拉撒睡 3天,其他事一件没干. 2.非阻塞I/O模型 老李去火车站买票, ...
- 重装windows10系统的教程
1.首先从官网上下载一个win10的系统, 2.准备一个8GB的移动U盘,下载好的镜像文件烧录在这个U盘, 3.按照不同型号的机型,使用不同的按键进入BOIS模式,然后选中U盘作为启动项,读取出来这个 ...
- 1216 Vue基础
目录 前端框架 Vue 1.简介 1.1 优点 2 使用 2.1 基础 2.2 文本指令 2.3 事件指令 2.4 属性指令 JS面向对象补充 前端框架 angular ---更新程度太快,且不向下兼 ...
- 行为型模式(十一) 解释器模式(Interpreter)
一.动机(Motivate) 在软件构建过程中,如果某一特定领域的问题比较复杂,类似的模式不断重复出现,如果使用普通的编程方式来实现将面临非常频繁的变化.在这种情况下,将特定领域的问题表达为某种语法规 ...
- vscode——配置go插件
前言 这个真的折腾死我了,看了N多博客才弄好 这两个还是比较有用的~ https://www.cnblogs.com/nickchou/p/9038114.html https://www.jians ...
- Dubbo源码分析(4):Protocol
Protocol接口是Dubbo框架的核心组件.Dubbo框架启动protocol接口实现类,由spring的xml文件配置决定.RegistryProtocol协议是Protocol协议的核心,它负 ...
- Zookeeper选举(fastleaderelection算法)
1.选举相关概念: 选票:(myid,zxid,当前节点选取轮次,被推举服务器选举轮次,状态(looking)). 选举发生情况:启动时选举,运行时选举. 外部投票:其他服务器发送来的投票. 内部投票 ...
- Greenplum FTS故障检测原理
前言 FTS(Fault Tolerance Serve)是GreenPlum中的故障检测服务,是保证GP高可用的核心功能.GreenPlum的Segment的健康检测及HA是由GP Master实现 ...
- Hadoop 副本放置策略的源码阅读和设置
本文通过MetaWeblog自动发布,原文及更新链接:https://extendswind.top/posts/technical/hadoop_block_placement_policy 大多数 ...
- 乌班图下fluent开启并行报错的解决方法
参考链接: CFD-online原帖:http://www.cfd-online.com/Forums/fluent/149668-fluent-16-0-0-ubuntu-12-04-a.html ...