cavenaghi

BlogJava 首页 新随笔 联系 聚合 管理
  4 Posts :: 0 Stories :: 3 Comments :: 0 Trackbacks

迷宫文件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!!!");
  }
 }
}

posted on 2005-07-27 11:54 Cavenaghi 阅读(750) 评论(1)  编辑  收藏

Feedback

# re: 递归寻求迷宫出路 2005-12-11 04:55 sd
xing bu xing ya zhe duan dai ma ?  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: