Leetcode 2069. 模拟行走机器人 II (数学模拟题目)

本文介绍了一个名为Robot的类,该类可以模拟一个机器人在一个指定宽度和高度的矩形区域内移动的过程。通过move方法输入步数,机器人会按照预设的路径移动,并能返回当前位置坐标及方向。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 

class Robot {
private:
    int step;
    int width;
    int height;
public:
    Robot(int width, int height) {
        this->width = width;
        this->height = height;
        step = 0;
    }
    
    void move(int num) {
        step = (step + num - 1) % ((width + height - 2) * 2) + 1; 
    }
    
    vector<int> getPos() {
        auto [x, y, _] = getPosAndDir();
        return {x, y};
    }

    tuple<int, int, string> getPosAndDir() {
        if (step < width) {
            return make_tuple(step, 0, "East");
        } else if (step < width + height - 1) {
            return make_tuple(width - 1, step - width + 1, "North");
        } else if (step < width * 2 + height - 2) {
            return make_tuple(width * 2 + height - 3 - step, height - 1, "West");
        } else {
            return make_tuple(0, (width + height - 2) * 2 - step, "South");
        }
    }
    
    string getDir() {
        auto [x, y, dir] = getPosAndDir();
        return dir;
    }
};

/**
 * Your Robot object will be instantiated and called as such:
 * Robot* obj = new Robot(width, height);
 * obj->move(num);
 * vector<int> param_2 = obj->getPos();
 * string param_3 = obj->getDir();
 */

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值