java迷宫DFS

package Date_5_12;
import java.util.Scanner;

//DFS 迷宫算法

public class maze_2 {
    static int[][] maze;//地图
    static int x;//地图x向量
    static int y;//地图y向量
    static int[][] dis;//每个点的距离起点
    public static void main(String args[]){
        Scanner in =new Scanner(System.in);
        System.out.println("输入地图的大小");
        x = in.nextInt();
        y = in.nextInt();
        maze = new int[x][y];
        dis= new int[x][y];
        System.out.println("输入地图");
        for (int i = 0; i < x; i++)
            for (int j = 0; j < y; j++)
                maze[i][j] = in.nextInt();
        //System.out.println(i+" "+ j+" "+maze[i][j]);
        System.out.println("输入入口,输入出口");
        int in_x = in.nextInt();
        int in_y = in.nextInt();
        int out_x = in.nextInt();
        int out_y = in.nextInt();
        DFS_1(in_x, in_y);
        System.out.println(dis[out_x][out_y]);
    }

    static int DFS_1(int in_x,int in_y){
        maze[in_x][in_y]=1;
        System.out.println(0+"["+in_x+","+in_y+"]");
        if(in_x-1>=0 && maze[in_x-1][in_y]==0)//向上
        {
            System.out.println(1+"["+in_x+","+in_y+"]");
            next(in_x,in_y,in_x-1,in_y);
        }
        if (in_x+1=0 && maze[in_x][in_y-1]==0)//向左
        {
            System.out.println(4+"["+in_x+","+in_y+"]");
            next(in_x,in_y,in_x,in_y-1);
        }
        return 0;
    }
    static int next(int x_1,int y_1,int a,int b){
        dis[a][b]=dis[x_1][y_1]+1;
        DFS_1(a, b);
        return 0;
    }
}

你可能感兴趣的:(DFS/BFS,算法)