2010-09-18 110 views
0

我应该只用C编写程序。这是一个网格追随者的代码。Bot以相反顺序排列网格

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

现在,我的bot在网格中移动,并通过某个路径转到网格中的某个坐标(x,y)。只允许90度和180度转弯。

我该如何达到(0,0),即起始点,遍历相同的路径?如何反转方向并使用C代码提供适当的车削指令?

+2

正在做作业吗? – t0mm13b 2010-09-18 21:13:49

+0

嗯......答案取决于你如何选择*得到*在那里摆在首位。如果你在水平方向移动,那么垂直方向回到相同的路径将需要在垂直方向移动,然后水平移动,不是吗?所以... – dmckee 2010-09-18 21:22:50

+0

机器人如何从一个位置移动到另一个位置?请用**代码**回答! – pmg 2010-09-18 21:32:51

回答

2

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

#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以及其他令人困惑的方面,使得该程序为了冗长和简单而实际上有用。希望它有帮助。

我也假设你不想要true路径寻找算法(如A *),因为这是一个不同的球赛。

+1

+1 LITTLE_CUTE_ROBOT的一个不错的开始 – pmg 2010-09-18 23:21:51

+0

@ david ...非常感谢...!非常好的代码结构...(至少对于像我这样的初学者!:P) – Kaushal 2010-09-20 18:19:25