Java Small case-(escape maze)

Source: Internet
Author: User

Java Small case-(escape maze)

One, maze requirements Description:

1, user input maze map (limit square): letter 1-bit wall, 0 for pass, E for exit, M for entrance, * for visited position, with peripheral 1 surround Maze

2, running trajectory right, left, bottom, top

3, determine whether the maze can walk from the entrance to the exit, and the search process output

Second, the Maze realization:

1, Labyrinth element class Mazecell:

 PackageSmalldemo.maze;classMazecell { Public intx, y;  PublicMazecell () {} PublicMazecell (intXinty) {       This. x=x;  This. y=y; }   Public Booleanequals (Mazecell cell) {returnX==cell.x && y==cell.y; }}

2, Maze search class Mazeutils:

 PackageSmalldemo.maze;ImportJava.io.BufferedReader;Importjava.io.IOException;ImportJava.io.InputStreamReader;ImportJava.io.PrintStream;ImportJava.util.Stack;classMazeutils {Private intRows,columns; PrivateMazecell currentcell,exitcell=NewMazecell (), entrycell=NewMazecell (); Private Final CharWall= ' 1 ', passage= ' 0 ', visited= ' * ', entrymarker= ' m ', exitmarker= ' e '; Private Char[] stores; PrivateStack<mazecell> mazepath=NewStack<mazecell>(); Mazeutils () {intRow=0,column=0; InputStreamReader in=NewInputStreamReader (system.in); BufferedReader buf=NewBufferedReader (in); Stack<String> mazerows=NewStack<string>(); System.out.println ("Enter a rectangular maze using the following" + "Characters\nm-entry\ne-exit\n1-wall \n0-passage\n" + "Enter on E line at one time: "+" End With ctrl-d: "); Try{String str=Buf.readline ();  while(str! =NULL) {row+ = 1; Columns=str.length (); STR= "1" + str + "1";                Mazerows.push (str); if(Str.indexof (entrymarker)! =-1) {entrycell.x=Row; Entrycell.y=Str.indexof (Entrymarker); }                if(Str.indexof (exitmarker)! =-1) {exitcell.x=Row; Exitcell.y=Str.indexof (Exitmarker); } STR=Buf.readline (); }        } Catch(IOException e) {e.printstacktrace (); } Rows=Row; Stores=New Char[Rows+2][]; stores[0]=New Char[Columns+2];  for(;! Mazerows.empty (); row--) {Stores[row]=Mazerows.pop (). ToCharArray (); } stores[rows+1]=New Char[Columns+2];  for(intI=0;i<columns+2;i+=1) {stores[0][i]=Wall; Stores[rows+1][i]=Wall; }    }    Private voiddisplay (PrintStream out) { for(intRow =0;row <rows +1;row+=1) {out.println (Stores[row]);    } out.println (); }    Private voidPushunvisited (intRowintCol) {        if(Stores[row][col] ==passage | | stores[row][col] = =Exitmarker) {Mazepath.push (NewMazecell (Row,col)); }    }     Public voidExitmaze (PrintStream out) {CurrentCell=Entrycell;        Out.println ();  while(!currentcell.equals (Exitcell)) {            intx=currentcell.x; inty=currentcell.y;            Display (System.out); Stores[x][y]=visited; pushunvisited (x-1, y); pushunvisited (x+1, y); pushunvisited (x, y-1); pushunvisited (x, y+1); if(Mazepath.isempty ()) {display (out); Out.println ("Failure"); return; }            Else{CurrentCell=Mazepath.pop ();        }} display (out); Out.println ("Success!"); }     Public Static voidMain (string[] args) {Newmazeutils (). Exitmaze (System.out); }}

3, the result is:

 enter a rectangular maze using the FOLLOWINGCHARACTERSM -entrye -exit  1- wall  0-< Span style= "COLOR: #000000" > Passageenter one line in one time:end with Ctrl - D:1101000e00m1  ^d  1111111110111000e1100m11  1111111110111000e1  100*11111111111011< Span style= "COLOR: #000000" >1000e1  10**111111111110111000e1  1*** 111111111110111*00e1  1***111111111110111**0e1  1***111111111110111***e1  1***11success ! process finished with exit code  0 
View Code

Java Small case-(escape maze)

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.