建设网站番禺,1688黄页网生产企业,黄页88成立时间,线上平面设计课程文章目录1. 题目2. 解题1. 题目
在无限的平面上#xff0c;机器人最初位于 (0, 0) 处#xff0c;面朝北方。机器人可以接受下列三条指令之一#xff1a;
“G”#xff1a;直走 1 个单位“L”#xff1a;左转 90 度“R”#xff1a;右转 90 度 机器人按顺序执行指令 ins…
文章目录1. 题目2. 解题1. 题目
在无限的平面上机器人最初位于 (0, 0) 处面朝北方。机器人可以接受下列三条指令之一
“G”直走 1 个单位“L”左转 90 度“R”右转 90 度 机器人按顺序执行指令 instructions并一直重复它们。
只有在平面中存在环使得机器人永远无法离开时返回 true。否则返回 false。
示例 1
输入GGLLGG
输出true
解释
机器人从 (0,0) 移动到 (0,2)转 180 度然后回到 (0,0)。
重复这些指令机器人将保持在以原点为中心2 为半径的环中进行移动。示例 2
输入GG
输出false
解释
机器人无限向北移动。示例 3
输入GL
输出true
解释
机器人按 (0, 0) - (0, 1) - (-1, 1) - (-1, 0) - (0, 0) - ... 进行移动。提示
1 instructions.length 100
instructions[i] 在 {G, L, R} 中来源力扣LeetCode 链接https://leetcode-cn.com/problems/robot-bounded-in-circle 著作权归领扣网络所有。商业转载请联系官方授权非商业转载请注明出处。 2. 解题
4次循环指令后能回到原点就永远走不出去
class Solution {
public:bool isRobotBounded(string instructions) {vectorvectorint dir {{0,1},{1,0},{0,-1},{-1,0}};int x 0, y 0, d 0, n 4;while(n--)for(char ch : instructions) {if(ch G){x dir[d][0];y dir[d][1];}else if(ch L)d (d-14)%4;elsed (d1)%4;}return x0 y0;}
};4 ms 6.2 MB
或者是一次指令后在原点或者方向变了就一定能再走回来。
class Solution {
public:bool isRobotBounded(string instructions) {vectorvectorint dir {{0,1},{1,0},{0,-1},{-1,0}};int x 0, y 0, d 0, n 4;for(char ch : instructions) {if(ch G){x dir[d][0];y dir[d][1];}else if(ch L)d (d-14)%4;elsed (d1)%4;}return (x0 y0) || d!0;}
};4 ms 6.5 MB 我的CSDN博客地址 https://michael.blog.csdn.net/
长按或扫码关注我的公众号Michael阿明一起加油、一起学习进步