每日一道leetcode(2026.04.07):模拟行走机器人 II

每日一道leetcode(2026.04.07):模拟行走机器人 II

  • [1. 题目](#1. 题目)
  • [2. 分析](#2. 分析)
  • [3. 代码实现](#3. 代码实现)

1. 题目

给你一个在 XY 平面上的 width x height 的网格图,左下角 的格子为 (0, 0) ,右上角 的格子为 (width -

1, height - 1) 。网格图中相邻格子为四个基本方向之一("North","East","South" 和

"West")。一个机器人 初始 在格子 (0, 0) ,方向为 "East" 。

机器人可以根据指令移动指定的 步数 。每一步,它可以执行以下操作。

沿着当前方向尝试 往前一步 。 如果机器人下一步将到达的格子 超出了边界 ,机器人会 逆时针 转 90 度,然后再尝试往前一步。

如果机器人完成了指令要求的移动步数,它将停止移动并等待下一个指令。

请你实现 Robot 类:

Robot(int width, int height) 初始化一个 width x height 的网格图,机器人初始在 (0, 0)

,方向朝 "East" 。 void step(int num) 给机器人下达前进 num 步的指令。 int[] getPos()

返回机器人当前所处的格子位置,用一个长度为 2 的数组 [x, y] 表示。 String getDir() 返回当前机器人的朝向,为

"North" ,"East" ,"South" 或者 "West" 。

示例 1:

输入:

"Robot", "step", "step", "getPos", "getDir", "step", "step", "step", "getPos", "getDir"\] \[\[6, 3\], \[2\], \[2\], \[\], \[\], \[2\], \[1\], \[4\], \[\], \[\]

输出: [null, null, null, [4, 0], "East", null, null, null, [1, 2],

"West"]

解释:

Robot robot = new Robot(6, 3); // 初始化网格图,机器人在 (0, 0) ,朝东。

robot.step(2); // 机器人朝东移动 2 步,到达 (2, 0) ,并朝东。 robot.step(2); //

机器人朝东移动 2 步,到达 (4, 0) ,并朝东。 robot.getPos(); // 返回 [4, 0]

robot.getDir(); // 返回 "East" robot.step(2); // 朝东移动 1 步到达 (5, 0)

,并朝东。

// 下一步继续往东移动将出界,所以逆时针转变方向朝北。

// 然后,往北移动 1 步到达 (5, 1) ,并朝北。 robot.step(1); // 朝北移动 1 步到达 (5, 2) ,并朝 北 (不是朝西)。 robot.step(4); //

下一步继续往北移动将出界,所以逆时针转变方向朝西。

// 然后,移动 4 步到 (1, 2) ,并朝西。 robot.getPos(); // 返回 [1, 2] robot.getDir(); // 返回 "West"

提示:

2 <= width, height <= 100

1 <= num <= 105

step ,getPos 和 getDir 总共 调用次数不超过 104 次。

2. 分析

今天这道题是中等难度,算是比较简单,主要是有两个需要注意的点。当移动的步数=n倍周长时,终点坐标不变,但是方向可能会变。

首先是周长的计算,比如2x3的平面,走一圈的步数是((width-1)+(height-1))*2,可以根据上图模拟走一遍,check一下。

另外一点就是终点的方向,方向跟走的圈数没有关系,而是跟起点的方向有关。如果是起点没有在拐角点,方向肯定是不变的;其次,如果是起点在拐角点,并且下一步需要先旋转后移动,这种情况也不是不变的。所以,唯一需要变方向的情况就是在拐角点,并且在当前线路的起始点位置和方向时,就需要逆时针旋转90°。这一点其实想通了也好理解,最后一次走圈完成后,位置是到了,但是不会主动旋转方向,需要再往前行进一个单位时才会触发旋转。

3. 代码实现

java 复制代码
class Robot {
    public int width;
    public int height;
    public int curX;
    public int curY;
    public int curDir;
    public int cycle;
    public final String[] directions = new String[] { "East", "North", "West", "South" };

    public Robot(int width, int height) {
        System.out.println(width + "," + height);
        this.width = width;
        this.height = height;
        curX = 0;
        curY = 0;
        cycle = (width - 1 + height - 1) * 2;
    }

    public void step(int num) {
        num = num % cycle;
        if (num == 0) {
            // 判断是否与原方向保持一致
            // 1. 非直角点;2. 直接点需要旋转方向时才能前进 两种情况与原方向保持一致
            if (curDir == 1 && curY == 0) {
                curDir = 0;
            } else if (curDir == 2 && curX == width - 1) {
                curDir = 1;
            } else if (curDir == 3 && curY == height - 1) {
                curDir = 2;
            } else if (curDir == 0 && curX == 0) {
                curDir = 3;
            }
            return;
        }
        int step;
        while (num > 0) {
            if (curDir == 0) {
                if (curX < width - 1) {
                    step = Math.min(num, width - 1 - curX);
                    curX += step;
                    num -= step;
                } else {
                    curDir = 1;
                }
            } else if (curDir == 1) {
                if (curY < height - 1) {
                    step = Math.min(num, height - 1 - curY);
                    curY += step;
                    num -= step;
                } else {
                    curDir = 2;
                }
            } else if (curDir == 2) {
                if (curX > 0) {
                    step = Math.min(num, curX);
                    curX -= step;
                    num -= step;
                } else {
                    curDir = 3;
                }
            } else if (curDir == 3) {
                if (curY > 0) {
                    step = Math.min(num, curY);
                    curY -= step;
                    num -= step;
                } else {
                    curDir = 0;
                }
            }
        }
    }

    public int[] getPos() {
        return new int[] { curX, curY };
    }

    public String getDir() {
        return directions[curDir];
    }
}

/**
 * Your Robot object will be instantiated and called as such:
 * Robot obj = new Robot(width, height);
 * obj.step(num);
 * int[] param_2 = obj.getPos();
 * String param_3 = obj.getDir();
 */
相关推荐
xiaoduo AI2 小时前
客服机器人用知识图谱推理吗?Agent功能介绍+关联问答,跨品类问题能推导?
人工智能·机器人·知识图谱
6Hzlia7 小时前
【Hot 100 刷题计划】 LeetCode 141. 环形链表 | C++ 哈希表直觉解法
c++·leetcode·链表
gaoshengdainzi8 小时前
血管介入手术机器人测试系统YY/T·1994-2025
机器人·血管介入手术机器人测试系统
北顾笙9809 小时前
day35-数据结构力扣
数据结构·算法·leetcode
ulias21211 小时前
leetcode热题 - 4
算法·leetcode·职场和发展
圣保罗的大教堂11 小时前
leetcode 1559. 二维网格图中探测环 中等
leetcode
gaoshengdainzi11 小时前
下肢外骨骼机器人测试模体YY/T·1973-2025
机器人·下肢外骨骼机器人测试模体
_日拱一卒12 小时前
LeetCode:148排序链表
算法·leetcode·链表
生信研究猿12 小时前
leetcode 78.子集
算法·leetcode·深度优先
沫儿笙12 小时前
焊接机器人弧焊节气设备
人工智能·机器人