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 ...
随机推荐
- dt开发之-自定义函数获取分类名称
需要在api/extend.func.php 文件中加入的函数 获取分类名称 cat_name($catid) 传入分类id function cat_name($catid) { global $d ...
- Python语言程序设计(2)--深入理解python
- JavaScript 进阶问题列表
https://github.com/lydiahallie/javascript-questions/blob/master/zh-CN/README-zh_CN.md 很考基本功
- 【Java】《Java程序设计基础教程》第三章学习
3.1 类 类在Java语言中是一种最基本的引用数据类型,是组成Java程序的基本要素.具有相同属性(状态)和方法(行为)的一组对象的集合称为类,其内部包括属性和方法两个主要部分. 3.11 类的定义 ...
- MongoDB 主从复制及 自动故障转移
1.MongoDB 主从复制 MongoDB复制是将数据同步在多个服务器的过程. 复制提供了数据的冗余备份,并在多个服务器上存储数据副本,提高了数据的可用性, 并可以保证数据的安全性. 复制还允许您从 ...
- [TypeScript ] Using the Null Coalescing operator with TypeScript 3.7
The postshows you how to use the null coalescing operator (??) instead of logical or (||) to set def ...
- 014——C#新建文件夹
(一)如果不存在路径就新建文件夹 string directory = @"C:\Users\Administrator\Desktop\温控数据\"; if (!Director ...
- 通过map文件找程序崩溃的代码行
一,配置vs 二,程序崩溃界面 // ConsoleApplication1.cpp : 此文件包含 "main" 函数.程序执行将在此处开始并结束. // #include &l ...
- nyar4psg: Cannot find a class or type named "MultiMarker"
Cannot find a class or type named "MultiMarker" 是一种常见错误,产生的原因是Library里面有1个以上的ar库. 以我的电脑为例, ...
- 数据结构Java版之红黑树(八)
红黑树是一种自动平衡的二叉查找树,因为存在红黑规则,所以有效的防止了二叉树退化成了链表,且查找和删除的速度都很快,时间复杂度为log(n). 什么是红黑规则? 1.根节点必须是黑色的. 2.节点颜色要 ...