cavenaghi

          BlogJava 首頁 新隨筆 聯(lián)系 聚合 管理
            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;//迷宮數(shù)組
           private int startX,startY,endX,endY;//迷宮起點(diǎn),終點(diǎn)的位置
           private int x,y,step=0;//迷宮長寬及步驟
           //依據(jù)輸入的文件名創(chuàng)建對象
           private Maze(String fileName){
            try{
             LinkedList aList=new LinkedList();//用于存儲文件每行的內(nèi)容
             BufferedReader files=new BufferedReader(new FileReader("map\\"+fileName));
             //將每行的內(nèi)容依次加入到LinkedList中
             String temp=new String();
             int i=0;
             while((temp=files.readLine())!=null){
              aList.add(temp);
             }
             files.close();
             //讀取并設(shè)置迷宮的長寬
             x=Integer.parseInt((String)aList.getFirst())+2;
             aList.removeFirst();
             y=Integer.parseInt((String)aList.getFirst())+2;
             aList.removeFirst();
             //依據(jù)長寬對迷宮進(jìn)行初始化
             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中內(nèi)容讀入數(shù)組
             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 閱讀(768) 評論(1)  編輯  收藏

          Feedback

          # re: 遞歸尋求迷宮出路 2005-12-11 04:55 sd
          xing bu xing ya zhe duan dai ma ?  回復(fù)  更多評論
            


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 天全县| 鄂州市| 德惠市| 台江县| 陆川县| 汪清县| 简阳市| 贵港市| 乌什县| 柳林县| 全南县| 启东市| 禹城市| 祁东县| 永定县| 新乡县| 成武县| 通河县| 晋城| 彩票| 积石山| 道孚县| 江阴市| 海兴县| 澄迈县| 福州市| 周至县| 昆山市| 临泽县| 漳平市| 永仁县| 壶关县| 咸宁市| 买车| 绍兴市| 云霄县| 巨野县| 墨脱县| 峨山| 永登县| 临洮县|