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

标签 c struct backtracking robot

这是一个使机器人能够沿着黑线穿过“迷宫”的程序。迷宫由 7x7 的黑线组成,并有一条入口线穿过其中。可能会有空的方 block 和无处可去的线条。黑线相交处有一个白色方 block 。

这个想法是将这些方 block 保存在一个结构中,让机器人穿过迷宫并将各个结构保存在一个堆栈中。

我想这样做是为了让机器人能够通过回溯找到回去的路。

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;
}

最佳答案

如果您希望堆栈 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--];
}

关于c - 实现堆栈以对结构使用回溯,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28951574/

相关文章:

c - 如何在 32 位 Linux 内核下映射 1GB(或更多)的物理内存

c - 如何将指针分配给此数组(OpenCL)中某些结构的数组元素?

algorithm - 高度h的二叉搜索树构建

c - 在c中通过函数填充typedef结构

python - python中这个递归问题的解释

c++ - 我的骑士之旅算法可能在无限循环中运行

c - C 中的哈希表(查找每个单词的频率)

c - 除了用作全局变量之外,如何使用 'flag' 变量?

c - 错误: incompatible integer to pointer conversion assigning to 'string' (aka 'char *' ) from 'int'

c - C 中的可移植性问题