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. 记一次 医院.NET公众号系统 线程CPU双高分析

    一:背景 1. 讲故事 上周四有位朋友加wx咨询他的程序出现 CPU + 线程 双高的情况,希望我能帮忙排查下,如下图: 从截图看只是线程爆高,没看到 cpu 爆高哈,有意思的是这位朋友说他: 一直在 ...

  2. Cobalt-Strike Office宏利用与免杀

    1.打开Cobalt-Strike生产Office宏病毒. 首先需要设置监听器.因为钓鱼的目标比较单纯,在这里就不采用域前置技术. 然后使用攻击模块,生产Office宏病毒. 设置好监听器. 生成宏病 ...

  3. K8S(17)二进制的1.15版本部署hpa自动伸缩

    K8S(17)二进制部署的K8S(1.15)部署hpa功能 目录 K8S(17)二进制部署的K8S(1.15)部署hpa功能 零.参考文件: 一.生成metrics-proxy证书 二.修改apise ...

  4. LA4851餐厅(求好的坐标的个数)

    题意:       有一个m*m的格子,左下角(0,0)右上角(m-1,m-1),网格里面有两个y坐标相同的宾馆(A,B),每个宾馆里面有一个餐厅,一共用n个餐厅,第1,2个都在宾馆里,3,4...在 ...

  5. Dalvik模式下基于Android运行时类加载的函数dexFindClass脱壳

    本文博客地址:http://blog.csdn.net/qq1084283172/article/details/78003184 前段时间在看雪论坛发现了<发现一个安卓万能脱壳方法>这篇 ...

  6. DVWA之SQL Injection

    SQL Injection SQL Injection,即SQL注入,是指攻击者通过注入恶意的SQL命令,破坏SQL查询语句的结构,从而达到执行恶意SQL语句的目的.SQL注入漏洞的危害是巨大的,常常 ...

  7. POJ1486模拟或者匈牙利变种

    题意:       有n个矩形,每个矩形上的某个位置上都有一个点,但是由于矩形是透明的,当一些矩形重叠在一起的时候就很可能分不清哪个点是那个矩形的,给你n个矩形的坐标,还有n个点的坐标,然后让你找出所 ...

  8. Portswigger web security academy:XML external entity (XXE) injection

    Portswigger web security academy:XML external entity (XXE) injection 目录 Portswigger web security aca ...

  9. Windows子系统Ubuntu安装宝塔面板

    首先你需要安装Ubuntu子系统,安装方法点击这里,安装完成后再进行下面的步骤. 安装宝塔面板 输入命令 wget -O install.sh http://download.bt.cn/instal ...

  10. git的一些常用命令总结

    1.拉取代码Git clone  "链接名称" 2.新建分支 git checkout -b "分支名称" 3.提交代码步骤 (1)Git status查看项目 ...