[leetcode]Walking Robot Simulation

链接:https://leetcode.com/problems/walking-robot-simulation/description/

Question

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 degrees
  • 1 <= x <= 9: move forward x units

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 <= 10000
0 <= obstacles.length <= 10000
-30000 <= obstacle[i][0] <= 30000
-30000 <= obstacle[i][1] <= 30000
The answer is guaranteed to be less than 2 ^ 31.

Solution

// 啃爹啊!!这道题要返回的是距离原点的最大值,而不是最终值!!
class Solution {
public:typedef pair<int, int> Pair;int direction[4][2] = {{0,1},  // N{1,0},  // E{0,-1}, // S{-1,0}  // W};int robotSim(vector<int>& commands, vector<vector<int>>& obstacles) {int dire = 0;int x = 0, y = 0;map<pair<int, int>, bool> mp;int result = 0;for (int i = 0; i < obstacles.size(); i++) {int x = obstacles[i][0];int y = obstacles[i][1];mp[Pair(x, y)] = true;}for (int i = 0; i < commands.size(); i++) {if (commands[i] == -1) {dire = (dire+1)%4;} else if (commands[i] == -2) {dire = (dire+4-1)%4;} else {for (int j = 0; j < commands[i]; j++) {int new_x = x+direction[dire][0];int new_y = y+direction[dire][1];// 有障碍if (mp.find(Pair(new_x, new_y)) != mp.end()) {break;} else {x = new_x;y = new_y;result = max(result, new_x*new_x+new_y*new_y);}}}}return result;}
};

思路:用一个数组direction来表示方向,注意顺序很重要!体现在后面只需将dire+1或者-1就可以表示右转和左转了。也不知道为啥他们说会超时;-)

Walking Robot Simulation相关推荐

  1. C#LeetCode刷题之#874-模拟行走机器人​​​​​​​(Walking Robot Simulation)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4038 访问. 机器人在一个无限大小的网格上行走,从点 (0, 0 ...

  2. Leetcode 874. Walking Robot Simulation

    文章作者:Tyan 博客:noahsnail.com  |  CSDN  |  简书 1. Description 2. Solution class Solution { public:int ro ...

  3. LeetCode.874-走路机器人模拟(Walking Robot Simulation)

    这是悦乐书的第335次更新,第360篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第205题(顺位题号是874).网格上的机器人从点(0,0)开始并朝北.机器人可以接收三 ...

  4. 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 ...

  5. Walking Robot

    https://codeforces.com/contest/1154/problem/D 题意:有个机器人在0位置处,现在要走到位置n.机器人现在有一个电池容量为a,还有一个蓄电池容量为b(一开始都 ...

  6. C#LeetCode刷题-贪心算法

    贪心算法篇 # 题名 刷题 通过率 难度 44 通配符匹配 17.8% 困难 45 跳跃游戏 II 25.5% 困难 55 跳跃游戏 30.6% 中等 122 买卖股票的最佳时机 II C#LeetC ...

  7. LeetCode 力扣算法题解汇总,All in One

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: https://fuxuemingzhu.cn 关键词:LeetCode,力扣,算法,题解,汇总,解析 把自己刷过的所有题目做一个整理, ...

  8. Leetcode算法题-解法转载

    版权声明:本文为博主原创文章,未经博主允许不得转载.    https://blog.csdn.net/fuxuemingzhu/article/details/85112591 作者: 负雪明烛 i ...

  9. LeetCode 874 题解

    874. Walking Robot Simulation 题目大意:二维空间,从原点出发 ,4个方向,走, 路上有障碍,有障碍的地方不能走. 解题思路:模拟 #define mp make_pair ...

  10. LeetCode All in One 题目讲解汇总(持续更新中...)

    原文地址:https://www.cnblogs.com/grandyang/p/4606334.html 终于将LeetCode的大部分题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开 ...

最新文章

  1. 正则表达式语法规则收集
  2. php正则判断不规范字符串,php学习_第9章_字符串处理与正则表达式
  3. 怎么在代码中打开、关闭屏幕旋转
  4. c语言计算输入的字母数字个数字,请问这个用c怎么做:输入一串字符,分别统计其中数字和字母的个数...
  5. 这两款无“节操”的浏览器,在315被曝光后,终于被下架了
  6. jsp java注释_jsp注释方式
  7. 大数据项目实践:基于hadoop+spark+mongodb+mysql开发医院临床知识库系统
  8. VBA之六--EXCEL VBA两则
  9. Axis2;wsdl生成客户端和serverJava代码
  10. linux中开启514端口,linux中开启指定端口
  11. python sendto(右键发送文件到执行的bat)功能的实现
  12. 提权获取进程路径并获取进程列表
  13. 遗传算法解决TSP问题MATLAB实现(详细)
  14. Java基础教程汇总
  15. sqlmap安装和使用
  16. 计算机主板上常用的接口,电脑主板上接口怎么接 主板所有接口插线功能作用识别图解...
  17. 编程路上,对于迷失者的一些小小建议
  18. 玻璃材料封接工艺技术介绍-电连接器封装形式
  19. k1075停运吗_要外出的街坊注意!受台风影响,最近三天这些火车班次停运
  20. Vue实现一键复制文本内容

热门文章

  1. CC2530 ADC学习笔记
  2. mysql变量赋值加冒号,mysql 冒号
  3. 【Learning Notes】Sequence Transducer
  4. XP系统开机显示“NTDETECT失败”
  5. 洛谷 P4200 千山鸟飞绝平衡树
  6. (原創) 如何設計一個數位相框? (SOC) (Quartus II) (SOPC Builder) (Nios II) (TRDB-LTM) (DE2-70)...
  7. 交通分析小区TAZ生成——以武汉市为例
  8. 127.0.0.1 192.168 localhost 之间的区别
  9. eclipse使用技巧——备注格式的定义与模板
  10. Sql中的left函数、right函数