Java面向对象 -- 迷宫

题目:如图所示是一迷宫,“0”表示路,“1”表示墙,只有路可以走,入口为左上角,出口为右下角,请用面向对象的思想找出迷宫的路。
在这里插入图片描述
包结构
在这里插入图片描述
Constant 类

package com.tulun.constant;

public class Constant {
    public static final int UP = 0;//上
    public static final int DOWN = 1;//下
    public static final int LEFT = 2;//左
    public static final int RIGHT = 3;//右

    public static final boolean GOABLE = true;//可以走
    public static final boolean NotGOABLE = false;//不可以走

}

Maze 类

package com.tulun.dao;

import com.tulun.constant.Constant;
import com.tulun.util.MySqStackUtil;

/**
 * 迷宫类
 */
public class Maze {
    //行数
    private int row;
    //列数
    private int col;
    //结点类型的二维数组
    private MazeNode[][] mazeNodes;
    //栈对象
    MySqStackUtil mySqStackUtil;
    //初始化行、列、结点数组
    public Maze(int row,int col){
        this.row = row;
        this.col = col;
        mazeNodes = new MazeNode[row][col];
    }

    public void setMazeNodes(int x,int y,int val){

        mazeNodes[x][y] = new MazeNode(x,y,val);
    }

    /**
     * 现在二维数组里面,已经存放了迷宫的路径,开始记录每个节点
     * 四周的可走状态
     */
    public void judgePath() {
        mySqStackUtil = new MySqStackUtil();
        for(int i = 0;i < mazeNodes.length;i++){
            for(int j = 0;j < mazeNodes[i].length;j++){
                if(mazeNodes[i][j].getVal() == 0){
                    //上
                    if(i-1 >= 0 && mazeNodes[i-1][j].getVal() == 0){
                        mazeNodes[i][j].setPathState(Constant.UP,Constant.GOABLE);
                    }
                    //下
                    if(i+1 < row && mazeNodes[i+1][j].getVal() == 0){
                        mazeNodes[i][j].setPathState(Constant.DOWN,Constant.GOABLE);
                    }
                    //左
                    if(j-1 >= 0 && mazeNodes[i][j-1].getVal() == 0){
                        mazeNodes[i][j].setPathState(Constant.LEFT,Constant.GOABLE);
                    }
                    //右
                    if(j+1 < col && mazeNodes[i][j+1].getVal() == 0){
                        mazeNodes[i][j].setPathState(Constant.RIGHT,Constant.GOABLE);
                    }
                }
            }
        }
    }

    /**
     * 开始走迷宫,如果可以走
     * 把最后路径放到栈当中
     */
    public void goMaze() {
        //将入口压栈
        if(mazeNodes[0][0].getVal() == 0){
            mySqStackUtil.push(mazeNodes[0][0]);
        }
        //栈不为空时
        while(!mySqStackUtil.isEmpty()){
            //获得栈顶元素
            MazeNode gTop = mySqStackUtil.getTop();
            //栈顶元素若为出口,则跳出 while()
            if(gTop.getX() == row-1 && gTop.getY() == col-1){
                break;
            }
            //右边
            if(gTop.getPathState(Constant.RIGHT) == Constant.GOABLE){
                //将右边节点 压栈
                mySqStackUtil.push(mazeNodes[gTop.getX()][gTop.getY()+1]);
                mazeNodes[gTop.getX()][gTop.getY()].setPathState(Constant.RIGHT,Constant.NotGOABLE);
                mazeNodes[gTop.getX()][gTop.getY()+1].setPathState(Constant.LEFT,Constant.NotGOABLE);
                //continue在java中是跳出本次循环,继续执行下一次循环
                continue;
            }
            //左边
            if(gTop.getPathState(Constant.LEFT) == Constant.GOABLE ){
                mySqStackUtil.push(mazeNodes[gTop.getX()][gTop.getY()-1]);
                mazeNodes[gTop.getX()][gTop.getY()].setPathState(Constant.LEFT,Constant.NotGOABLE);
                mazeNodes[gTop.getX()][gTop.getY()-1].setPathState(Constant.RIGHT,Constant.NotGOABLE);
                continue;
            }
            //上边
            if(gTop.getPathState(Constant.UP) == Constant.GOABLE ){
                mySqStackUtil.push(mazeNodes[gTop.getX()-1][gTop.getY()]);
                mazeNodes[gTop.getX()][gTop.getY()].setPathState(Constant.UP,Constant.NotGOABLE);
                mazeNodes[gTop.getX()-1][gTop.getY()].setPathState(Constant.DOWN,Constant.NotGOABLE);
                continue;
            }
            //下边
            if(gTop.getPathState(Constant.DOWN) == Constant.GOABLE ){
                mySqStackUtil.push(mazeNodes[gTop.getX()+1][gTop.getY()]);
                mazeNodes[gTop.getX()][gTop.getY()].setPathState(Constant.DOWN,Constant.NotGOABLE);
                mazeNodes[gTop.getX()+1][gTop.getY()].setPathState(Constant.UP,Constant.NotGOABLE);
                continue;
            }
            //若该节点走到了死胡同,就出栈
            mySqStackUtil.pop();
        }
    }

    /**
     * 打印迷宫
     */
    public void showPath() {

        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                System.out.print(mazeNodes[i][j].getVal()+ " ");
            }
            System.out.println();
        }
    }
    //打印路径
    public void show() {
        if (mySqStackUtil.isEmpty()) {
            System.out.println("此迷宫无路径~");
            return;
        }
        while (!mySqStackUtil.isEmpty()) {
            MazeNode m = mySqStackUtil.getTop();
            m.setVal(8);
            mySqStackUtil.pop();
        }
        System.out.println("迷宫路径为:");

        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                System.out.print(mazeNodes[i][j].getVal() + " ");
            }
            System.out.println();
        }
    }

}

MazeNode 类

package com.tulun.dao;

import com.tulun.constant.Constant;

/**
 * 节点类
 */
public class MazeNode {
    // x坐标(行所在位置)
    private int x;
    // y坐标(列所在位置)
    private int y;
    //结点内的值,是 0 还是 1
    private int val;
    //结点的状态数组,能走还是不能走
    private boolean[] hashTable;
    //变量初始化
    public MazeNode(int x,int y,int val){
        this.x = x;
        this.y = y;
        this.val = val;
        this.hashTable = new boolean[4];
        for (int i = 0;i < hashTable.length;i++){
            hashTable[i] = Constant.NotGOABLE;
        }
    }

    public int getX() { return x; }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getVal() {
        return val;
    }

    public void setVal(int val) {
        this.val = val;
    }

    /**
     * 需要写一个结点能否走的状态方法
     * 设置状态
     * @param n
     * @param m
     */
    public void setPathState(int n,boolean m){
        hashTable[n] = m;
    }

    public boolean getPathState(int n){
        return hashTable[n];
    }
}

Main 类

package com.tulun.main;


import com.tulun.dao.Maze;

import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        System.out.println("========欢迎来到迷宫世界==========");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入迷宫的行数:");
        int row = scanner.nextInt();
        System.out.println("请输入迷宫的列数:");
        int col = scanner.nextInt();
        Maze maze = new Maze(row,col);
        //开始给二维数组赋值
        int value = 0;
        System.out.println("=========请输入迷宫的路径===========");
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                value = scanner.nextInt();
                maze.setMazeNodes(i,j,value);
            }
        }
        maze.showPath();
        maze.judgePath();
        maze.goMaze();
        maze.show();
    }
}

MySqStackUtil 类

package com.tulun.util;

import com.tulun.dao.MazeNode;

public class MySqStackUtil {
    //top 栈顶标识
    private int top;
    //数组实现栈
    private MazeNode[] elem;
    //栈对象
    MySqStackUtil mySqStackUtil;
    //初始化栈大小为10
    public MySqStackUtil() {
        this(10);
    }
    //初始化栈类型为 MazeNode 类型
    public MySqStackUtil(int size) {
        this.elem = new MazeNode[size];
    }

    /**
     * 判断是否为满
     * @return
     */
    public boolean isFull() {
        if(this.top == this.elem.length) {
            return true;
        }
        return false;
    }

    /**
     * 压栈,压结点类型
     * @param mazeNode
     */
    public void push(MazeNode mazeNode) {
        if(isFull()) {
            return;
        }
        this.elem[top++] = mazeNode;
    }

    /**
     * 判断是否为空
     * @return
     */
    public boolean isEmpty() {
        return this.top == 0;
    }

    /**
     * 出栈
     */
    public void pop() {
        if(isEmpty()) {
            return;
        }
        this.top--;
    }

    /**
     * 返回栈顶元素
     * @return
     */
    public MazeNode getTop(){
        return elem[this.top-1];
    }
}

运行示例 <1> :
在这里插入图片描述
运行示例 <2> :
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/xyxy66/article/details/84065752
0条评论
添加一条新回复
  
今日推荐