简体   繁体   English

使用队列数据结构解决迷宫?

[英]Solve maze using queue data structure?

I'm taking a class in data structures and was given the assignment to find the shortest path through a maze using C and implementing the queue data structure. 我正在学习数据结构类,并获得了分配任务,可以使用C并通过实现队列数据结构来找到通过迷宫的最短路径。 However, I can't really wrap my head around how to use a queue here. 但是,我无法真正围绕如何在这里使用队列而烦恼。

I know the idea is to count every possible move from the start position, and when you hit the target, you're supposed to trace back to the initial position. 我知道这个想法是要计算从起始位置开始的所有可能移动,当您击中目标时,应该追溯到初始位置。 This is what I don't understand. 这是我不明白的。 Because if I use a queue and delete all the moves that leads up to the target, I have no data to use to do the trace back, and if I don't delete the moves that lead to the target (ie saving all the possible moves and deleting them when I actually do the trace back), I might as well be using a stack. 因为如果我使用队列并删除所有导致目标的移动,那么我将没有数据可用于追溯,并且如果我不删除导致目标的移动(即保存所有可能的操作)移动并在实际执行追溯时将其删除),那么我不妨使用堆栈。

I know there's something I don't quite get, but I can't figure out what it is. 我知道有些事情我还不太了解,但我不知道是什么。 How would I utilize the queue data structure in this case? 在这种情况下,我将如何利用队列数据结构?

What your professor is trying to get you to use is called "breadth-first search". 教授试图让您使用的东西称为“宽度优先搜索”。 The queue comes in for deciding which spaces to explore next. 队列进入以决定下一步要探索的空间。 When you are looking at the possible paths to take, you enqueue all the paths you have yet to explore. 当您考虑采取的可能途径时,您会排队所有尚未探索的途径。 Instead of continuing down the path you're on (which would be "depth-first search"), you dequeue the next spot you need to check, which will take you back to one of the positions you were considering earlier. 您无需继续前进(这是“深度优先搜索”),而是使需要检查的下一个地点出队,这将使您回到之前考虑的位置之一。

The actual implementation is up to you, I'd recommend looking for examples of breadth-first search online. 实际的实现取决于您,我建议您在线查找广度优先搜索的示例。

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

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