java迷宫问题


题目描述

定义一个二维数组N*M(其中2<=N<=10;2<=M<=10),如5 × 5数组下所示:

int maze [5] [5] = {
0, 1, 0, 0, 0,
0, 1, 0, 1, 0,
0, 0, 0, 0, 0,
0, 1, 1, 1, 0,
0, 0, 0, 1, 0,
};

它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。入口点为[0,0],既第一空格是可以走的路。

本题含有多组数据。

eg:

输入:
5 5
0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0

输出:
(0,0)
(1,0)
(2,0)
(2,1)
(2,2)
(2,3)
(2,4)
(3,4)
(4,4)


以下是本篇文章正文内容,下面案例可供参考

解题思路

遇0可走,遇1不可走

每一个位置有四个方向,可走方向的情况:

  1. 没有越界
  2. 不为1
  3. 没有走过

当满足条件时,将当前位置保存到路径中,继续搜素四个方向,若为右下角出口位置,表示一个新的路径产生,比较路径大小,若更短则更新

当不满足条件时,把当前位置从路径中删除,寻找新的路径

代码如下

import java.util.ArrayList;
import java.util.Scanner;

class Node {
    int x;
    int y;

    public Node(int x, int y) {
        this.x = x;
        this.y = y;
    }
}
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()){
            int row = sc.nextInt();
            int col = sc.nextInt();
            //矩阵
            int[][] mat = new int[row][col];
            for (int i = 0; i < row; i++) {
                for (int j = 0; j < col; j++) {
                    mat[i][j] = sc.nextInt();
                }
            }
            //搜索最短路径
            ArrayList<Node> path = new ArrayList<>();
            ArrayList<Node> minPath = new ArrayList<>();
            int[][] book = new int[row][col];
            
            getMinPath(mat,row,col,0,0,book,path,minPath);
            //打印
            for (Node n:minPath
                 ) {
                System.out.println("("+n.x+","+n.y+")");
            }
        }
    }

    /**
     * 迷宫问题
     * @param mat 迷宫矩阵
     * @param row 迷宫矩阵的行
     * @param col 迷宫矩阵的列
     * @param x 当前位置横坐标
     * @param y 当前位置纵坐标
     * @param book 标记矩阵,当前位置是否走过
     * @param path 保存当前路径的每个位置
     * @param minPath 保存最短路径
     */
    public static void getMinPath(int[][] mat, int row, int col, int x, int y, int[][] book, ArrayList<Node> path, ArrayList<Node> minPath) {
        //判断x,y是否越界,是否走过,是否为1
        if(x<0||x>=row||y<0||y>=col||book[x][y]==1||mat[x][y]==1){
            return;
        }
        //把当前位置存入路径中
        path.add(new Node(x,y));
        //走过了,标记当前位置
        book[x][y] = 1;
        //判断当前位置是否为出口位置
        if(x == row-1&&y==col-1){
            //一条新的路径产生
            //判断是否为更短路径
            if (minPath.isEmpty() || path.size()<minPath.size()){
                //更新更短路径
                minPath.clear();
                for (Node n:path
                     ) {
                    minPath.add(n);
                }
            }
        }
        //继续搜索(x,y)的上下左右四个方向
        getMinPath(mat, row, col, x+1, y, book, path, minPath);
        getMinPath(mat, row, col, x-1, y, book, path, minPath);
        getMinPath(mat, row, col, x, y+1, book, path, minPath);
        getMinPath(mat, row, col, x, y-1, book, path, minPath);

        //走到这,这说明走不通了
        //把当前位置删除,寻找新的路径
        path.remove(path.size()-1);
        book[x][y]=0;
    }
}

相似题目

走方格的方案数

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值