机器人以相反的顺序遵循网格

发布于 2024-09-24 08:04:40 字数 234 浏览 2 评论 0原文

我应该只用 C 语言编写一个程序。这是网格追随者的代码。

我定义了网格的坐标系(0-5,0-5)。还定义了机器人方向(+y、-y、+x、-x)和位置。 (我欢迎有关如何为此编写良好代码的提示。)

现在,当我的机器人穿过网格并通过某个路径到达网格中的某个坐标(x,y)时。仅允许 90 度和 180 度转弯。

我怎样才能到达(0,0),即起点,穿过相同的路径?如何使用 C 代码反转方向并给出适当的转动指令?

I am supposed to write a program in C only. It's code for a grid follower.

I have defined a co-ordinate system of the grid (0-5,0-5). Also the bot orientations (+y,-y,+x,-x) and positions are defined. (I would welcome tips on how to write good code for this.)

Now as my bot moves through the grid and goes to some coordinate (x,y) in the grid through some path. Only 90 deg and 180 deg turns are allowed.

How can I reach (0,0), i.e. the starting point, traversing the same path? How can I reverse the orientations and give appropriate turning instructions using C code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

謸气贵蔟 2024-10-01 08:04:40

这可以清理很多,但它应该是理解概念的一个很好的框架。我们基本上所做的就是保存每一个动作,然后简单地回溯它。

#include <stdio.h>

#define MAX_STEPS 256

enum CARDINAL_DIRECTIONS { N = 0, W, S, E };

struct _s_robot {
    int x;
    int y;
    int orientation;
    int step;
    int x_history[MAX_STEPS];
    int y_history[MAX_STEPS];
    int turn_history[MAX_STEPS];
} MY_LITTLE_ROBOT = {0, 0, 0, 0, {0}, {0}, {0}};

void robot_go() {
    switch(MY_LITTLE_ROBOT.orientation) {
    case N:
        ++MY_LITTLE_ROBOT.y;
        break;
    case W:
        --MY_LITTLE_ROBOT.x;
        break;
    case S:
        --MY_LITTLE_ROBOT.y;
        break;
    case E:
        ++MY_LITTLE_ROBOT.x;
        break;
    }
    MY_LITTLE_ROBOT.x_history[MY_LITTLE_ROBOT.step] = MY_LITTLE_ROBOT.x;
    MY_LITTLE_ROBOT.y_history[MY_LITTLE_ROBOT.step] = MY_LITTLE_ROBOT.y;
    MY_LITTLE_ROBOT.turn_history[MY_LITTLE_ROBOT.step] = MY_LITTLE_ROBOT.orientation;
    ++MY_LITTLE_ROBOT.step;
}

void robot_change_orientation(int _orientation) {
    MY_LITTLE_ROBOT.orientation = _orientation;
}

void robot_reverse_orientation(int _orientation) {
    if (_orientation == N) MY_LITTLE_ROBOT.orientation = S;
    else if (_orientation == W) MY_LITTLE_ROBOT.orientation = E;
    else if (_orientation == S) MY_LITTLE_ROBOT.orientation = N;
    else if (_orientation == E) MY_LITTLE_ROBOT.orientation = W;
}

void robot_backtrack() {
    int i;
    printf("MY_LITTLE_ROBOT wants to turn around, currently at: %i, %i\n", MY_LITTLE_ROBOT.x, MY_LITTLE_ROBOT.y);
    robot_reverse_orientation(MY_LITTLE_ROBOT.orientation);
    for (i = MY_LITTLE_ROBOT.step-1; i >=  0; --i) {
        printf("Robot is @ %i, %i, with an orientation of: %i \n", MY_LITTLE_ROBOT.x, MY_LITTLE_ROBOT.y,  MY_LITTLE_ROBOT.orientation );
        robot_reverse_orientation(MY_LITTLE_ROBOT.turn_history[i]);
        robot_go();
    }
}

void dump_history() {
    int i;
    for (i = MY_LITTLE_ROBOT.step-1; i >=  0; --i) {
        printf("Robot is @ %i, %i, with an orientation of: %i \n", MY_LITTLE_ROBOT.x_history[i], MY_LITTLE_ROBOT.y_history[i],  MY_LITTLE_ROBOT.turn_history[i] );
    }
}

int main() {
    printf("***START: Robot is @ %i, %i\n", MY_LITTLE_ROBOT.x, MY_LITTLE_ROBOT.y);
    robot_go();
    robot_go();
    robot_go();
    robot_change_orientation(S);
    robot_go();
    robot_go();
    robot_change_orientation(W);
    robot_go();
    robot_go();
    robot_go();
    robot_change_orientation(N);
    robot_go();
    dump_history();
    robot_backtrack();
    printf("***FINISH: Robot is @ %i, %i\n", MY_LITTLE_ROBOT.x, MY_LITTLE_ROBOT.y);
    return 0;
}

要一遍又一遍地使用机器人,您可能必须在原路返回后重置所有旅行历史记录,这应该有点微不足道。为了冗长和简单起见,我故意避免了 malloc 和其他使程序真正有用的令人困惑的方面。希望它有帮助。

我还假设您不需要真正的寻路算法(例如 A*),因为那完全是不同的游戏。

This can be cleaned up quite a lot, but it should be a good framework to understand the concepts. All we're basically doing is saving every move, and then simply backtracking it.

#include <stdio.h>

#define MAX_STEPS 256

enum CARDINAL_DIRECTIONS { N = 0, W, S, E };

struct _s_robot {
    int x;
    int y;
    int orientation;
    int step;
    int x_history[MAX_STEPS];
    int y_history[MAX_STEPS];
    int turn_history[MAX_STEPS];
} MY_LITTLE_ROBOT = {0, 0, 0, 0, {0}, {0}, {0}};

void robot_go() {
    switch(MY_LITTLE_ROBOT.orientation) {
    case N:
        ++MY_LITTLE_ROBOT.y;
        break;
    case W:
        --MY_LITTLE_ROBOT.x;
        break;
    case S:
        --MY_LITTLE_ROBOT.y;
        break;
    case E:
        ++MY_LITTLE_ROBOT.x;
        break;
    }
    MY_LITTLE_ROBOT.x_history[MY_LITTLE_ROBOT.step] = MY_LITTLE_ROBOT.x;
    MY_LITTLE_ROBOT.y_history[MY_LITTLE_ROBOT.step] = MY_LITTLE_ROBOT.y;
    MY_LITTLE_ROBOT.turn_history[MY_LITTLE_ROBOT.step] = MY_LITTLE_ROBOT.orientation;
    ++MY_LITTLE_ROBOT.step;
}

void robot_change_orientation(int _orientation) {
    MY_LITTLE_ROBOT.orientation = _orientation;
}

void robot_reverse_orientation(int _orientation) {
    if (_orientation == N) MY_LITTLE_ROBOT.orientation = S;
    else if (_orientation == W) MY_LITTLE_ROBOT.orientation = E;
    else if (_orientation == S) MY_LITTLE_ROBOT.orientation = N;
    else if (_orientation == E) MY_LITTLE_ROBOT.orientation = W;
}

void robot_backtrack() {
    int i;
    printf("MY_LITTLE_ROBOT wants to turn around, currently at: %i, %i\n", MY_LITTLE_ROBOT.x, MY_LITTLE_ROBOT.y);
    robot_reverse_orientation(MY_LITTLE_ROBOT.orientation);
    for (i = MY_LITTLE_ROBOT.step-1; i >=  0; --i) {
        printf("Robot is @ %i, %i, with an orientation of: %i \n", MY_LITTLE_ROBOT.x, MY_LITTLE_ROBOT.y,  MY_LITTLE_ROBOT.orientation );
        robot_reverse_orientation(MY_LITTLE_ROBOT.turn_history[i]);
        robot_go();
    }
}

void dump_history() {
    int i;
    for (i = MY_LITTLE_ROBOT.step-1; i >=  0; --i) {
        printf("Robot is @ %i, %i, with an orientation of: %i \n", MY_LITTLE_ROBOT.x_history[i], MY_LITTLE_ROBOT.y_history[i],  MY_LITTLE_ROBOT.turn_history[i] );
    }
}

int main() {
    printf("***START: Robot is @ %i, %i\n", MY_LITTLE_ROBOT.x, MY_LITTLE_ROBOT.y);
    robot_go();
    robot_go();
    robot_go();
    robot_change_orientation(S);
    robot_go();
    robot_go();
    robot_change_orientation(W);
    robot_go();
    robot_go();
    robot_go();
    robot_change_orientation(N);
    robot_go();
    dump_history();
    robot_backtrack();
    printf("***FINISH: Robot is @ %i, %i\n", MY_LITTLE_ROBOT.x, MY_LITTLE_ROBOT.y);
    return 0;
}

To use the robot over and over again, you may have to reset all travel history after a backtrack, which should be somewhat trivial. I purposely avoided mallocs and other confusing aspects of making the program actually useful for the sake of verbosity and simplicity. Hopefully it helps.

I also assumed you didn't want a true path-finding algorithm (such as A*), because that's a different ballgame altogether.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文