Java小案例-(逃離迷宮)

來源:互聯網
上載者:User

標籤:column   play   使用者   read   view   input   while   utils   process   

Java小案例-(逃離迷宮)

一,迷宮需求描述:

1,使用者輸入迷宮圖(限制方形):字母1位牆,0為通,e為出口,m為入口,*為已訪問的位置,用外圍1圍住迷宮

2,運行軌跡右,左,下,上

3,判斷該迷宮是否能從入口走到出口,並將搜尋過程輸出

二,迷宮實現:

1,迷宮元素類MazeCell:

package smalldemo.maze; class MazeCell {  public int x,y;  public MazeCell(){  }  public MazeCell(int x,int y){      this.x=x;      this.y=y;  }  public boolean equals(MazeCell cell){     return x==cell.x && y==cell.y;  }}

2,迷宮搜尋類MazeUtils:

package smalldemo.maze;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintStream;import java.util.Stack;class MazeUtils {    private int rows,columns;    private MazeCell currentCell,exitCell=new MazeCell(),entryCell=new MazeCell();    private final char wall=‘1‘,passage=‘0‘,visited=‘*‘,entryMarker=‘m‘,exitMarker=‘e‘;    private char[][] stores;    private Stack<MazeCell> mazePath=new Stack<MazeCell>();    MazeUtils(){        int row=0,column=0;        InputStreamReader in=new InputStreamReader(System.in);        BufferedReader buf=new BufferedReader(in);        Stack<String> mazeRows=new Stack<String>();        System.out.println("Enter a rectangular maze using the following"+        "characters\nm -entry\ne -exit\n1 - wall \n0 - passage\n"+"Enter one 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(int i=0;i<columns+2;i+=1){            stores[0][i]=wall;            stores[rows+1][i]=wall;        }    }    private void display(PrintStream out){        for(int row =0;row <rows +1;row+=1){            out.println(stores[row]);        }        out.println();    }    private void pushUnvisited(int row,int col){        if(stores[row][col] ==passage || stores[row][col] == exitMarker){            mazePath.push(new MazeCell(row,col));        }    }    public void exitMaze(PrintStream out){        currentCell=entryCell;        out.println();        while (!currentCell.equals(exitCell)){            int x=currentCell.x;            int y=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 void main(String[] args){        new MazeUtils().exitMaze(System.out);    }}

3,結果是:

Enter a rectangular maze using the followingcharactersm -entrye -exit1 - wall 0 - passageEnter one line at one time:end with Ctrl- D:1101000e00m1^D1111111110111000e1100m111111111110111000e1100*111111111110111000e110**111111111110111000e11***111111111110111*00e11***111111111110111**0e11***111111111110111***e11***11Success!Process finished with exit code 0
View Code

 

Java小案例-(逃離迷宮)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.