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 ...
随机推荐
- IDEA实用教程(十)—— 配置Maven的全局设置
使用之前需要提前安装好Maven 第一步 第二步
- React使用axios请求并渲染数据
一.什么是 axios Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中 二.axios 的安装 1.在项目根目录终端引入: npm isntall -- ...
- c# 定时关闭 MessageBox 或弹出的模态窗口
我们都知道,MessageBox弹出的窗口是模式窗口,模式窗口会自动阻塞父线程的.所以如果有以下代码: MessageBox.Show("内容',"标题"); 则只有关闭 ...
- rxjs 入门--环境配置
原文: https://codingthesmartway.com/getting-started-with-rxjs-part-1-setting-up-the-development-enviro ...
- 一 创建一个springboot项目之(微信点餐系统的设计与开发)
第一步:收到项目需求,进行数据库表的设计. 1.角色的划分: 卖家: 订单,类目 买家: 商品列表 2.功能模块的划分: 商品:商品列表 订单: 订单创建,订单查询,订单取消 类目:基于管理的功 ...
- POJ3709 K-Anonymous Sequence
题意 Language:Default K-Anonymous Sequence Time Limit: 4000MS Memory Limit: 65536K Total Submissions: ...
- STAF Trust Level 4 required for FS copy request
C#中使用STAF从本机传输文件到远程的电脑,出现如下错误: 解决方法: 修改service.ini文件,添加信任IP段,并将trust level 设置为5,修改后文件内容如下 trace enab ...
- Stone Game II
Description There is a stone game.At the beginning of the game the player picks n piles of stones in ...
- YAML_09 脚本调用变量+触发器
ansible]# vim adhttp2.yml --- - hosts: cache remote_user: root vars: server: httpd tasks: ...
- C二维数组用指针地址遍历
#include <stdio.h> #include <stdlib.h> int main(){ int a = 100; void *p = &a; printf ...