2069. 模拟行走机器人 II

2069. 模拟行走机器人 II

给你一个在 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 move(int num) 给机器人下达前进 num 步的指令。
int[] getPos() 返回机器人当前所处的格子位置,用一个长度为 2 的数组 [x, y] 表示。
String getDir() 返回当前机器人的朝向,为 "North" ,"East" ,"South" 或者 "West" 。

示例 1:
在这里插入图片描述

example-1

输入:
[“Robot”, “move”, “move”, “getPos”, “getDir”, “move”, “move”, “move”, “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.move(2); // 机器人朝东移动 2 步,到达 (2, 0) ,并朝东。
robot.move(2); // 机器人朝东移动 2 步,到达 (4, 0) ,并朝东。
robot.getPos(); // 返回 [4, 0]
robot.getDir(); // 返回 “East”
robot.move(2); // 朝东移动 1 步到达 (5, 0) ,并朝东。
// 下一步继续往东移动将出界,所以逆时针转变方向朝北。
// 然后,往北移动 1 步到达 (5, 1) ,并朝北。
robot.move(1); // 朝北移动 1 步到达 (5, 2) ,并朝 北 (不是朝西)。
robot.move(4); // 下一步继续往北移动将出界,所以逆时针转变方向朝西。
// 然后,移动 4 步到 (1, 2) ,并朝西。
robot.getPos(); // 返回 [1, 2]
robot.getDir(); // 返回 “West”

解题代码如下:

typedef struct {
    int drection_now;
    int x;
    int y;
    int x_limit;
    int y_limit;
} Robot;
int drection[4][2]={{1,0},{0,1},{-1,0},{0,-1}};


Robot* robotCreate(int width, int height) {
    Robot* R=(Robot* )malloc(sizeof(Robot));
    R->x=0;
    R->y=0;
    R->x_limit=width-1;
    R->y_limit=height-1;
    R->drection_now=0;
    return R;

    
}

void robotStep(Robot* obj, int num) {
    
    int a=num;
    num=num%((obj->x_limit+obj->y_limit)*2);
     if(a!=0&&num==0 && obj->x == 0 && obj->y == 0&&obj->drection_now==0){ // 如果刚好是整数倍,走了一圈回到原地,这时方便从东变为南
        obj->drection_now=(obj->drection_now+3)%4;
    }
   

    
   
   

  
  
    while(num!=0){
        
        int x_new=obj->x+drection[obj->drection_now][0];
        int y_new=obj->y+drection[obj->drection_now][1];;
        if(x_new>=0&&x_new<=obj->x_limit&&y_new>=0&&y_new<=obj->y_limit){
             obj->x=x_new;
             obj->y=y_new;
             num--;

        }
        else{
            obj->drection_now=(obj->drection_now+1)%4;
        }
    
    }
    
  //  printf("XY %d %d ", obj->x, obj->y);
  
}

int* robotGetPos(Robot* obj, int* retSize) {
    int *a=(int *)malloc(sizeof(int)*2);
    a[0]=obj->x;
    a[1]=obj->y;
    *retSize=2;
  //  printf("--%d %d ",retSize[0],retSize[1]);
    return a;
  
}
char Dir[4][10]={"East\0","North\0","West\0","South\0"};
char * robotGetDir(Robot* obj) {
   
   // printf("%d %s",obj->drection_now, Dir[obj->drection_now]);
    return Dir[obj->drection_now];
  
}

void robotFree(Robot* obj) {
    
}

/**
 * Your Robot struct will be instantiated and called as such:
 * Robot* obj = robotCreate(width, height);
 * robotStep(obj, num);
 
 * int* param_2 = robotGetPos(obj, retSize);
 
 * char * param_3 = robotGetDir(obj);
 
 * robotFree(obj);
*/
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值