LeetCode-困于环中的机器人

题目

在无限的平面上,机器人最初位于 (0, 0) 处,面朝北方。注意:

北方向 是y轴的正方向。
南方向 是y轴的负方向。
东方向 是x轴的正方向。
西方向 是x轴的负方向。
机器人可以接受下列三条指令之一:

"G":直走 1 个单位
"L":左转 90 度
"R":右转 90 度
机器人按顺序执行指令 instructions,并一直重复它们。

只有在平面中存在环使得机器人永远无法离开时,返回 true。否则,返回 false。

示例 1:

输入:instructions = "GGLLGG"
输出:true
解释:机器人最初在(0,0)处,面向北方。
“G”:移动一步。位置:(0,1)方向:北。
“G”:移动一步。位置:(0,2).方向:北。
“L”:逆时针旋转90度。位置:(0,2).方向:西。
“L”:逆时针旋转90度。位置:(0,2)方向:南。
“G”:移动一步。位置:(0,1)方向:南。
“G”:移动一步。位置:(0,0)方向:南。
重复指令,机器人进入循环:(0,0)——>(0,1)——>(0,2)——>(0,1)——>(0,0)。
在此基础上,我们返回true。
示例 2:

输入:instructions = "GG"
输出:false
解释:机器人最初在(0,0)处,面向北方。
“G”:移动一步。位置:(0,1)方向:北。
“G”:移动一步。位置:(0,2).方向:北。
重复这些指示,继续朝北前进,不会进入循环。
在此基础上,返回false。
示例 3:

输入:instructions = "GL"
输出:true
解释:机器人最初在(0,0)处,面向北方。
“G”:移动一步。位置:(0,1)方向:北。
“L”:逆时针旋转90度。位置:(0,1).方向:西。
“G”:移动一步。位置:(- 1,1)方向:西。
“L”:逆时针旋转90度。位置:(- 1,1)方向:南。
“G”:移动一步。位置:(- 1,0)方向:南。
“L”:逆时针旋转90度。位置:(- 1,0)方向:东方。
“G”:移动一步。位置:(0,0)方向:东方。
“L”:逆时针旋转90度。位置:(0,0)方向:北。
重复指令,机器人进入循环:(0,0)——>(0,1)——>(- 1,1)——>(- 1,0)——>(0,0)。
在此基础上,我们返回true。

我的解答

class Solution {
    public boolean isRobotBounded(String instructions) {
        String[] ins=instructions.split("");
        String direction="north";
        int x=0,y=0;
        for(String insOne:ins){
            if(insOne.equals("G")){
                x=locationX(direction,x);
                y=locationY(direction,y);
            }
            if(insOne.equals("L")){
                direction=turnLeft(direction);
            }
            if(insOne.equals("R")){
                direction=turnRight(direction);
            }
        }
        return !direction.equals("north")||(x==0&&y==0);
    }
    private String turnLeft(String direction){
        switch(direction){
            case "north":
            direction="west";
            break;
            case "west":
            direction="south";
            break;
            case "south":
            direction="east";
            break;
            case "east":
            direction="north";
            break;
        }
        return direction;
    }
    private String turnRight(String direction){
        switch(direction){
            case "north":
            direction="east";
            break;
            case "east":
            direction="south";
            break;
            case "south":
            direction="west";
            break;
            case "west":
            direction="north";
            break;
        }
        return direction;
    }
    private int locationX(String direction,int x){
        switch(direction){
            case "east":
            x+=1;
            break;
            case "west":
            x-=1;
            break;
        }
        return x;
    }
    private int locationY(String direction,int y){
        switch(direction){
            case "north":
            y+=1;
            break;
            case "south":
            y-=1;
            break;           
        }
        return y;
    }
}

官方题解

class Solution {
    public boolean isRobotBounded(String instructions) {
        int[][] direc = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
        int direcIndex = 0;
        int x = 0, y = 0;
        int n = instructions.length();
        for (int idx = 0; idx < n; idx++) {
            char instruction = instructions.charAt(idx);
            if (instruction == 'G') {
                x += direc[direcIndex][0];
                y += direc[direcIndex][1];
            } else if (instruction == 'L') {
                direcIndex += 3;
                direcIndex %= 4;
            } else {
                direcIndex++;
                direcIndex %= 4;
            }
        }
        return direcIndex != 0 || (x == 0 && y == 0);
    }
}

作者:力扣官方题解
链接:https://leetcode.cn/problems/robot-bounded-in-circle/solutions/2217873/kun-yu-huan-zhong-de-ji-qi-ren-by-leetco-kjya/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

思考

我是看了官方题解之后,才反应过来需要循环指令,最后的判断,是根据官方题解做的,下次再来一遍,我还是不清楚原理。
问了AI之后才知道,非北方向导致机器人在有限步内闭合的本质是旋转操作的周期性和位移向量的对称叠加​:

朝南(180°)需 ​2次​ 形成反向闭合路径;
朝东/西(90°/270°)需 ​4次​ 形成正方形闭合路径。

这一原理在路径规划算法(如SLAM、RRT*)中用于检测循环和优化轨迹,是机器人运动控制的数学基础之一。

posted @ 2025-06-11 15:31  尼兰  阅读(13)  评论(0)    收藏  举报