力扣 -- 1041. 困于环中的机器人

一 、题目描述   1041. 困于环中的机器人

在无限的平面上,机器人最初位于 (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. 1 <= instructions.length <= 100
  2. instructions[i] 在 {'G', 'L', 'R'} 中

二、实现思路以及代码

class Solution {
    public boolean isRobotBounded(String instructions) {
        int len = instructions.length();
        // 一次指令之后,只有(x,y)不是原点,并且方向和原来的方向一致,最后才回不去
        char[] instruction = instructions.toCharArray();
        boolean res = true;
        int i = 0;
        int x = 0, y = 0; // 机器人的位置(x,y)
        int path = 1;// path代表方向 1指向北,2指向南,3指向西,4指向东`
        while (i < len) {`
            switch (path) { //先选择方向,在不同方向下选择不同的操作
                case 1: //面朝北 (判断好方向后,根据指令操作下一步)
                    if (instruction[i] == 'L') { // 指令是左转90度
                        path = 3;
                        break;
                    } else if (instruction[i] == 'R') { // 指令是右转90度
                        path = 4;
                        break;
                    } else { // 指令是向前移动1
                        y = y + 1;
                        break;
                    }
                case 2: //面朝南
                    if (instruction[i] == 'L') { // 指令是左转90度
                        path = 4;
                        break;
                    } else if (instruction[i] == 'R') { // 指令是右转90度
                        path = 3;
                        break;
                    } else {
                        y = y - 1;
                        break;
                    }
                case 3: //面朝西
                    if (instruction[i] == 'L') { // 指令是左转90度
                        path = 2;
                        break;
                    } else if (instruction[i] == 'R') { // 指令是右转90度
                        path = 1;
                        break;
                    } else {
                        x = x - 1;
                        break;
                    }
                case 4: //面朝东
                    if (instruction[i] == 'L') { // 指令是左转90度
                        path = 1;
                        break;
                    } else if (instruction[i] == 'R') { // 指令是右转90度
                        path = 2;
                        break;
                    } else {
                        x = x + 1;
                        break;
                    }
                default:
            }
            i++;
            if(i == len){ // 遍历完一遍字符串,判断机器人当前的位置以及朝向
                if(x ==0 && y == 0){//若直接回到了原点,则返回true
                    return res;
                }else{
                    if(path != 1){// 此时不在原点且朝向不是和出发朝向相同,结果有可能会回到原点,从头开始开始遍历指令
                        i = 0;
                    }else{
                        res = false;// 此时不在原点且朝向和出发朝向相同,不可能回到原点,返回false
                        break;
                    }
                }
            }
        }
        return res;
    }
}

你可能感兴趣的:(leetcode)