这道题直接按照题意来解,建立坐标系和移动方案,思路是比较简单的。只是需要注意需要使用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. HRBUST 2072 树上求最大异或路径值

    一个很经典的套路 思想是 F [u,v] = F[1,u] ^ F[1,v] 这样就转化成了n个数两两异或 求最大值 可以用字典树来做 每次用当前数去树中寻求最大xor值 然后把这个数字插进去 就相当 ...

  2. 80X86寄存器详解<转载>

    引子 打算写几篇稍近底层或者说是基础的博文,浅要介绍或者说是回顾一些基础知识, 自然,还是得从最基础的开始,那就从汇编语言开刀吧, 从汇编语言开刀的话,我们必须还先要了解一些其他东西, 像  CPU ...

  3. nginx路由重定向

    location / { if ($http_host !~ "m.xxx.cn"){ rewrite ^/web/(.*)/bdu(\d?)\.htm(.*)$ /rewrite ...

  4. MVC3 学习总结一(未发布)

    MVC:  Model,View,Control   设置View中的数据 1. 返回model,View中强类型化 Control: public ActionResult Browse(strin ...

  5. 51 nod 1091 贪心

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1091 1091 线段的重叠 基准时间限制:1 秒 空间限制:131072 ...

  6. JS将数字转换成三位逗号分隔的样式

    function formatNum(num) { if(!/^(\+|-)?(\d+)(\.\d+)?$/.test(num)){alert("wrong!"); return ...

  7. spring发布RMI服务(-)

    spring发布RMI服务 最近交流了一个项目,需要从RMI.WebService.接口文件中采集数据到大数据平台,下面自己测试了通过Spring发布RMI服务. 说明:RMI服务要求服务端和客户端都 ...

  8. NYOJ-626-intersection set(二分查找)

    题目链接 /* Name:NYOJ-626-intersection set Copyright: Author: Date: 2018/4/12 21:30:10 Description: 二分查找 ...

  9. Codeforces Round #263 (Div. 2)C(贪心,联想到huffman算法)

    数学家伯利亚在<怎样解题>里说过的解题步骤第二步就是迅速想到与该题有关的原型题.(积累的重要性!) 对于这道题,可以发现其实和huffman算法的思想很相似(可能出题人就是照着改编的).当 ...

  10. centos type.h 编译错误问题

    # ifndef __int8_t_defined # define __int8_t_defined __intN_t (, __QI__); __intN_t (, __HI__); __intN ...