机器人路径模拟器

时间:2019-12-03 02:40:24

标签: c terminal

我对编程和所有方面都还很陌生,自从我们最近开始使用C以来,我们的任务是在假设的无限网格上仅使用三个可能的动作来创建程序(机器人模拟器):向右转,向左转并前进。输出只需要是x和y坐标(x,y)。现在的事情是,我只能通过所有四个运动(上,下,左,右-北,南,西,东,东)来理解并实现(假设没有向左转或向右转的选项,向左意味着移动1位置向左,向右表示向右移动1个位置等))。因此,仅需简单地减去它们即可得出最终坐标,但我根本不知道如何为带有2个转弯选项并前进的规则制定规则。我会将代码留给已经完成的任务,如果有人认为我可以升级该代码,或者对如何使其工作有任何建议或想法,请告诉我。

#include<stdio.h> 
static void position(int n, char* string)
{
    int left = 0, right = 0, up = 0, down = 0;
    for (int i = 0; i < n; i++) {
        if (string[i] == 'L')
            left++;
        else if (string[i] == 'R')
            right++;
        else if (string[i] == 'U')
            up++;
        else if (string[i] == 'D')
            down++;
    }
    printf("(%d,%d)", (right - left), (up - down));
}
int main()
{
    printf("Enter your movement: ");
    char string[1000];
    scanf("%s", string);
    int n;
    for (n = 0; string[n] != '\0'; ++n);
    string[100] = string[n];
    position(n, string);
}

1 个答案:

答案 0 :(得分:1)

解决这个问题的一种方法是使用笛卡尔坐标系。

check

我没有跟踪最后的一切,而是跟踪运动的进行情况。

#include<stdio.h> 

enum {NORTH/*UP*/,SOUTH/*DOWN*/, EAST/*LEFT*/, WEST/*RIGHT*/};
static void position(int n, char* string)
{                           // let's use a Cartesian coordinate system
    // assume a 10x10 grid
    int X=0,Y=0;            // start at this place within the grid (the center)
    int direction = NORTH;  // we are facing NORTH
    int i;
    for (i = 0; i < n; i++) {
        if (string[i] == 'L') // TURN LEFT, no step
        {   // pivot
            switch (direction)
            {
            case NORTH: 
                direction = EAST;   // face EAST, but do not step
                break;
            case SOUTH: 
                direction = WEST;   // face WEST, but do not step
                break;
            case EAST: 
                direction = SOUTH;  // face SOUTH, but do not step
                break;
            case WEST: 
                direction = NORTH;  // face NORTH, but do not step
                break;
            }
        }
        else if (string[i] == 'R') // TURN RIGHT, no step
        {   // pivot
            switch (direction)
            {
            case NORTH: 
                direction = WEST;   // face WEST, but do not step
                break;
            case SOUTH: 
                direction = EAST;   // face EAST, but do not step
                break;
            case EAST: 
                direction = NORTH;  // face NORTH, but do not step
                break;
            case WEST: 
                direction = SOUTH;  // face SOUTH, but do not step
                break;
            }
        }
        else if (string[i] == 'A')
        {   // advance
            switch (direction)
            {
            case NORTH: 
                if (Y == 5)
                    printf("you cannot go NORTH any more.\n");
                else
                    Y++;
                break;
            case SOUTH: 
                if (Y == -5)
                    printf("you cannot go SOUTH any more.\n");
                else
                    Y--;
                break;
            case EAST: 
                if (X == -5)
                    printf("you cannot go EAST any more.\n");
                else
                    X--;
                break;
            case WEST: 
                if (X == 5)
                    printf("you cannot go WEST any more.\n");
                else
                    X++;
                break;
            }
        }
        else
        {
            printf("%c is not a valid command.\n", string[i]);
        }
    }
    printf("(%d,%d)", X, Y);
}
int main()
{
    char string[1000];
    int n;
    for(;;)
    {   // only valid movements are turn Right,turn Left and Advance. (RLA)
        printf("Enter your movement: ");
        scanf("%s", string);
        for (n = 0; string[n] != '\0'; ++n);
        string[100] = string[n];
        position(n, string);
    }
}

I / O示例

输入您的动作:@

@不是有效的命令。

(0,0)

输入您的动作:LLLA

(1,0)

输入您的动作:LLLA

(1,0)

输入您的动作:LLLAAA

(3,0)

输入您的动作:LLARRRAA

(2,-1)

输入您的动作:LRLRLRA

(0,1)