874. 行走机器人模拟

模拟

描述方向时有个技巧:int[][] dx = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};

分别存储机器人向上、右、下、左走时,坐标应该如何变换

class Solution {
public int robotSim(int[] commands, int[][] obstacles) {
int max = 0;
int[][] dx = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
int k = 0;
Map<String, Boolean> map = new HashMap<>();
for (int i = 0; i < obstacles.length; i++) {
map.put(obstacles[i][0] + "," + obstacles[i][1], true);
}
int p = 0, q = 0;
for (int command : commands) {
if (command == -1) {
k = (k + 1) % 4;
} else if (command == -2) {
k = (k + 4 - 1) % 4;
} else {
int cur[] = dx[k];
for (int i = 0; i < command; i++) {
if (map.containsKey((p + cur[0]) + "," + (q + cur[1]))) {
break;
}
p += cur[0];
q += cur[1];
}
max = Math.max(max, p * p + q * q);
}
}
return max;
}
}

[leetcode] 874. 行走机器人模拟(周赛)的更多相关文章

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

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

  2. OpenAI 开源机器人模拟 Python 库,并行模拟处理速度提升400%

    10000da.cnvboyule.cnjiaeidaypt.cn  在过去一年的研究中,OpenAI团队开源一个使用 MuJoCoengine开发的用于机器人模拟的高性能Python库.雷锋网了解到 ...

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

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

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

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

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

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

  7. leetcode 874. Walking Robot Simulation

    874. Walking Robot Simulation https://www.cnblogs.com/grandyang/p/10800993.html 每走一步(不是没走commands里的一 ...

  8. LeetCode 6 ZigZag Conversion 模拟 难度:0

    https://leetcode.com/problems/zigzag-conversion/ The string "PAYPALISHIRING" is written in ...

  9. [LeetCode] Squirrel Simulation 松鼠模拟

    There's a tree, a squirrel, and several nuts. Positions are represented by the cells in a 2D grid. Y ...

随机推荐

  1. [源码分析] 并行分布式任务队列 Celery 之 Timer & Heartbeat

    [源码分析] 并行分布式任务队列 Celery 之 Timer & Heartbeat 目录 [源码分析] 并行分布式任务队列 Celery 之 Timer & Heartbeat 0 ...

  2. 基于Frida框架打造Art模式下的脱壳工具(OpenMemory)的原理分析

    本文博客地址:https://blog.csdn.net/QQ1084283172/article/details/80956614 作者dstmath在看雪论坛公布一个Android的art模式下基 ...

  3. Python多线程_thread和Threading

    目录 多线程 _thread模块 使用 _thread模块创建线程 threading 使用 threading模块创建线程 线程同步 在讲多线程之前,我们先看一个单线程的例子: import _th ...

  4. Tomcat PUT方法任意文件上传(CVE-2017-12615)

    目录 漏洞复现: 漏洞利用工具: 漏洞环境:当 Tomcat运行在Windows操作系统,且启用了HTTP PUT请求方法(例如,将 readonly 初始化参数由默认值设置为 false),攻击者将 ...

  5. JVM默认内存大小

    堆(Heap)和非堆(Non-heap)内存 按照官方的说法:"Java虚拟机具有一个堆,堆是运行时数据区域,所有类实例和数组的内存均从此处分配.堆是在Java虚拟机启动时创建的." ...

  6. android手势识别

    一.简单手势识别: 简单手势识别分为以下几个步骤: 1.手指触碰屏幕时,触发MotionEvent事件: 2.该事件被OnTouchListener监听,可在它的onTouch()方法中获得该Moti ...

  7. linux安装MySQL报 error while loading shared libraries: libtinfo.so.5 解决办法

    MySQL服务启动报错 error while loading shared libraries: libtinfo.so.5: cannot open shared object file: No ...

  8. Gridea博客无法载入CSS样式的解决办法

    今日在使用Gridea客户端更新博客的过程中,推送到远端仓库后内容显示正常,但是无法载入主题样式,就是没有载入CSS样式,折腾了一下午在搞懂问题出在哪里了,下面说一下自己的解决思路. 问题描述 首先, ...

  9. JavaWeb——反射、注解

    单元测试.反射.注解 1. Junit单元测试 2. 反射 3. 注解 Junit单元测试: * 测试分类: 1. 黑盒测试:不需要写代码,给输入值,看程序是否能够输出期望的值. 2. 白盒测试:需要 ...

  10. Django(13)django时区问题

    前言 我们都知道时区,标准时区是UTC时区,django默认使用的就是UTC时区,所以我们存储在数据库中的时间是UTC的时间,但是当我们做的网站只面向国内用户,或者只是提供内部平台使用,我们希望存储在 ...