874. Walking Robot Simulation
A robot on an infinite grid starts at point (0, 0) and faces north. The robot can receive one of three possible types of commands:
-2: turn left 90 degrees-1: turn right 90 degrees1 <= x <= 9: move forwardxunits
Some of the grid squares are obstacles.
The i-th obstacle is at grid point (obstacles[i][0], obstacles[i][1])
If the robot would try to move onto them, the robot stays on the previous grid square instead (but still continues following the rest of the route.)
Return the square of the maximum Euclidean distance that the robot will be from the origin.
Example 1:
Input: commands = [4,-1,3], obstacles = []
Output: 25
Explanation: robot will go to (3, 4)
Example 2:
Input: commands = [4,-1,4,-2,4], obstacles = [[2,4]]
Output: 65
Explanation: robot will be stuck at (1, 4) before turning left and going to (1, 8)
Note:
0 <= commands.length <= 100000 <= obstacles.length <= 10000-30000 <= obstacle[i][0] <= 30000-30000 <= obstacle[i][1] <= 30000- The answer is guaranteed to be less than
2 ^ 31.
Approach #1: C++.
class Solution {
public:
int robotSim(vector<int>& commands, vector<vector<int>>& obstacles) {
vector<pair<int, int>> dirs = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
int x = 0, y = 0, di = 0;
int ans = 0;
set<pair<int, int>> obstacleSet;
for (auto obstacle : obstacles)
obstacleSet.insert(make_pair(obstacle[0], obstacle[1]));
for (int command : commands) {
if (command == -2) {
di = (di + 3) % 4;
} else if (command == -1) {
di = (di + 1) % 4;
} else {
for (int i = 0; i < command; ++i) {
int nx = x + dirs[di].first;
int ny = y + dirs[di].second;
if (obstacleSet.find(make_pair(nx, ny)) == obstacleSet.end()) {
x = nx;
y = ny;
ans = max(ans, x*x + y*y);
}
}
}
}
return ans;
}
};
Analysis:
If we know the relation of the directions and turn, it will become easier.
874. Walking Robot Simulation的更多相关文章
- leetcode 874. Walking Robot Simulation
874. Walking Robot Simulation https://www.cnblogs.com/grandyang/p/10800993.html 每走一步(不是没走commands里的一 ...
- 【Leetcode_easy】874. Walking Robot Simulation
problem 874. Walking Robot Simulation solution1: 思路:1)如何表示移动的方向以及移动的位置坐标; 2)障碍物坐标如何检查;3)求解的是最大距离; cl ...
- [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】874. Walking Robot Simulation 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 模拟 日期 题目地址:https://leetcod ...
- [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 ...
- 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 ...
- Leetcode874.Walking Robot Simulation模拟行走的机器人
机器人在一个无限大小的网格上行走,从点 (0, 0) 处开始出发,面向北方.该机器人可以接收以下三种类型的命令: -2:向左转 90 度 -1:向右转 90 度 1 <= x <= 9:向 ...
- Codeforces Round #605 (Div. 3) B. Snow Walking Robot(构造)
链接: https://codeforces.com/contest/1272/problem/B 题意: Recently you have bought a snow walking robot ...
随机推荐
- 新手C#ListView使用记录2018.08.03
在使用C#的ListView时,感觉有一些部分需要自己注意. ListView1.Clear()这条指令在使用时,用于Click后在ListView中显示数据,应该放在输入数据的前面. 在ListVi ...
- word2012写论文之参考文献和图片
每次写论文都感到word的“博大精深”啊,一个版本一个样,一些原来版本还有的功能,后来的版本就没有了,一些原来版本叫这个名字的功能,下一个版本就换了个名字,你也太考验你用户的智商和耐心了吧!即使同一份 ...
- Nginx源码完全注释(9)nginx.c: ngx_get_options
本文分析 ngxin.c 中的 ngx_get_options 函数,其影响: nginx.c 中的: static ngx_uint_t ngx_show_help; static ngx_uint ...
- unity在安卓中横屏闪退
竖屏没问题,横屏闪退 配置文件的AndoridManifest.xml横竖屏设置要和UNITY设置的一致,否则就会强退 UNITY横竖屏设置
- 使用CocoaPods卡在了"pod setup"界面的解决办法
http://blog.csdn.net/samoy/article/details/51956799 有时候,我们在执行pod install或pod search命令时,会在终端偶现卡在’ ...
- Paradox
克己博伦 当一个无法阻挡的力量,碰到了一个无法移动的物体?如果这个力量移动了物体,那么这个物体就不是无法移动的.如果这个力量没有移动物体,那么这个无法阻挡的力量就被挡了下来. 上帝能造出一个重到他自己 ...
- CentOS JAVA安装及查看路径方法
一.安装: 方法一:手动解压JDK的压缩包,然后设置环境变量 1.在/usr/目录下创建java目录 [root@localhost ~]# mkdir/usr/java[root@localhost ...
- jenkins持续集成之Global Tool Configuration的配置
Global Tool Configuration的配置过程: 1.点击系统管理2.点击 Global Tool Configuration3.必须配置: jdk git ant maven 1.点击 ...
- linux常用Java程序员使用命令(二)
出品人:北极的大企鹅 1. pwd 显示当前路径 2. cd 切换目录 . .. ~ 例如: cd /root 3. ls 显示文件(夹) -l 显示详细信息 -a 显示全部,包括隐藏文件(夹)(这个 ...
- 15 输入三个整数x,y,z,请把这三个数由小到大输出。
题目:输入三个整数x,y,z,请把这三个数由小到大输出. public class _015ThreeNumberSort { public static void main(String[] arg ...