Maze problem (Java implementation)

Source: Internet
Author: User
Tags gety

1.

 Public classDirection {Private intx; Private inty;  PublicDirection (intXinty) { This. x =x;  This. y =y; }     Public intGetX () {returnx; }     Public voidSetX (intx) { This. x =x; }     Public intGetY () {returny; }     Public voidSety (inty) { This. y =y; }}

2.

 Public classPosition {Private intx; Private inty; Private intD;  Public intGetX () {return  This. x; }     Public voidSetX (intx) { This. x =x; }     Public intGetY () {return  This. Y; }     Public voidSety (inty) { This. y =y; }     PublicPosition (intXintYintd) { This. x =x;  This. y =y;  This, db=D; }     Public intgetd () {return  This. D; }     Public voidSETD (intd) { This, db=D; }}

3.

ImportJava.util.Iterator;ImportJava.util.Stack; Public classMazeproblem { Public StaticStack<position> Path (int[] [] maze, direction[] move) {Stack<Position> s =NewStack<position>(); //The starting position has not been explored so there is no directionPosition start_p =NewPosition (1, 1, 1);        S.push (start_p); maze[1][1] =-1;//The starting position indicates that you have passed, you can not go back to explore, pop can be returned         while(!S.empty ()) {Position temp= S.pop ();//take out the current position and prepare for a downward exploration//determine the location direction of the exploration            intx = Temp.getx ();//the location and direction you are currently exploring            inty =temp.gety (); intD = temp.getd () + 1;//the direction of exploration             while(D < 8) {//start exploring a total of eight directions                inti = x + Move[d].getx ();//the next position, based on a certain direction.                intj = y +move[d].gety (); if(Maze[i][j] = = 0) {//if the next position is available, put it in the current positionS.push (NewPosition (x, y, D)); X= i;//adjust the current exploration position to the placed positiony =J; D= 0;//adjust the direction to 0 to prepare for the next explorationMaze[x][y] =-1;//and set it to the past                    if(x = = destination.m && y = = DESTINATION.N) {//The program exits if it is not already the end positionS.push (NewPosition (x, y, D)); returns; }                } Else{d++;////if the next position is not allowed to go in, then put the adjustment direction                }            }        }        return NewStack<position>(); }    //End Position    Static classDestination {Static intm = 6; Static intn = 8; }     Public Static voidMain (string[] arg) {//0 means that you can enter 1 to indicate that you can't go in-1 means you can't go through the road.//each location has eight directions        int[] Maze = {            //0 1 2 3 4 5 6 7 8 9{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},//0{1, 0, 1, 1, 1, 0, 1, 1, 1, 1},//1{1, 1, 0, 1, 0, 1, 1, 1, 1, 1},//2{1, 0, 1, 0, 0, 0, 0, 0, 0, 1},//3{1, 0, 1, 1, 1, 1, 1, 0, 1, 1},//4{1, 1, 0, 0, 1, 1, 1, 1, 0, 1},//5{1, 0, 1, 1, 0, 0, 0, 0, 0, 1},//6{1, 1, 1, 1, 1, 1, 1, 1, 1, 1}//7        }; Direction[] Move=         {            NewDirection (0, 1),            NewDirection (1, 1),             NewDirection (1, 0),            NewDirection (1,-1),            NewDirection (0,-1),            NewDirection ( -1,-1),             NewDirection (-1, 0),            NewDirection (-1, 1)        }; Stack<Position> s =Mazeproblem.path (Maze, move); Iterator<Position> it =S.iterator ();  while(It.hasnext ()) {Position e=It.next (); System.out.println (E.getx ()+ ",-->" +e.gety ()); }    }}

Maze problem (Java implementation)

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.