实现堆栈以对结构使用回溯

问题描述 投票:0回答:1

这是一个使机器人能够沿着黑线穿过“迷宫”的程序。迷宫由 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;
}
c struct backtracking robotics
1个回答
0
投票

如果您希望堆栈

a
保存
struct proxy
而不是
int
,请将假定的
int a[50];
更改为
proxy a[50];
以及函数
push()
pop()
,如下所示:

void push(proxy value)
{
    if (top == 49) puts("'a' stack overflow"), exit(1);
    else a[++top] = value;
}

proxy pop()
{
    if (top == -1) return (proxy){-234};
    else return a[top--];
}
© www.soinside.com 2019 - 2024. All rights reserved.