Leetcode874.Walking Robot Simulation模拟行走的机器人
机器人在一个无限大小的网格上行走,从点 (0, 0) 处开始出发,面向北方。该机器人可以接收以下三种类型的命令:
- -2:向左转 90 度
- -1:向右转 90 度
- 1 <= x <= 9:向前移动 x 个单位长度
在网格上有一些格子被视为障碍物。
第 i 个障碍物位于网格点 (obstacles[i][0], obstacles[i][1])
如果机器人试图走到障碍物上方,那么它将停留在障碍物的前一个网格方块上,但仍然可以继续该路线的其余部分。
返回从原点到机器人的最大欧式距离的平方。
示例 1:
输入: commands = [4,-1,3], obstacles = [] 输出: 25 解释: 机器人将会到达 (3, 4)
示例 2:
输入: commands = [4,-1,4,-2,4], obstacles = [[2,4]] 输出: 65 解释: 机器人在左转走到 (1, 8) 之前将被困在 (1, 4) 处
提示:
- 0 <= commands.length <= 10000
- 0 <= obstacles.length <= 10000
- -30000 <= obstacle[i][0] <= 30000
- -30000 <= obstacle[i][1] <= 30000
- 答案保证小于 2 ^ 31
题目感觉没有说清楚,应该说是过程中最大的欧式平方
class Solution {
public:
int robotSim(vector<int>& commands, vector<vector<int> >& obstacles) {
map<pair<int, int>, bool> check;
for(int i = 0; i < obstacles.size(); i++)
{
check[make_pair(obstacles[i][0], obstacles[i][1])] = true;
}
//北东南西
int dir = 0;
int res = 0;
int currentx = 0;
int currenty = 0;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
for(int i = 0; i < commands.size(); i++)
{
if(commands[i] == -1)
{
dir = (dir + 1) % 4;
}
else if(commands[i] == -2)
{
dir = (dir + 4 - 1) % 4;
}
else
{
int step = commands[i];
for(int i = 0; i < step; i++)
{
int newx = currentx + dx[dir];
int newy = currenty + dy[dir];
if(check[make_pair(newx, newy)] == false)
{
currentx = newx;
currenty = newy;
res = max(res, currentx * currentx + currenty * currenty);
}
}
}
}
return res;
}
};
Leetcode874.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 ...
- [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 ...
- 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. 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 ...
- 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 ...
- [ACM] hdu 1035 Robot Motion (模拟或DFS)
Robot Motion Problem Description A robot has been programmed to follow the instructions in its path. ...
随机推荐
- 使用 jQuery 设置 disabled 属性与移除 disabled 属性
表单中readOnly和disabled的区别:Readonly只针对input(text/ password)和textarea有效,而disabled对于所有的表单元素都有效,包括select,r ...
- HBase Master-status
- 微信小程序之组件的集合(六)
这个将是最后一篇关于小程序的记录了,课程接近尾声,最后一个是关于用户的page页面,看看这个页面中有哪些值得学习的地方! 一.page中my开发 这个主要是展示用户喜欢的杂志,以及用户的信息,需要创建 ...
- Quick BI 的模型设计与生成SQL原理剖析
一.摘要 随着物联网的告诉发展,数据量呈现井喷式的增长,如何来分析和使用这些数据,使数据产生商业价值,已经变得越来越重要.值得高兴的是,当前越来越多的人已经意识到了用数据分析决定商业策略的重要性,也都 ...
- javascript的Touch事件
js的touch事件,一般用于移动端的触屏滑动 $(function(){document.addEventListener("touchmove", _touch, false) ...
- qml获取实际渲染的字体
当设置qml的Text元素的字体时,如果系统中不存在设置的字体,qml会根据匹配算法自动选取系统中存在的一种字体.比如:设置font.family: "微软雅黑",但系统中根本没有 ...
- js 打开app应用,如果没有安装就去下载
废话不多说,直接上代码 var APPCommon = { iphoneSchema: 'XingboTV://', iphoneDownUrl: 'https://itunes.apple.com/ ...
- iOS Bezier曲线
https://www.jianshu.com/p/2316f0d9db65 1. Bezier曲线 相关软件:PaintCode:可以直接画图,软件根据图像生产Bezier曲线 相关概念:UIBez ...
- iOS学习笔记-084.粒子效果——路径移动
https://blog.csdn.net/qiwenmingshiwo/article/details/75806637 粒子效果路径移动一说明1 效果2 步骤分析二代码1 VCViewh2 VCV ...
- pytorch dataloader 取batch_size时候 出现bug
1.RuntimeError: invalid argument 0: Sizes of tensors must match except in dimension 0. Got 342 and 2 ...