实现堆栈以使用struct的回溯

时间:2015-03-09 20:55:31

标签: c struct backtracking robot

这是一个程序,使机器人能够通过迷宫跟踪黑线"。迷宫由7x7黑色线条组成,入口线穿过它。可能有空的正方形和线条无处可去。黑线相遇的地方有一个白色方块。

我们的想法是将这些方块保存在结构中,让机器人在迷宫中运行并将各个结构保存在堆栈中。

我想这样做,以便机器人可以通过回溯找到返回的路。

void push(int value ){
if(top ==49){ //uncertain what to write here 
} else{
    top = top+1;
    a[top] = value;
}

int pop(){

if(top ==-1){
    return -234;
}else{

    int r = a[top];
    top = top-1;
    return r;
   }
 }

我不确定如何使这与这个一起工作

typedef struct proxy {
int edges[4];
int visited;
int exists;
int token;
} proxy;

int main() {

   /* This part is to make simulation work, idea is to start in the middle
      an 14x14, because that allows us to go in any direction. 
      the arrays are the values that the simulation gives back. 
      This seems to work even though it's not pretty. */

   static int x = 7; 
   static int y = 7;
   static int zaehler = 0;  //Zähler für Rundenbegrenzung
   int Ary[14][14];
   int hilfN, hilfO, hilfS, hilfW;
   int i,j;
   int N[8] = {16, 48, 80, 112, 144, 176, 208, 240};
   int O[8] = {128, 144, 160, 176, 192, 208, 224, 240};
   int S[8] = {32, 48, 96, 112, 160, 176, 224, 240};
   int W[8] = {64, 80, 96, 112, 192, 208, 224, 240};
   int Intersec = Robot_GetIntersections();
   int myRobot_Move(x,y){
   return Robot_Move(x-7,y-7);
}
for(i= 0; i < 14; i++){

    for(j= 0; j < 14; j++){
        Ary[i][j] = 0;
        }
}

myRobot_Move(x,y);

while (x < 14 && y < 14) {
Intersec = Robot_GetIntersections();
Ary[x][y] = Intersec;

for (hilfN=0; hilfN<8; hilfN++){
    if(N[hilfN] == Intersec){
        if(Ary[x][y+1] == 0){
            y++;
            myRobot_Move(x,y);
        }
    }
}
//Osten
for (hilfO=0; hilfO<8; hilfO++){
        if(O[hilfO] == Intersec){
            if(Ary[x+1][y] == 0){
                x++;
                myRobot_Move(x,y);
            }
        }
    }
//Süden
for (i=0; i<8; i++){
        if(S[i] == Intersec){
            if(Ary[x][y-1] == 0){
                y--;
                myRobot_Move(x,y);
            }
        }
    }
//Westen
for (i=0; i<8; i++){
            if(W[i] == Intersec){
                if(Ary[x-1][y] == 0){
                x--;
                myRobot_Move(x,y);
            }
        }
    }

if (Ary[x][y+1] && Ary[x+1][y] && Ary[x][y-1] && Ary[x-1][y] > 0){
    break;
    printf("End");
   }
 }


return EXIT_SUCCESS;
}

1 个答案:

答案 0 :(得分:0)

如果您希望堆栈a保存struct proxy而不是int,请将假定的int a[50];更改为proxy a[50];和函数{{1 }和push()如下:

pop()