Simple realization of java finding the maze path

Labyrinth project realization design document

Project Introduction:

A grid maze consists of n rows and m columns of cells, and each compound is either a clearing (represented by 0) or an obstacle (represented by 1). Your task is to find a movement sequence from the start point to the end point, in which you can only move up, down, left, and right to adjacent cells. At no time can you be in a cell with obstacles, nor can you go outside the maze. The starting point is the upper left corner and the end point lower right.

Project function:

Solve the problem of maze path search, find a valid path from the entrance of the maze in the upper left corner to the exit of the maze in the lower right corner, 0 means walkable, 1 means unable to walk, please output the final maze and path information if found, please output not exist Effective path.

Knowledge points used in the project:

Implemented using Java object-oriented ideas, two-dimensional arrays and non-recursive stacks

Project realization ideas:

  1. Define a two-dimensional array of maze node type (MazeNode)
  2. Initialize the value in each grid. Store objects for each grid of a two-dimensional array. The value of the object can only be 0 (the current grid can be moved) or 1 (the current grid cannot be moved)
  3. Creating a fence can effectively prevent cross-border problems. According to the value values ​​in the grids in the four directions around the current node, judge whether the current node can go up, down, left, and right (0 means go, 1 cannot go).
  4. Start walking the maze. The stack operation is used to record the walking path, push the element into the stack, determine which direction of the current stack top element can go, and push one of the goable directions into the stack until the element in the lower right corner stops. The path traversed is saved in the stack. Note: If you encounter the problem of going into a dead end, you need to be the top element of the stack at this time and the top element of the stack cannot be walked in the four directions. At this time, pop it out of the stack and choose a new direction to push it again until the element in the lower right corner stops.

Project realization:

Maze class

import java.util.Scanner;

public class Maze {
    private MazeNode[][] mazenode;
    private int row ;//行
    private int colum;//列
    public Maze(){


    }
    public void innode(){//Add maze path;
        Scanner scanner=new Scanner(System.in);
        System.out.println("Please enter the number of rows and columns of the maze");
        row=scanner.nextInt()+2;//Add a wall to the back
        colum=scanner.nextInt()+2;
        System.out.println("Please enter the path of the maze:");
        mazenode=new MazeNode[row][colum];
        build(mazenode);//Create a mazenode in the row row and column column and give the value 1
        for(int i=1;i<row-1;i++){//Assign the value inside the wall;
            for(int j=1;j<colum-1;j++){
            mazenode[i][j].value=scanner.nextInt();

    }
        }
    }
//Create a fence;
    public void build(MazeNode[][] mazenode){
        for(int i=0;i<row;i++){
            for(int j=0;j<colum;j++){
                mazenode[i][j]=new MazeNode(1,i,j);
            }
        }
    }
    public void goMaze(){//Walk the maze
        innode ();
        MyStack route=new MyStack();//Storage path
        if(mazenode[1][1].value==0){//Determine whether the entrance is accessible;
        route.push(mazenode[1][1]);
        }else {System.out.println("The path to the maze entrance is wrong");
        }
        if(mazenode[row-2][colum-2].value!=0){//Judge whether the end point is correct
            System.out.println("The end of the maze is wrong");
        }
        for(int i=1,j=1;;){//起点
            i=route.gettop().index1;//Assign the subscript of the top element of the stack to i
            j=route.gettop().index2;//Assign the subscript two of the top element of the stack to j
            if(i==row-2&&j==colum-2){
                break;
            }//Exit the loop at the end
                if(mazenode[i][j].right(mazenode,i,j+1)){//Determine whether the right side can go
                    if(route.contain(route,mazenode,i,j+1)){//Judge whether the right side is in the stack
                        mazenode[i][j].changeValue(mazenode,i,j);
                        route.pop(mazenode[i][j]);//If there is a pop-up
                    }else {
                    route.push(mazenode[i][j+1]);//If there is no right side of the stack
                    }
                } else if(i+1<row&&mazenode[i][j].down(mazenode,i+1,j)){
                    if(route.contain(route,mazenode,i+1,j)){//Judge whether the bottom is in the stack
                        mazenode[i][j].changeValue(mazenode,i,j);
                        route.pop(mazenode[i+1][j]); Exit the stack
                    }else {
                    route.push(mazenode[i+1][j]);
                    }
                }else if(i+1<row&&mazenode[i][j].left(mazenode,i,j-1)){
                    if(route.contain(route,mazenode,i,j-1)){//Judging whether the left side is in the stack
                        mazenode[i][j].changeValue(mazenode,i,j);
                        route.pop(mazenode[i][j]); exit the stack
                    }else{
                    route.push(mazenode[i][j-1]);
                    }
                }else if(i+1<row&&mazenode[i][j].up(mazenode,i-1,j)){
                    if(route.contain(route,mazenode,i-1,j)){//Judging whether the above is in the stack
                        mazenode[i][j].changeValue(mazenode,i,j);
                        route.pop(mazenode[i][j]); exit the stack
                    }else{
                    route.push(mazenode[i-1][j]);
                    }
                }
        }
        route.toRoute();//Modify the value of the route
        for(int i=1;i<row-1;i++){//print
            for(int j=1;j<colum-1;j++){
                System.out.print(mazenode[i][j].value+"  ");
            }
            System.out.println();
        }
    }
}

mazenode type

public class MazeNode {
    public int index1;
    public int index2;
    public int value;
    public MazeNode(int value,int index1,int index2) {
        this.value=value;
        this.index1=index1;//下标1
        this.index2=index2;//下标2
    }
    //Change the value of the point to 2
   public void changeValue(MazeNode[][] mazeNode,int index1,int index2){

        mazeNode[index1][index2].value=2;
   }
   //Determine whether the left side can go
    public boolean left(MazeNode[][] mazeNode,int index1,int index2){
       if(mazeNode[index1][index2].value==0){
        return true;
       }return false;
    }
    //Determine whether the upper side can go
    public boolean up(MazeNode[][] mazeNode,int index1,int index2){
        if(mazeNode[index1][index2].value==0){
            return true;
        }return false;
    }
    //Determine whether the right side can go
    public boolean right(MazeNode[][] mazeNode,int index1,int index2){
        if(mazeNode[index1][index2].value==0){
            return true;
        }return false;
    }
    //Determine whether the bottom can go
    public boolean down(MazeNode[][] mazeNode,int index1,int index2){
        if(mazeNode[index1][index2].value==0){
            return true;
        }return false;
    }
}

MyStake class // stack

import java.util.Arrays;
import java.util.EmptyStackException;

public class MyStack {
    private PuzzleValue[]array2;
    private MazeNode[]array;
    private int size;
    private final int INITSIZE=10;
    public MyStack () {
        array=new MazeNode[INITSIZE];
        array2=new PuzzleValue[INITSIZE];

    }
    //Find if this path exists in the stack
    public  boolean contain(MyStack stack,MazeNode[][] mazeNode,int index1,int index2){

        for(int i=0;i<size;i++){
            if(array[i].index1==index1&&array[i].index2==index2){
                return true;
            }
            }
        return false;
    }
    //Into the stack
    public void  push(MazeNode mazeNode){
        if(array.length==size){
           array= Arrays.copyOf(array,array.length+(array.length>>1));
        }else {
        array[size]=mazeNode;
        size++;
        }
    }
    //Pull
    public void pop(MazeNode mazeNode){
        if(size==0){
            return;
        }else{
            array[size]=null;
            size--;
        }

    }
    //Get the top element of the stack
    public MazeNode gettop(){
        return array[size-1];
    }
    //Change the value value in the stack
    public void toRoute(){
        for(int i=0;i<size;i++){
            array[i].value=3;
        }
    }
    }

Guess you like

Origin blog.csdn.net/tdongmu/article/details/109275211