Research and realization of maze problem

Source: Internet
Author: User
Tags exit continue empty gety

"Problem description"

The maze is represented by a long square of MXN, 0 and 1 representing the pathways and obstacles in the maze respectively. Design a program to find a path from the entrance to the exit, or to conclude that there is no access to the maze set arbitrarily.

(1) The graph of the maze is output according to the two-dimensional array.

(2) Explore the four directions of the maze: right is to starboard, down to left, up, to output from the entrance to the exit of the walking path.

"Algorithmic Analysis"

Main examination data structure-stack. As a data structure, the stack is a special linear table that can only be inserted and deleted at one end. It stores the data according to the advanced principle, the first data is pressed into the bottom of the stack, the final data is on the top of the stack, the data needs to be read from the top of the stack to pop up (the last data is first read out). The stack has memory function, the stack inserts and deletes the operation, does not need to change the stack bottom pointer.

Detailed See Baidu Encyclopedia: Http://baike.baidu.com/subview/38877/12246229.htm?fr=aladdin

"Implementation Analysis"

Retrospective method can be used, that is, from the entrance, along a certain direction of exploration, if you can go through, then continue to move forward, or back along the original road, another direction to continue to explore, until the exit position, to obtain a path. If all possible pathways are explored and fail to reach the exit, the maze set has no access.

"Code Show"

1. First create a point class:

Package com.albertshao.study;  
      
public class Point {  
      
    private int x;  
      
    private int y;  
      
    Public Point () {  
      
    } (  
      
    int x, int y) {  
        this.x = x;  
        This.y = y;  
    }  
      
    @Override public
    String toString () {return  
        "point [x=" + x + ", y=" + y + "]";  
    }  
      
    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;  
    }  
      
}

Maze class:

Package com.albertshao.study;  
Import Java.util.Scanner;  
      
Import Java.util.Stack;  
    public class Maze {int maze[][];  
    private int row = 3;  
    private int col = 3;  
    Stack<point> Stack;  
      
    Boolean p[][] = null;  
        Public Maze () {Maze = new int[15][15];  
        stack = new stack<point> ();  
    p = new BOOLEAN[15][15];   
    }/* More highlights: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/* Construct the maze * *  
        public void init () {Scanner Scanner = new Scanner (system.in);  
        System.out.println ("Please enter the number of lines in the Maze");  
        row = Scanner.nextint ();  
        System.out.println ("Please enter the number of columns in the Maze");  
        col = Scanner.nextint ();  
        System.out.println ("Please enter" + row + "line" + col + "column Maze");  
        int temp = 0; for (int i = 0; i < row; ++i) {for (int j = 0; J < col; ++j) {temp = Scanner.nexti  NT ();
                MAZE[I][J] = temp;  
            P[I][J] = false; 
     }}/* * list, decide whether there is a road.  
        */public void Findpath () {int temp[][] = new Int[row + 2][col + 2];  for (int i = 0; i < row + 2; ++i) {for (int j = 0; J < col + 2; ++j) {Temp[0][j] =  
                1;  
                Temp[row + 1][j] = 1;  
            Temp[i][0] = Temp[i][col + 1] = 1; (int i = 0; i < row + 2; i + +) {for (int j = 0; ; Col + 2;  
                      
            J + +) {System.out.print (Temp[i][j] + "");  
        } System.out.println ();  
        }//Copy the original maze to the new temp. for (int i = 0; i < row; ++i) {for (int j = 0; J < col; ++j) {temp[i + 1][j + 1] = Maze[i][j];  
        } System.out.println ("After copying data");  
                for (int i = 0; i < row + 2; i + +) {for (int j = 0; J < col + 2; j + +) {  
                      
            System.out.print (Temp[i][j] + "");  
        } System.out.println ();  
        }//from Up-left to skip in the clockwise direction int i = 1;  
        int j = 1;  
        P[I][J] = true;  
        Stack.push (new Point (I, j)); while (!stack.empty () && (!) ( i = = (row) && (j = = col))) {if (temp[i][j + 1] = = 0) && (p[i][j + 1] = false)) {  
                Turn right P[i][j + 1] = true;  
                Stack.push (new Point (I, J + 1));  
            j + +;   
   else if ((Temp[i + 1][j] = = 0) && (p[i + 1][j] = false)) {//Turn down P[i + 1][j] = true;             Stack.push (new Point (i + 1, j));  
            i++;  
                else if ((temp[i][j-1] = = 0) && (p[i][j-1] = false) {//Turn left p[i][j-1] = true;  
                Stack.push (new Point (I, j-1));  
            j--;  
                else if ((temp[i-1][j] = = 0) && (p[i-1][j] = false) {//turn up p[i-1][j] = true;  
                Stack.push (new Point (I-1, J));  
            i--;  
                else {stack.pop ();  
                if (Stack.empty ()) {break;  
                } i = Stack.peek (). GetX ();  
            j = Stack.peek (). GetY ();  
        } stack<point> Newpos = new stack<point> ();  
        if (Stack.empty ()) {System.out.println ("no path");  
            else {System.out.println ("There is a path");  
  SYSTEM.OUT.PRINTLN ("path follows:");          while (!stack.empty ()) {Point pos = new Point ();  
                pos = Stack.pop ();  
                System.out.print ("(" +pos.getx () + "," + pos.gety () + ")");  
            Newpos.push (POS); } * * Print the path */String resault[][] = new String[r  
        ow + 1][col + 1]; for (int k = 0; k < row; ++k) {for (int t = 0; t < col; ++t) {resault[k][t] = (Maz  
            E[K][T]) + "";  
            } while (!newpos.empty ()) {Point p1 = Newpos.pop ();  
      
        Resault[p1.getx ()-1][p1.gety ()-1] = "#"; for (int k = 0; k < row; ++k) {for (int t = 0; t < col; ++t) {S  
            Ystem.out.print (Resault[k][t] + "T");  
        } System.out.println (); } public static void Main (String []args) {Maze Maze = new Maze ();  
        Maze.init ();  
    Maze.findpath (); }  
}

Test results:

Please enter the number of rows in the Maze  
3 Please enter the  
number of columns in the Maze  
3  
Please enter 3 rows 3 of the maze  
0   
1 0 0 0 1 1 0 0 1 1 1 1 1 1 0 0 0 1 1 0 1<  C11/>1 0 0 0 1 1 1 1 1 1 after   
copying the data  
1   
1 1 1 1 1 0 1 0 1 1 0 0 1 1 1 1 0 0 1 1 1 1 1   
There are path  
paths as follows:  
(3,3)   
(3,2)   
(2,2)   
(2,1)   
(1,1)   
#   1   0     
#   #   1     
1   # #

Note: Part of the code to draw on users.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.