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 ...
随机推荐
- 转 Android:文件下载和写入SD卡学习小结
转自 http://blog.csdn.net/zzp_403184692/article/details/8160739 一.文件下载 Android开发中,有时需要从网上下载一些资源以供用户使 ...
- zoj1001-A + B Problem
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1 A + B Problem Time Limit: 2 Seconds ...
- 297. Serialize and Deserialize Binary Tree二叉树的序列化和反序列化(就用Q)
[抄题]: Serialization is the process of converting a data structure or object into a sequence of bits ...
- spring4-2-bean配置-4-bean之间的关系
- ICO流程,casestudy
https://medium.com/crypto-oracle/ico-analysis-framework-nex-case-study-bf65586b4b32
- c语言条件编译#ifdef与#if defined
c语言条件编译#ifdef与#if defined c语言条件编译#ifdef与#if defined 摘自:https://www.cnblogs.com/zhangshenghui/p/566 ...
- 掌握zigbee网络里的相关的重要概论
1.zigbee无线通信,需要高频的载波来提供发射效率,zigbee模块之间要可以正常的收发,必须把接收频率设置和发射模块的载波频率一致. 2.zigbee有27个载波可以进行通信,载波叫做信道(无线 ...
- HUST软测1504班第2周作业成绩:WordCount
说明 本次公布的成绩为第2周个人作业WordCount的结果: 第2周个人作业:WordCount 如果同学对作业结果存在异议,可以: 在毕博平台讨论区的第2周作业第在线答疑区发帖申诉. 或直接在博客 ...
- vSphere5.5体系结构
1.vSphere5.5体系结构 vSphere5.5作为云集算操作系统,可以虚拟化服务器.存储.网络在内的整个IT基础架构,将这些架构转化为易于管理的虚拟化!vSphere5.5从逻辑上,可以划分为 ...
- Java书籍经典Top10
1)Java Language Specification, Third Edition (by James Gosling) 本书由Java技术的发明者编写,是Java TM编程语言的权威性技术指南 ...