小编van*_*738的帖子

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

我正在尝试通过创建一个程序来学习 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 …
Run Code Online (Sandbox Code Playgroud)

java traversal multidimensional-array depth-first-search

6
推荐指数
1
解决办法
1万
查看次数