Java8与迷宫回溯问题

Wesley13
• 阅读 544

文章目录

引入

  实现功能:
  1)setStart():设置起点;
  2)setEnd():设置终点;
  3)setWall():设置墙壁;
  4)findWay():寻找路径;
  5)display():展示地图。

  说明:
  1)使用二维数组表示地图,边界默认为墙壁,无法更改,用1表示;
  2)9表示起点,6表示终点;
  3)2表示路径;
  4)3表示不通;
  5)找寻顺序为↓→↑←
  初始地图示例如下:

1 0 0 0 0 0 0 1
1 9 0 0 0 0 0 1
1 1 1 0 0 0 0 1
1 0 0 0 0 0 0 1
1 0 0 0 0 0 0 1
1 0 0 1 0 6 0 1
1 1 1 1 1 1 1 1

  路径找寻示例如下:

1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 1
1 9 2 2 0 0 0 1
1 1 1 2 0 0 0 1
1 0 0 2 0 0 0 1
1 0 0 2 2 0 0 1
1 0 0 1 2 6 0 1
1 1 1 1 1 1 1 1

代码

package Test;

import java.util.Arrays;
import java.util.InputMismatchException;

/**
 * @author: Inki
 * @email: inki.yinji@qq.com
 * @create: 2020 1111
 * @last_modify: 2020 1112
 */
public class MazeBacktracking {
   
   
   

    /**
     * The default number rows.
     */
    private final int NUMBER_ROW = 8;

    /**
     * The default number columns.
     */
    private int NUMBER_COLUMN = 8;

    /**
     * Number rows.
     */
    private int numRow;

    /**
     * Number columns.
     */
    private int numColumn;

    /**
     * The maze matrix.
     */
    private int[][] maze;

    /**
     * The start point index.
     */
    private int[] start;

    /**
     * The end point index.
     */
    private int[] end;

    /**
     * The first constructor.
     */
    public MazeBacktracking() {
   
   
   
        initialize(NUMBER_ROW, NUMBER_COLUMN);
    }//Of first constructor

    /**
     * The first constructor.
     */
    public MazeBacktracking(int paraNumRow, int paraNumColumn) {
   
   
   
        initialize(paraNumRow, paraNumColumn);
    }//Of first constructor

    /**
     * Initialize.
     *
     * @param: paraNumRow: The number of rows.
     * paraNumColumn: The number of columns.
     */
    private void initialize(int paraNumRow, int paraNumColumn) {
   
   
   
        numRow = paraNumRow;
        numColumn = paraNumColumn;
        maze = new int[numRow][numColumn];

        // Initialize walls.
        Arrays.fill(maze[0], 1);
        Arrays.fill(maze[numColumn - 1], 1);
        for (int i = 1; i < numRow - 1; i++) {
   
   
   
            maze[i][0] = maze[i][numColumn - 1] = 1;
        }//Of for i

        // Set start point.
        start = new int[2];
        start[0] = 1;
        start[1] = 1;

        // Set end point.
        end = new int[2];
        end[0] = numRow - 2;
        end[1] = numColumn - 2;
    }//Of initialize

    /**
     * Set start point.
     *
     * @param: paraStart:
     * The start point index.
     */
    public void setStart(int[] paraStart) {
   
   
   

        if (paraStart.length != 2) {
   
   
   
            throw new InputMismatchException("The columns of paraSite must equal 2.");
        }//Of if

        start = paraStart;

    }//Of start

    /**
     * Set end point.
     *
     * @param: paraStart:
     * The end point index.
     */
    public void setEnd(int[] paraEnd) {
   
   
   

        if (paraEnd.length != 2) {
   
   
   
            throw new InputMismatchException("The columns of paraSite must equal 2.");
        }//Of if

        end = paraEnd;

    }//Of start

    /**
     * Set wall.
     *
     * @param: paraSite:
     * The site index of wall.
     */
    public void setWall(int[] paraSite) {
   
   
   
        int[][] tempSite = new int[1][];
        tempSite[0] = paraSite;
        setWall(tempSite);
    }//Of setWall

    /**
     * Set walls.
     *
     * @param: paraSite:
     * The site index of walls.
     */
    public void setWall(int[][] paraSite) {
   
   
   

        if (paraSite[0].length != 2) {
   
   
   
            throw new InputMismatchException("The columns of paraSite must equal 2.");
        }//Of if

        for (int[] idx : paraSite) {
   
   
   
            maze[idx[0]][idx[1]] = 1;
        }//Of for i

    }//Of setWall

    /**
     * Find ways.
     */
    public boolean findWay() {
   
   
   
        return findWay(start[0], start[0]);
    }//Of findWay

    /**
     * Find ways.
     *
     * @param: paraI: The current index of row.
     * paraJ: The current index of column.
     */
    public boolean findWay(int pareI, int paraJ) {
   
   
   
        if (maze[end[0]][end[1]] == 2) {
   
   
   
            return true;
        } else {
   
   
   
            if (maze[pareI][paraJ] == 0) {
   
   
   
                // Down --> right --> up --> left.
                maze[pareI][paraJ] = 2;
                if (findWay(pareI + 1, paraJ)) {
   
   
   
                    return true;
                } else if (findWay(pareI, paraJ + 1)) {
   
   
   
                    return true;
                } else if (findWay(pareI - 1, paraJ)) {
   
   
   
                    return true;
                } else if (findWay(pareI, paraJ - 1)) {
   
   
   
                    return true;
                } else {
   
   
   
                    maze[pareI][paraJ] = 3;
                    return false;
                }//Of if
            } else {
   
   
   
                return false;
            }//Of if
        }//Of if

    }//Of findWay

    /**
     * Display maze.
     */
    public void display() {
   
   
   

        for (int i = 0; i < numRow; i++) {
   
   
   
            for (int j = 0; j < numColumn; j++) {
   
   
   
                if (i == start[0] && j == start[1]) {
   
   
   
                    System.out.print("9 ");
                } else if (i == end[0] && j == end[1]) {
   
   
   
                    System.out.print("6 ");
                } else {
   
   
   
                    System.out.print(maze[i][j] + " ");
                }//Of if
            }//of for j
            System.out.println();
        }//Of for i
        System.out.println();

    }//Of display

    /**
     * The main.
     */
    public static void main(String[] args) {
   
   
   
        MazeBacktracking test = new MazeBacktracking();
        int[][] tempSite = {
   
   
   {
   
   
   3, 1}, {
   
   
   3, 2}, {
   
   
   6, 3}};
        int[] tempStart = {
   
   
   2, 1};
        int[] tempEnd = {
   
   
   6, 5};
        test.setStart(tempStart);
        test.setEnd(tempEnd);
        test.setWall(tempSite);
        test.display();
        test.findWay();
        test.display();
    }//Of main

}//Of class MazeBacktracking

本文分享 CSDN - 因吉。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。

点赞
收藏
评论区
推荐文章
待兔 待兔
4个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Wesley13 Wesley13
3年前
java类封装成dll
@参考文章1(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fblog.csdn.net%2Fazure_sky_2014%2Farticle%2Fdetails%2F71915605),@参考文章2(https://www.oschina.net/action/GoToLink?u
Wesley13 Wesley13
3年前
java 开发环境搭建
文章目录java开发环境搭建(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fblog.csdn.net%2Fliouwb%2Farticle%2Fdetails%2F109048255%23java_1)安装jdk(https:
Stella981 Stella981
3年前
Django REST framework学习笔记
文章目录1\.API接口开发(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fblog.csdn.net%2FThanlon%2Farticle%2Fdetails%2F106855676%231_API_2)
Wesley13 Wesley13
3年前
FPGA逻辑设计回顾(2)那些年学习FPGA较为常见的疑问?
文章目录前言(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Freborn.blog.csdn.net%2Farticle%2Fdetails%2F111129860%23_2)如何理解FPGA中的帧、字与比特?(http
Stella981 Stella981
3年前
Spring Boot中的测试
文章目录简介(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fblog.csdn.net%2Fsuperfjj%2Farticle%2Fdetails%2F104206183%23_3)添加maven依赖(https://www.oschina.net/
Stella981 Stella981
3年前
Kafka、RabbitMQ、RocketMQ等 消息中间件 介绍和对比
文章目录1、前言(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fblog.csdn.net%2Fu014597198%2Farticle%2Fdetails%2F100563722%231_1)2、概念(https://www.oschina.net/
Wesley13 Wesley13
3年前
VMware安装教程
文章目录安装VMware(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fblog.csdn.net%2Fweixin_45135767%2Farticle%2Fdetails%2F108745522%3Fbiz_id%3D102%26utm_term%3Dvmwar
Stella981 Stella981
3年前
Spring Boot注解
文章目录简介(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fblog.csdn.net%2Fsuperfjj%2Farticle%2Fdetails%2F104112926%23_3)@SpringBootApplication(https://www
Stella981 Stella981
3年前
Dubbo系列之微服务框架整合教程
文章目录一、分布式基本知识(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fsmilenicky.blog.csdn.net%2Farticle%2Fdetails%2F84455282%23_2)1.1)架构演变(https: