简体   繁体   English

使用 C 中的堆栈获取迷宫的出口

[英]Get the exit of maze using stack in C

I have an algorithm that search for a exit of a maze using recursive function, how i pass this to a function without recursive but using stack?What this algorithm do is basically, try to found the exit of a matriz, if the next step is 0 or if has already been covered comeback and try another path, but ai need to change the recursive function to a function using stack instead of recursion how i do that?我有一个使用递归 function 搜索迷宫出口的算法,我如何将它传递给 function 而不使用递归但使用堆栈?这个算法的作用基本上是,尝试找到矩阵的出口,如果下一步是0 或者如果已经被覆盖卷土重来并尝试另一条路径,但是我需要使用堆栈而不是递归将递归 function 更改为 function 我怎么做?

    void print_maze(char **maze, int width, int height) {

for (int i=0; i<13; i++ ){
  for (int j=0; j<10; j++ )
  {
     printf ("%d", maze[i][j]);
  }
printf("\n");
}
}

int maze(int x_current, int y_current,char **mazeHere, int height,int width)
{
    if (x_current < 0 || x_current >= width || y_current < 0 || y_current >= height)
        return 0;

        char here = mazeHere[x_current][y_current];
        if (here == 3)
        return 1;

        if(here == 0 || here == 2)
            return 0;


        mazeHere[x_current][y_current] = 2;
         if(maze(x_current ,y_current-1,mazeHere,height,width))return 1;
        if(maze(x_current+1,y_current,mazeHere,height,width))return 1;
        if(maze(x_current,y_current+1,mazeHere,height,width))return 1;

          if(maze(x_current-1,y_current,mazeHere,height,width))return 1;



       else{ mazeHere[x_current][y_current] = 1;
            return 0;
       }

}


int main(void)
{
char matriz [13][10]={
{0,0,0,0,0,0,0,0,0,0},
{0,0,1,1,1,1,1,0,0,0},
{0,0,1,0,0,0,1,0,0,0},
{0,0,1,0,0,0,1,0,0,0},
{1,1,1,0,1,1,1,1,1,0},
{0,1,0,0,1,0,0,0,0,0},
{0,1,1,0,1,1,1,0,0,0},
{0,0,1,0,1,0,0,0,1,0},
{0,0,1,0,0,0,1,1,1,0},
{0,0,1,1,1,1,1,0,0,0},
{0,0,0,0,0,0,1,0,0,0},
{0,0,0,0,0,0,1,1,1,3},
{0,0,0,0,0,0,0,0,0,0},};
int b= 0;
int height = 10;
int width = 13;
int p = 0;
int o = 0;
char **a = malloc(width * sizeof(char*));
for(int x = 0; x< width; x++){
    a[x] = malloc(height * sizeof(char));
}


for (int i=0; i<13; i++ ){
  for (int j=0; j<10; j++ )
  {
      a[i][j]= matriz[i][j];
  }

}



int res = maze(4,0,a,height,width);
    puts(res ? "success!" : "no luck!");
print_maze(a, width, height);
return 0;
}

I understand it's hard without recursion.我知道没有递归很难。 By the way, conversion from recursion into using stack is not so hard, if you wrote code of stack data structure and its operation.顺便说一句,如果您编写了堆栈数据结构及其操作的代码,那么从递归到使用堆栈的转换并不难。 We are going to pursue the matter on the assume.我们将在假设上追究此事。

Recursive function basically has structure like this:递归 function 基本上有这样的结构:

position *seek(position *now, type arg...){
    ...
    if(condition){
        return NULL;
    } else {
        now->next = seek(arg_next);
    }
}

It's OK to understand just with your feeling.只凭自己的感觉就可以理解。 What it does here is chaining new result to results until now.它在这里所做的是将新结果链接到现在的结果。 In other words, storing results as data structure.换句话说,将结果存储为数据结构。 Here, data structure changes to stack.在这里,数据结构更改为堆栈。 Then...:然后...:

void seek(stack *stack, type arg...){
    ...
    while(1){
        if(condition){
            break;
        } else {
            stack->push(next_result)
        }
    }
}

It's so easy.它是如此容易。 All that is left is increasing conditions and considering backtracking, Based on the above, try to rewrite your code!剩下的就是增加条件和考虑回溯,基于以上,尝试重写你的代码!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM