递归寻求迷宫出路

迷宫文件boya.ice:

8
9
#########
#s0##0###
#0##00###
#0##0####
#0000####
#0##0####
#0##00e##
#0000####

 

package maze;
import java.io.*;
import java.util.*;
public class Maze{
 private char[][] maze;//迷宫数组
 private int startX,startY,endX,endY;//迷宫起点,终点的位置
 private int x,y,step=0;//迷宫长宽及步骤
 //依据输入的文件名创建对象
 private Maze(String fileName){
  try{
   LinkedList aList=new LinkedList();//用于存储文件每行的内容
   BufferedReader files=new BufferedReader(new FileReader("map//"+fileName));
   //将每行的内容依次加入到LinkedList中
   String temp=new String();
   int i=0;
   while((temp=files.readLine())!=null){
    aList.add(temp);
   }
   files.close();
   //读取并设置迷宫的长宽
   x=Integer.parseInt((String)aList.getFirst())+2;
   aList.removeFirst();
   y=Integer.parseInt((String)aList.getFirst())+2;
   aList.removeFirst();
   //依据长宽对迷宫进行初始化
   maze=new char[x][y];
   //将迷宫的赋予外围墙
   for(i=0;i<x;i++){
    maze[i][0]='#';
    maze[i][y-1]='#';
   }
   for(i=0;i<y;i++){
    maze[0][i]='#';
    maze[x-1][i]='#';
   }
   //将LinkedList中内容读入数组
   Iterator it=aList.iterator();
   i=1;
   char[] row;
   while(it.hasNext()){
    temp=((String)it.next());
    row=new char[y-2];
    row=temp.toCharArray();
    for(int j=1;j<y-1;j++){
     maze[i][j]=row[j-1];
     if(maze[i][j]=='s'){
      startX=i;
      startY=j;
      maze[i][j]='0';
     }
     else if(maze[i][j]=='e'){
      endX=i;
      endY=j;
      maze[i][j]='0';
     }
    }
    i++;
   }
  }
  catch(FileNotFoundException e){
   System.out.println("File Name Input Wrong!!!");
  }
  catch(IOException e){
   System.out.println("Wrong Input!!!");
  }
 }
 //递归方法寻找路径
 private boolean findWay(int x,int y){
  if(maze[endX][endY]=='i')
   return true;
  else
   if(maze[x][y]=='0'){
    maze[x][y]='i';
    if(findWay(x-1,y))
     return true;
    else if(findWay(x+1,y))
     return true;
    else if(findWay(x,y+1))
     return true;
    else if(findWay(x,y-1))
     return true;
    else{
     maze[x][y]='c';
     return false;
    }
   }
   else return false;
 }
 //打印迷宫路径
 private void show(){
  maze[startX][startY]='s';
  maze[endX][endY]='e';
  for(int i=1;i<x-1;i++){
   for(int j=1;j<y-1;j++){
    if(maze[i][j]=='i'){
     maze[i][j]=' ';
     step++;
    }
    else if(maze[i][j]=='c') maze[i][j]='0';
    System.out.print(maze[i][j]);
   }
   System.out.println("");
  }
  System.out.println("I Have went "+step+" Steps To The End!");
 }
 public static void main(String arg[]){
  try{
   System.out.println("Boya(8*9)/n"+"Ice(10*12)/n"+"Sky(15*17)/n"+"Input the map name:");
   BufferedReader is=new BufferedReader(new InputStreamReader(System.in));
   for(;;){
    String input=new String();
    input=is.readLine().trim();
    if(input.equals("q")) break;
    else{
     Maze boya=new Maze(input+".ice");
     if(boya.findWay(boya.startX,boya.startY)){
      boya.show();
     }
     else System.out.println("No Ways to the end!");
    }
    System.out.println("Input another map name or input 'q' to quit:");
   }
   is.close();
  }
  catch(IOException e){
   System.out.println("Wrong Input!!!");
  }
  catch(NullPointerException e){
   System.out.println("Wrong Input!!!");
  }
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值