Java小案例-(逃离迷宫)

Posted SparkDr

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java小案例-(逃离迷宫)相关的知识,希望对你有一定的参考价值。

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 followingcharacters
m -entry
e -exit
1 - wall 
0 - passage
Enter one line at one time:end with Ctrl- D:
1101
000e
00m1
^D

111111
111011
1000e1
100m11

111111
111011
1000e1
100*11

111111
111011
1000e1
10**11

111111
111011
1000e1
1***11

111111
111011
1*00e1
1***11

111111
111011
1**0e1
1***11

111111
111011
1***e1
1***11

Success!

Process finished with exit code 0
View Code

 

以上是关于Java小案例-(逃离迷宫)的主要内容,如果未能解决你的问题,请参考以下文章

逃离迷宫(HDU 1728 BFS)

Hdu 1728 逃离迷宫

HDU1728-逃离迷宫-BFS

hdu 1728 逃离迷宫 bfs记步数

hdu 1728 逃离迷宫 bfs记转向

HDU 1728 逃离迷宫(DFS经典题,比赛手残写废题)