深度优先搜索-2D游戏地图
问题内容:
我创建了一个2D迷宫,我想找到红色->蓝色节点之间的最快路径。我不确定如何实施深度优先搜索。我知道邻接矩阵或列表可以用来表示节点之间的连接。虽然,我不确定如何构造它。
为简便起见
:我需要返回一个列表,其中搜索了瓷砖坐标(当寻找目标节点时),因此我可以在迷宫中描述搜索。或者我将为此构造一个邻接矩阵?以及相应的顶点列表?
深度优先搜索的一般结构
- 访问节点(单元格)(将访问标志更改为true)
- 推入堆叠
- 如果没有,则获取未访问的顶点(窥视堆栈)(弹出堆栈)-更新迷宫模型视图
重复1-3,直到堆栈为空
这是迷宫类的当前代码。
public class Maze {
//Tile ids
public static short OBSTICLE = 0;
public static short START_LOC_VALUE = -2;
public static short GOAL_LOC_VALUE = -3;
private int rows, cols;
private int numTiles;
private int[][] map;
private int[][] adjMatrix;
private Queue theQueue;
public Maze(int rows, int cols){
this.rows = rows;
this.cols = cols;
theQueue = new Queue();
numTiles = rows*cols;
map = new int[rows][cols];
adjMatrix = new int[rows][cols];
for (int i=0; i<rows; i++) {
for (int j=0; j<cols; j++) {
map[i][j] = 1;
}
}
}
/*
* Generate Maze
* @param numObstacles - number of obstacles
*/
public void generateMaze(int numObstacles){
for (int i = 0; i < numObstacles; i++)
setTile((int)(Math.random()*rows),(int)(Math.random()*cols), Maze.OBSTICLE);
//setTile((int)(Math.random()*rows),(int)(Math.random()*cols),Maze.START_LOC_VALUE);
//setTile((int)(Math.random()*rows),(int)(Math.random()*cols),Maze.GOAL_LOC_VALUE);
createAdjMatrix();
}
public void createAdjMatrix(){
for (int i=0; i<rows; i++) {
for (int j=0; j<cols; j++) {
if (map[i][j] == 1) {
addEdge(i,j);
}
}
}
}
/*
* Set Tile
* @param x - x cord
* @param y - y cord
* @param entity - OBSTICLE,START_LOC_VALUE or GOAL_LOC_VALUE ID
*/
public void setTile(int x, int y, short entity){
this.map[x][y] = entity;
}
public void addEdge(int start, int end) {//Start and end arguments index multidimensional array
adjMatrix[start][end] = 1;
adjMatrix[end][start] = 1;
}
public void bfs(int startDest, int goalDest) // breadth-first search
{
// begin at vertex 0
vertexList[startDest].wasVisited = true; // mark it
displayVertex(startDest); // display it
theQueue.enQueue(startDest); // insert at tail
int v2;
while (!theQueue.isEmpty()) // until queue empty,
{
int v1 = theQueue.deQueue(); // remove vertex at head
// until it has no unvisited neighbors
while ((v2 = getAdjUnvisitedVertex(v1)) != -1)
{ // get one,
vertexList[v2].wasVisited = true; // mark it
displayVertex(v2); // display it
theQueue.enQueue(v2); // insert it
} // end while(unvisited neighbors)
} // end while(queue not empty)
// queue is empty, so we’re done
for (int j = 0; j < nVerts; j++) // reset flags
vertexList[j].wasVisited = false;
}// end bfs()
/*
* Drawn Maze
* @param g - Graphics object
*/
public void draw(Graphics g){
for (int y = 0; y < cols; y++)
for (int x = 0; x < rows; x++) {
int val = map[x][y];
if (val==Maze.OBSTICLE) {
g.setColor(Color.BLACK);
g.fillRect(x*20, y*20, 20, 20);
}else if(val == Maze.START_LOC_VALUE){
g.setColor(Color.RED);
g.fillRect(x*20, y*20, 20, 20);
}else if(val==Maze.GOAL_LOC_VALUE){
g.setColor(Color.BLUE);
g.fillRect(x*20, y*20, 20, 20);
}else{
g.setColor(Color.BLACK);
g.drawRect(x*20, y*20, 20, 20);
}
}
}
}
当前的DFS代码。
public void dfs(int vertexStart) // depth-first search
{
// begin at vertexStart
vertexList[vertexStart].wasVisited = true; // mark it
displayVertex(vertexStart); // display it
theStack.push(vertexStart); // push it
while (!theStack.isEmpty()) // until stack empty,
{
// get an unvisited vertex adjacent to stack top
int v = getAdjUnvisitedVertex(theStack.peek());
if (v == -1) // if no such vertex,
theStack.pop(); // pop a new one
else // if it exists,
{
vertexList[v].wasVisited = true; // mark it
displayVertex(v); // display it
theStack.push(v); // push it
}
} // end while
}
下图描述了迷宫结构,它是伪随机生成的;最终的迷宫实施将得到完善。
谢谢,如果您能引导我朝正确的方向前进,我将非常满意…
问题答案:
对于2D迷宫,您可以简单地使用广度优先搜索而不是深度优先搜索,如果您有n * n个迷宫,它将在O(n 2)中找到它。
但是还有另一种选择,它是一种标签和BFS,可在您的迷宫上工作(无需绘制图形)。
编号算法
理解广度优先搜索的一种有趣方法是以这种方式(对于迷宫)进行搜索:
-
将所有单元格设置为0,并将块设置为-1
-
从源位置开始,将其设置为1,将所有0个邻居标记为2,并将所有2个邻居保存在列表中。之后,将2的所有0个邻居标记为3,清除2的列表并保存3的列表,然后继续到达目的地。在每个级别中,请勿更改源值。
-
现在假设您在目的地并且想要查找路径,您的目的地的得分为m,找到得分为m-1的邻居,....并输出路径。
实际上,BFS的常规和简单方式就是使用Q,但是我之所以提供此功能是因为它很简单,因为它模拟了Q方式。
使用邻接矩阵
为了创建邻接矩阵,您应该命名节点和边,因此您可以为边缘和节点设置一个如下所示的类(我用伪C#编写):
public class Edge
{
public Edge(Node start, Node end, decimal weight)
{
StartNode = ...,...,...
}
public Node StartNode;
public Node EndNode;
public decimal weight;
public bool IsDirected;
}
public class Node
{
public Node(int index)
{
this.Index = index;
}
public int Index;
public List<Edge> Edges = new List<Edge>();
public bool Visited = false;
}
现在,您的图形是您的Node对象的列表:
public class Graph
{
public List<Node> Nodes = new List<Node>();
}
对于建模迷宫,您应该选择单元作为节点,并在相邻单元之间绘制边缘。我将留给您如何在图上添加节点。