简体   繁体   English

堆栈用于回溯迷宫

[英]Stack for backtracking maze

For the algorithm DFS for maze-generation: 对于迷宫生成算法DFS:

1) Is the stack value supposed to change once I push the stack in whenever there is a path to go? 1)每当有路径走入堆栈时,堆栈值是否应该更改?

For example as below 5x5 Maze: 例如下面的5x5迷宫:

0, 0, 0, 0, 0

0, 1, 0, 0, S

0, 1, 1, 1, 1

0, D, 0, 0, 0 

0, 0, 0, 0, 0

Let's say from source I move down one step (CurrentCell) 假设从源头上我向下移动了一个步骤(CurrentCell)

The stack will push in coordinates (5,2) (Current Source Location) 堆栈将推入坐标(5,2)(当前源位置)

Then when I reaches (5,3), the currentCell will become the Source. 然后,当我到达(5,3)时,currentCell将成为Source。 (5,3) (5,3)

Am i correct? 我对么?

So when the source moves to the next left path, the stack will push in the current coordinate, which is (5,3) so on and so for until it reaches a dead end instead of the destination. 因此,当源移动到下一个左路径时,堆栈将推入当前坐标,即(5,3),依此类推,直到到达死角而不是终点为止。

So as per above example: 因此,根据上述示例:

The stack will first push in value of 堆栈将首先压入

Stack(0) (5,2) Stack(1) (5,3) 堆栈(0)(5,2)堆栈(1)(5,3)

. . . .

The question is, how am i supposed to push in the x and y? 问题是,我应该如何推入x和y? Because the current problem is, even if I push in the x and y, the x and y stays at the original value instead of constantly updating. 因为当前的问题是,即使我推入x和y,x和y仍保持原始值,而不是不断更新。 That's the reason why my backtrack is not working, I guess. 我想这就是为什么我的跟踪无法正常工作的原因。

Code: 码:

(random == 3) //To check for bottom neighbor
                        {
                            if (y+ 2 < column) //In case the column overshots
                            {

                                {
                                    maze[x][y+ 1].setValue(1); //New Path Set as 1
                                    myStack.push(maze[x][y]);
                                    y= y+ 1;
                                    }
                               }
                           }

turns out I am correct. 原来我是对的。

                maze[x][y] = myStack.top();
                myStack.pop();
                cout << "Array X is : " << maze[x][y].getX();
                cout << "Array Y is : " << maze[x][y].getY();

All i need is to pop and check whether is the route I took backtracks back. 我需要做的只是弹出并检查我是否选择了回溯的路线。 Thanks all for the help! 谢谢大家的帮助!

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

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