Leetcode 874. Walking Robot Simulation
文章作者:Tyan
博客:noahsnail.com | CSDN | 简书
1. Description
2. Solution
class Solution {
public:int robotSim(vector<int>& commands, vector<vector<int>>& obstacles) {int x = 0;int y = 0;int maximum = 0;char directions[] = {'N', 'E', 'S', 'W'};int index = 0;set<pair<int, int>> obstacleSet;for (vector<int> obstacle: obstacles) {obstacleSet.insert(make_pair(obstacle[0], obstacle[1]));}for(int i = 0; i < commands.size(); i++) {if(commands[i] == -1) {index = (index + 1) % 4;}else if(commands[i] == -2) {index = (index - 1 + 4) % 4;}else {for(int j = 0; j < commands[i]; j++) {moveForward(obstacleSet, x, y, directions[index]);maximum = max(maximum, x*x + y*y);}}}return maximum;}private:void moveForward(set<pair<int, int>>& obstacleSet, int& x, int& y, char direction) {switch(direction) {case 'N':if (obstacleSet.find(make_pair(x, y + 1)) == obstacleSet.end()) {y += 1;}break;case 'E':if (obstacleSet.find(make_pair(x + 1, y)) == obstacleSet.end()) {x += 1;}break;case 'S':if (obstacleSet.find(make_pair(x, y - 1)) == obstacleSet.end()) {y -= 1;}break;case 'W':if (obstacleSet.find(make_pair(x - 1, y)) == obstacleSet.end()) {x -= 1;}break;}}
};
Reference
- https://leetcode.com/problems/walking-robot-simulation/description/
Leetcode 874. Walking Robot Simulation相关推荐
- C#LeetCode刷题之#874-模拟行走机器人(Walking Robot Simulation)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4038 访问. 机器人在一个无限大小的网格上行走,从点 (0, 0 ...
- LeetCode.874-走路机器人模拟(Walking Robot Simulation)
这是悦乐书的第335次更新,第360篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第205题(顺位题号是874).网格上的机器人从点(0,0)开始并朝北.机器人可以接收三 ...
- LeetCode 874 题解
874. Walking Robot Simulation 题目大意:二维空间,从原点出发 ,4个方向,走, 路上有障碍,有障碍的地方不能走. 解题思路:模拟 #define mp make_pair ...
- leetcode -874 - 模拟行走机器人 - java版
文章目录 题目 代码 题目 机器人在一个无限大小的网格上行走,从点 (0, 0) 处开始出发,面向北方.该机器人可以接收以下三种类型的命令: -2:向左转 90 度 -1:向右转 90 度 1 < ...
- [leetcode] 874. 行走机器人模拟(周赛)
874. 行走机器人模拟 模拟 描述方向时有个技巧:int[][] dx = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; 分别存储机器人向上.右.下.左走时,坐标应该如何变 ...
- Yet Another Walking Robot CodeForces - 1296C
There is a robot on a coordinate plane. Initially, the robot is located at the point (0,0)(0,0). Its ...
- LeetCode 874. 模拟行走机器人(set)
1. 题目 机器人在一个无限大小的网格上行走,从点 (0, 0) 处开始出发,面向北方.该机器人可以接收以下三种类型的命令: -2:向左转 90 度 -1:向右转 90 度 1 <= x < ...
- Walking Robot
https://codeforces.com/contest/1154/problem/D 题意:有个机器人在0位置处,现在要走到位置n.机器人现在有一个电池容量为a,还有一个蓄电池容量为b(一开始都 ...
- Leetcode 874. 模拟行走机器人 (模拟,一步一步的走)
这里可以将obstacles存在set中,这里有一个坐标hash的问题,Python用tuple可以直接搞,C++需要自己实现hash算法.可以用set解决. class Solution:def r ...
最新文章
- 阿里首次开源深度学习框架X-Deep Learning
- winscp linux中文文件夹名称乱码,解决windows传送到linux下中文文件名及文件内容乱码问题...
- springboot4.1.1的log4j2配置
- SAP Spartacus开启SSR服务器端渲染之后,和默认客户端渲染的差异比较
- 连续对焦 auto对焦_如何在Windows 10上使用对焦辅助(请勿打扰模式)
- jboss eap_HawtIO在JBoss EAP上(第二部分)
- Hibernate学习之一级缓存
- ADO:用代码调用存储过程
- centos 网络自动连接_自动连接最优信号 腾讯云?云兔解决物联网络连接问题
- leaflet+vue:色斑图的实现大体步骤
- PLC控制三相异步电动机正反转系列实训QY-DG800E
- Linux有道词典依赖问题
- libreelec投屏_低配置主机安装Kodi操作系统 - LibreELEC
- word回车后间距太大_Word调整字号间距 Word回车换行间距变大
- 学计算机选择师范类好吗,师范教育类专业和计算机类专业,两者相比,哪个更适合自考生报读...
- Visual studio 2015(VS2015)的下载和安装,以及安装VS2015中的C++
- 视频测试软件+视频测试硬件=视频质量测试解决方法+视频测试的重要性
- 微信小程序 MinUI 组件库系列之 loadmore 页底组件
- aws课程_AWS DeepRacer,Reinforcement Learning 101和一门关于AI管治的小课程
- Office 2016安装