这道题直接按照题意来解,建立坐标系和移动方案,思路是比较简单的。只是需要注意需要使用set来判断是否遇到障碍,否则会超时。

int robotSim(vector<int>& commands, vector<vector<int>>& obstacles) {

    int N = commands.size();
int M = obstacles.size(); //向上 Wx=0,Wy=1
//向左 Wx=-1,Wy=0
//向下 Wx=0,Wy=-1
//向右 Wx=1,Wy=0 set<pair<int, int>> st;
for (auto &obs : obstacles)
{
st.insert(make_pair(obs[], obs[]));
} int Wx = ;
int Wy = ; int Px = ;
int Py = ;
int maxdistance = ; for (int i = ; i < N; i++)
{
int cmd = commands[i];//当前指令
if (cmd == -)//左转
{
if (Wx == && Wy == )//上-->左
{
Wx = -;
Wy = ;
}
else if (Wx == - && Wy == )//左-->下
{
Wx = ;
Wy = -;
}
else if (Wx == && Wy == -)//下-->右
{
Wx = ;
Wy = ;
}
else if (Wx == && Wy == )//右-->上
{
Wx = ;
Wy = ;
}
}
else if (cmd == -)//右转
{
if (Wx == && Wy == )//上-->右
{
Wx = ;
Wy = ;
}
else if (Wx == - && Wy == )//左-->上
{
Wx = ;
Wy = ;
}
else if (Wx == && Wy == -)//下-->左
{
Wx = -;
Wy = ;
}
else if (Wx == && Wy == )//右-->下
{
Wx = ;
Wy = -;
}
}
else//移动 1<=x<=9
{
//此次移动之前的起点位置为Px和Py
int Tmpx = Px;
int Tmpy = Py;
while (cmd > )
{
//以(Wx,Wy)为步进,移动一次
Tmpx += Wx;
Tmpy += Wy; if (st.find(make_pair(Tmpx, Tmpy)) != st.end())
{
Tmpx -= Wx;
Tmpy -= Wy;
cmd = ;
}
cmd--;
}
int distance = Tmpx*Tmpx + Tmpy*Tmpy;
if (maxdistance < distance)
{
maxdistance = distance;
}
Px = Tmpx;
Py = Tmpy;
}
} return maxdistance;
}

leetcode874的更多相关文章

  1. [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 ...

  2. Leetcode874.Walking Robot Simulation模拟行走的机器人

    机器人在一个无限大小的网格上行走,从点 (0, 0) 处开始出发,面向北方.该机器人可以接收以下三种类型的命令: -2:向左转 90 度 -1:向右转 90 度 1 <= x <= 9:向 ...

  3. LeetCode874 模拟行走机器人(简单模拟—Java之HashSet简单应用)

    题目: 机器人在一个无限大小的网格上行走,从点 (0, 0) 处开始出发,面向北方.该机器人可以接收以下三种类型的命令: -2:向左转 90 度-1:向右转 90 度1 <= x <= 9 ...

随机推荐

  1. UVA 11029 || Lightoj 1282 Leading and Trailing 数学

    Leading and Trailing You are given two integers: n and k, your task is to find the most significant ...

  2. c++ STL库deque和vector的例子

    头文件wuyong.h: #pragma once #include<iostream> #include<vector> #include<deque> #inc ...

  3. C++初始化小问题

    #include<; } 发现,没有对string进行初始化,就已经默认可以使用,并且是空串,一直用java,对c++不熟悉.搜索了下,发现在c++中,只要对对象进行了定义,如果没有初始化,就会 ...

  4. DocString

    文档字符串:写注释专用的, 在函数的第一个逻辑行的字符串是这个函数的 文档字符串 .注意,DocStrings也适用于模块和类,我们会在后面相应的章节学习它们. 文档字符串的惯例是一个多行字符串,它的 ...

  5. 22 Python 模块与包

    一 模块 1 什么是模块? 常见的场景:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 但其实import加载的模块分为四个通用类别: 1 使用python编 ...

  6. CodeForces - 438D: The Child and Sequence(势能线段树)

    At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at ...

  7. CodeForces - 650D:Zip-line (LIS & DP)

    Vasya has decided to build a zip-line on trees of a nearby forest. He wants the line to be as long a ...

  8. [Luogu4177][CEOI2008]order

    luogu sol 这题有点像网络流24题里面的太空飞行计划啊. 最大收益=总收益-最小损失. 先令\(ans=\sum\)任务收益. 源点向每个任务连容量为收益的边. 每个机器向汇点连容量为购买费用 ...

  9. 基于JQ的简单左右轮播图

    // 轮播图 主要实现思想: a.第一层div,设置overflow为hidden. b.里面是一个ul,每个li里面有个img或者为每个li设置背景图片也可以. c.li设置为左浮动,排成一行,还有 ...

  10. vc++2008 采用GSoap访问 WebService

    (转http://www.cppblog.com/yeqing/articles/12762.html) 前一阶段写gSOAP 的文章没保存好,后来想写的,越学越没有写的勇气了,感觉自己很菜,但是现在 ...