模拟行走机器人-c语言

模拟行走机器人-c语言

机器人在一个无限大小的 XY 网格平面上行走,从点 (0, 0) 处开始出发,面向北方。该机器人可以接收以下三种类型的命令 commands :

-2 :向左转 90 度
-1 :向右转 90 度
1 <= x <= 9 :向前移动 x 个单位长度

在网格上有一些格子被视为障碍物 obstacles 。第 i 个障碍物位于网格点 obstacles[i] = (xi, yi) 。

机器人无法走到障碍物上,它将会停留在障碍物的前一个网格方块上,但仍然可以继续尝试进行该路线的其余部分。

返回从原点到机器人所有经过的路径点(坐标为整数)的最大欧式距离的平方。(即,如果距离为 5 ,则返回 25 )

注意:

北表示 +Y 方向。
东表示 +X 方向。
南表示 -Y 方向。
西表示 -X 方向。

示例 1:

输入:commands = [4,-1,3], obstacles = []
输出:25
解释:
机器人开始位于 (0, 0):

  1. 向北移动 4 个单位,到达 (0, 4)
  2. 右转
  3. 向东移动 3 个单位,到达 (3, 4)
    距离原点最远的是 (3, 4) ,距离为 32 + 42 = 25

示例 2:

输入:commands = [4,-1,4,-2,4], obstacles = [[2,4]]
输出:65
解释:机器人开始位于 (0, 0):

  1. 向北移动 4 个单位,到达 (0, 4)
  2. 右转
  3. 向东移动 1 个单位,然后被位于 (2, 4) 的障碍物阻挡,机器人停在 (1, 4)
  4. 左转
  5. 向北走 4 个单位,到达 (1, 8)
    距离原点最远的是 (1, 8) ,距离为 12 + 82 = 65

代码解决如下,还是很不错的一题

int robotSim(int* commands, int commandsSize, int** obstacles, int obstaclesSize, int* obstaclesColSize){
    int d=0;
    int i,j;
    int x=0;
    int y=0;
    int l[commandsSize];
    int p=0;
    int lr;
    for(i=0;i<commandsSize;i++){
        if(commands[i]==-1){
            d=(d+1)%4;
            continue;
        }
     if(commands[i]==-2){
            d=abs(d-1+4)%4;
            continue;
        }

    if(commands[i]>=0){
        if(d==0)
        {
            lr=y+commands[i]+1;

             if(obstaclesSize>0)
            for(j=0;j<obstaclesSize;j++){
                if(y+commands[i]>=obstacles[j][1]&&x==obstacles[j][0]&&y<obstacles[j][1]){
                    if(obstacles[j][1]<lr){
                        lr=obstacles[j][1];
                    }
                    
                  
                }
            }
            y=lr-1;
             printf("xy %d %d %d ",x,y,x*x+y*y);
            l[p++]=x*x+y*y;

        }
        else if(d==1){
            lr=x+commands[i]+1;
            if(obstaclesSize>0)
              for(j=0;j<obstaclesSize;j++){
                if(x+commands[i]>=obstacles[j][0]&&y==obstacles[j][1]&&x<obstacles[j][0]){
                     if(obstacles[j][0]<lr){
                        lr=obstacles[j][0];
                    }
                }
            }


            x=lr-1;
          printf("xy %d %d %d ",x,y,x*x+y*y);
            l[p++]=x*x+y*y;
        }
        else if(d==2){
            lr=y-commands[i]-1;
             if(obstaclesSize>0)
             for(j=0;j<obstaclesSize;j++){
                if(y-commands[i]<=obstacles[j][1]&&x==obstacles[j][0]&&y>obstacles[j][1]){
                       if(obstacles[j][1]>lr){
                        lr=obstacles[j][1];
                    }
                }
            }
            y=lr+1;
            printf("xy %d %d %d ",x,y,x*x+y*y);
            l[p++]=x*x+y*y;     
        }
        else if(d==3)
        {
            lr=x-commands[i]-1;
             if(obstaclesSize>0)
             for(j=0;j<obstaclesSize;j++){
                if(x-commands[i]<=obstacles[j][0]&&y==obstacles[j][1]&&x>obstacles[j][0]){
                      if(obstacles[j][0]>lr){
                        lr=obstacles[j][0];
                    }
                }
            }

            x=lr+1;
           printf("xy %d %d %d ",x,y,x*x+y*y);
            l[p++]=x*x+y*y;
        }

    }
  




    }
   
    int max =0;
    for(i=0;i<p;i++){
        if(l[i]>max){
          //  printf("l %d ",l[i]);
            max=l[i];
        }
    }
     
  return max;


}
  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的灭火机器人C语言代码示例,仅供参考: ```c #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <time.h> #define TEMPERATURE_THRESHOLD 50 // 温度阈值,超过该温度则启动灭火程序 #define WATER_TANK_CAPACITY 100 // 水箱容量,单位为升 typedef struct { int current_water_level; // 当前水位,初始值为满水箱 bool is_running; // 灭火机器人的运行状态,初始值为false,即未启动 } FirefightingRobot; void detect_temperature(int current_temperature, FirefightingRobot *robot); void start_firefighting(FirefightingRobot *robot); void spray_water(FirefightingRobot *robot); void stop_firefighting(FirefightingRobot *robot); int main() { FirefightingRobot robot = {WATER_TANK_CAPACITY, false}; // 模拟温度检测,随机生成温度值,范围为0~100 srand(time(NULL)); // 初始化随机数生成器 while (true) { int current_temperature = rand() % 101; detect_temperature(current_temperature, &robot); sleep(1); // 暂停1秒钟 } return 0; } void detect_temperature(int current_temperature, FirefightingRobot *robot) { if (current_temperature > TEMPERATURE_THRESHOLD) { start_firefighting(robot); } } void start_firefighting(FirefightingRobot *robot) { if (!robot->is_running) { robot->is_running = true; while (robot->current_water_level > 0) { spray_water(robot); sleep(1); // 暂停1秒钟 } stop_firefighting(robot); } } void spray_water(FirefightingRobot *robot) { robot->current_water_level -= 1; printf("喷水灭火!当前水位:%dL\n", robot->current_water_level); } void stop_firefighting(FirefightingRobot *robot) { robot->is_running = false; printf("灭火机器人停止工作!\n"); } ``` 这是一个简单的灭火机器人C语言代码,其中包含了温度检测、灭火、停止工作等函数,可以根据需要进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值