当前位置: 首页 > news >正文

困于环中的机器人

*************

c++

topic: 1041. 困于环中的机器人 - 力扣(LeetCode)

*************

Inspect the topic first.

And it looks really familiar with another robot topic.

657. 机器人能否返回原点 - 力扣(LeetCode)https://leetcode.cn/problems/robot-return-to-origin/description/?envType=study-plan-v2&envId=programming-skills

You gays have found what are the differences? That is direction.

Here is the code without considering the direction.

class Solution {
public:
    bool isRobotBounded(string instructions) {
        
        int x = 0;
        int y = 0;
 
        for (char c : instructions) 
        {
            if (c == 'U') y++;
            else if (c == 'D') y--;
            else if (c == 'R') x++;
            else if (c == 'L') x--;
        }
        
        return x == 0 && y == 0;
    }
};

To see if there is any spark from the basic code to solve this problem? In the topic, the robot need to change the direction.

  • L --> turn left
  • R --> turn right
  • G --> go

So what is to describe the direction? Maybe the coordinate axis system, like (x, y)? coordinate axis system is necessary. And besides that, need to add direction.

direction = 0, 向 ↑y++;
direction = 1, 向 →x++;
direction = 2, 向 ↓y--;
direction = 3, 向 ←x--;

    Good.

    class Solution {
    public:
        bool isRobotBounded(string instructions) {
            int x = 0, y = 0;
            int dir = 0; // 0: North, 1: East, 2: South, 3: West
            
            for (char c : instructions) 
            {
                if (c == 'G') 
                {
                    if (dir == 0) 
                    {
                        y++; // Move North
                    } 
                    else if (dir == 1) 
                    {
                        x++; // Move East
                    } 
                    else if (dir == 2) 
                    {
                        y--; // Move South
                    } 
                    else if (dir == 3) 
                    {
                        x--; // Move West
                    }
                } 
                else if (c == 'L') 
                {
                    if (dir == 0) 
                    {
                        dir = 3; // North -> West
                    } 
                    else if (dir == 1) 
                    {
                        dir = 0; // East -> North
                    } 
                    else if (dir == 2) 
                    {
                        dir = 1; // South -> East
                    } else if (dir == 3) 
                    {
                        dir = 2; // West -> South
                    }
                } 
                else if (c == 'R') 
                {
                    if (dir == 0) 
                    {
                        dir = 1; // North -> East
                    } 
                    else if (dir == 1) 
                    {
                        dir = 2; // East -> South
                    } 
                    else if (dir == 2) 
                    {
                        dir = 3; // South -> West
                    } else if (dir == 3) 
                    {
                        dir = 0; // West -> North
                    }
                }
            }
            
            // Robot is in a circle if:
            // 1. It returns to (0, 0), or
            // 2. It's not facing North (dir != 0)
            return (x == 0 && y == 0) || (dir != 0);
        }
    };
    

    too many if.

    The original position is dir = 0; (0, 0);

    if turn left, dir = 3; (0, 0);

    if continue turn left, dir = 2; (0, 0);

    if continue turn left, dir = 1; (0, 0);

    So you can see the pired of turns around is 4 steps. %4 maybe works.

    L -> turn left -> dir - 1. but if dir == 0 and dir - 1 is negative, that unsatisfied. so add a period:

    L -> turn left -> (dir - 1 + 4) % 4;

    R -> turn right -> (dir + 1) % 4;

    • Let's take an example.
    • the position is (0, 0) and direction is 2(↓):
    • if L:turn left, should be 1(↑), dir = ( 2 - 1 + 4) % 4 = 1;
    • if R: turn right, should be 3(←), dir = ( 2 + 1) % 4 = 3;

    so lets change a little code.

    class Solution {
    public:
        bool isRobotBounded(string instructions) {
            
            int x = 0;
            int y = 0;
            int dir = 0; // 0: North, 1: East, 2: South, 3: West
    
            for (char c: instructions)
            {
                if (c == 'G')
                {
                    if (dir == 0)
                    {
                        y++;
                    }
                    else if (dir == 1)
                    {
                        x++;
                    }
                    else if (dir == 2)
                    {
                        y--;
                    }
                    else if (dir == 3)
                    {
                        x--;
                    }
                }
    
                else if (c == 'L')
                {
                    dir = (dir - 1 + 4) % 4;
                }
                else if (c == 'R')
                {
                    dir = (dir + 1) % 4;
                }
            }
    
            return (x == 0 && y == 0) || (dir != 0);
        }
    };

    and the  code here looks unreadable. So introoduce switch algorhythm in c++.

    switch (expression) {
        case value1:
            // 当 expression == value1 时执行的代码
            break;
        case value2:
            // 当 expression == value2 时执行的代码
            break;
        // 可以有任意数量的 case
        default:
            // 当 expression 不匹配任何 case 时执行的代码
    }
    

    for a little example:

    #include <iostream>
    using namespace std;
    
    int main() {
        int choice = 2;
    
        switch (choice) {
            case 1:
                cout << "选择 1" << endl;
                break;
            case 2:
                cout << "选择 2" << endl;
                break;
            case 3:
                cout << "选择 3" << endl;
                break;
            default:
                cout << "无效选择" << endl;
        }
    
        return 0;
    }
    
    //输出2

    Then use switch in the program.

    class Solution {
    public:
        bool isRobotBounded(string instructions) {
    
            int x = 0;
            int y = 0;
            int dir = 0; // 0: North, 1: East, 2: South, 3: West
    
            for (char c : instructions) 
            {
                if (c == 'G') 
                {
                    switch (dir) 
                    {
                    case 0:
                        y++;
                        break;
                    case 1:
                        x++;
                        break;
                    case 2:
                        y--;
                        break;
                    case 3:
                        x--;
                        break;
                    }
                }
    
                else if (c == 'L') 
                {
                    dir = (dir - 1 + 4) % 4;
                } 
                else if (c == 'R') 
                {
                    dir = (dir + 1) % 4;
                }
            }
    
            return (x == 0 && y == 0) || (dir != 0);
        }
    };

    相关文章:

  • 【2025 年华为杯广东工业大学程序设计竞赛(同步赛)】部分题解
  • JavaScript函数知识点总结
  • 【力扣hot100题】(027)两数相加
  • CST学习笔记(三)MATLAB与CST联合仿真-远场数据批量导出
  • 【学Rust写CAD】22 双圆径向渐变的结构体(two_circle_radial_gradient.rs)
  • 现代简洁线条视觉冲击几何风psai无衬线英文字体安装包 Adobe Fonts – Transducer Font Family
  • RK3588使用笔记:ubuntu/麒麟系统功能测试程序
  • 博客学术汇测试报告
  • Mamba4D阅读
  • 人工智能大模型-数据预处理-文本数据预处理-图像数据预处理
  • HCIA-数据通信datacom认证
  • Cookie与Token详解及测试需重点关注点
  • JxBrowser 8.5.1 版本发布啦!
  • npu踩坑记录
  • C++设计模式-迭代器模式:从基本介绍,内部原理、应用场景、使用方法,常见问题和解决方案进行深度解析
  • AI提示语:个人学习路线规划
  • Linux基础入门:从零开始掌握Linux命令行操作
  • 【Feign】⭐️使用 openFeign 时传递 MultipartFile 类型的参数参考
  • 【分享】内外网文件摆渡系统:让数据传输更安全更可靠
  • ORB-SLAM学习感悟记录
  • 长春百度网站快速排名/凡科建站快车
  • 中国临沂网站优化/站外推广
  • 网站备案 每年/seo软件服务
  • 怎样搭建一个企业网站/南昌seo全网营销
  • 修改 网站 数据库/网站策划方案
  • 怎么把网站做二维码/如何推广网站方法