<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    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 閱讀(767) 評論(1)  編輯  收藏

    Feedback

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


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


    網站導航:
     
    主站蜘蛛池模板: 久久免费观看国产99精品| 最新欧洲大片免费在线 | 亚洲一区二区三区久久| 欧美男同gv免费网站观看| 色老头综合免费视频| 精品亚洲aⅴ在线观看| 日韩免费观看的一级毛片| 全黄大全大色全免费大片| 亚洲色图综合网站| 亚洲国产精品嫩草影院久久| 性色午夜视频免费男人的天堂 | 84pao强力永久免费高清| 欧洲 亚洲 国产图片综合| 一本色道久久综合亚洲精品高清| 久久国产乱子伦精品免费看| 亚洲欧洲专线一区| 亚洲AV综合色区无码另类小说 | 亚洲精品成人片在线播放| 香蕉高清免费永久在线视频| 日韩精品在线免费观看| 美女18一级毛片免费看| 亚洲的天堂av无码| 国产精品亚洲二区在线观看| 免费无码AV片在线观看软件| 18禁超污无遮挡无码免费网站| 精品视频免费在线| 中文字幕亚洲综合小综合在线| 精品亚洲综合久久中文字幕| 免费在线观看日韩| 我想看一级毛片免费的| 久久99热精品免费观看牛牛| 一级毛片免费毛片毛片| 亚洲色欲啪啪久久WWW综合网| 亚洲人成网站影音先锋播放| 中文字幕精品无码亚洲字| 免费看a级黄色片| 99在线精品视频观看免费| 69视频在线观看高清免费| a毛片视频免费观看影院| 人碰人碰人成人免费视频| 亚洲人成人网站18禁|