二维数组上的深度优先搜索

van*_*738 6 java traversal multidimensional-array depth-first-search

我正在尝试通过创建一个程序来学习 DFS,该程序可以通过迷宫(二维数组)导航我的食人魔。这类似于日常编程挑战,但我只使用 1x1 食人魔。

我的迷宫:

static int[][] maze = { 
{2,1,0,0,0,0,0,0,0,0},
{0,0,1,0,0,0,0,0,0,0},
{1,0,0,0,0,1,0,1,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,1,1,0,0,0,0,0,0},
{0,0,1,0,0,0,0,1,0,1},
{1,1,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,1,1,0,0,0},
{0,0,0,0,0,1,0,0,0,3}};
Run Code Online (Sandbox Code Playgroud)

其中 2 是我的英雄 (0,0),3 是我的目标 (9,9),1 是障碍物,0 是可穿越的空间。

由于我是新手,我怀疑是否需要它,但为了便于复制和故障排除,我将包含整个程序。

import java.awt.Point;
import java.util.ArrayList;


public class OgrePath {

    static int[][] maze = { 
        {2,1,0,0,0,0,0,0,0,0},
        {0,0,1,0,0,0,0,0,0,0},
        {1,0,0,0,0,1,0,1,0,0},
        {0,0,0,0,0,0,0,0,0,0},
        {0,0,1,1,0,0,0,0,0,0},
        {0,0,1,0,0,0,0,1,0,1},
        {1,1,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,1,1,0,0,0},
        {0,0,0,0,0,1,0,0,0,3}};
public static boolean[][] visited = new boolean[maze.length][maze[0].length];
static ArrayList<Point> neighbors = new ArrayList<Point>();

public static void main(String[] args) {
    OgrePath OP = new OgrePath();
    for (int i=0;i<maze.length;i++){
        for (int j=0;j<maze[i].length;j++){
            visited[j][i] = false;
        }
    }
    visited[getOgre(maze).x][getOgre(maze).y] = true;
    System.out.println("Ogre: " + getOgre(maze));
    dfs(maze, getOgre(maze));
}

public static boolean dfs(int[][] maze, Point p){
    neighbors = getNeighbors(maze,p);
    if (maze[p.x][p.y] == 3){
        System.out.println("FOUND IT");
        return true;
    }
    if (neighbors.isEmpty()){
        return false;
    }
    for (int i=0;i<neighbors.size();i++){
        System.out.println("Nieghbors: " + neighbors);
        System.out.println(i + "(" + p.x + "," + p.y + ")");
        visited[neighbors.get(i).x][neighbors.get(i).y] = true;
        dfs(maze, neighbors.get(i));
    }
    return false;
}

public static ArrayList<Point> getNeighbors(int[][] maze, Point p){
    ArrayList<Point> neighbors = new ArrayList<Point>();
    Point left = new Point();
    Point right = new Point();
    Point down = new Point();
    Point up = new Point();
    down.x = p.x - 1;
    down.y = p.y;
    if (valid(maze,down)) neighbors.add(down);
    up.x = p.x + 1;
    up.y = p.y;
    if (valid(maze,up)) neighbors.add(up);
    left.x = p.x;
    left.y = p.y - 1;
    if (valid(maze,left)) neighbors.add(left);
    right.x = p.x;
    right.y = p.y + 1;
    if (valid(maze,right)) neighbors.add(right);
    return neighbors;
}

public static boolean valid(int[][] maze, Point p){
    if (inMaze(maze,p) && canGo(maze,p) && visited[p.x][p.y] == false) return true;
    else return false;
}

public static boolean inMaze(int[][] maze, Point p){
    if (p.x < (maze[0].length - 1) && p.x > -1 && p.y < (maze.length - 1) && p.y > -1){
        return true;
    } else return false;
}

public static boolean canGo(int[][] maze, Point p){
    if (maze[p.x][p.y] != 1 && maze[p.x][p.y] != 4) return true;
    else return false;  
}

public static Point getOgre(int[][] maze){
    Point ogre = new Point();
    for (int i=0;i<maze.length;i++){
        for (int j=0;j<maze[i].length;j++){
            if (maze[i][j] == 2){
                ogre.x = j;
                ogre.y = i;
            }
        }
    }
    return ogre;
}
}
Run Code Online (Sandbox Code Playgroud)

我希望能够递归调用 DFS,但是我编写它的方式使程序在探索 1 条可能的行并失败后停止。

Ang*_*ril 2

好的,我发现有一些问题会阻止您的代码正常工作,所以让我们一次看一个问题。

首先,您的 dfs 函数不会迭代“for”循环,因为它会立即返回。尝试改变

dfs(maze, neighbors.get(i));
Run Code Online (Sandbox Code Playgroud)

到

if(dfs(maze, neighbors.get(i))){
    return true;
}
Run Code Online (Sandbox Code Playgroud)

这解决了仅搜索单个路径的部分问题。

第二个问题是与邻居的关系。当你的 dfs 完全探索一条路径时,它应该后退一步并检查所有邻居。您只有一个顶级邻居变量,因此当您的分支以零邻居终止时,它认为所有较早的节点都有零邻居。

删除静态邻居变量

static ArrayList<Point> neighbors = new ArrayList<Point>();
Run Code Online (Sandbox Code Playgroud)

并在 getNeighbors 中放入一个非静态版本

ArrayList<Point> neighbors = new ArrayList<Point>();
Run Code Online (Sandbox Code Playgroud)

这几乎完全解决了搜索问题,但对于你的迷宫来说,你仍然找不到终点。

您的 inMaze 函数错误地检查边界。您正在检查 x 或 y 是否小于长度减一。您只需要使用“小于”来检查边界。

if (p.x < maze[0].length && p.x > -1 && p.y < maze.length && p.y > -1)
Run Code Online (Sandbox Code Playgroud)