11.4(周六)

  1. </pre><pre name="code" class="java">package com.maze.path;  
  2.   
  3. import java.util.Stack;  
  4. import java.io.BufferedReader;  
  5. import java.io.File;  
  6. import java.io.FileInputStream;  
  7. import java.io.InputStreamReader;  
  8.   
  9. class Step{  
  10.     int x,y;  
  11.     public Step(int x,int y) {  
  12.         this.x = x;  
  13.         this.y = y;  
  14.     }  
  15. }  
  16.   
  17. public class Path {  
  18.     private int inx,iny; //the location of Entrance and Exit  
  19. //  private char map[][];   
  20.     static  int dir[][] = {{0,1},{0,-1},{1,0},{-1,0}}; //4 directories each point  
  21.     static  int N; //maze size=N*N   
  22.       
  23.     public static char[][] readMap(String filePath){  
  24.         try {  
  25.                 String encoding="GBK";  
  26.                 File file=new File(filePath);  
  27.                 if(file.isFile() && file.exists()){   
  28.                     InputStreamReader read = new InputStreamReader(new FileInputStream(file),encoding);  
  29.                     BufferedReader bReader = new BufferedReader(read);  
  30.                     String lineTxt = null;  
  31.                     String array = "";  
  32.                     while((lineTxt = bReader.readLine()) != null){     
  33.                         array += lineTxt;  
  34.                     }  
  35.                     read.close();  
  36.                     array = array.replace(" """);  
  37.                       
  38.                     int i = 0;     
  39.                     int n = Integer.parseInt(array.charAt(i++)+"");   
  40.                     Path.N = n;  
  41.                     //System.out.println("n:6 "+n+"i:0 "+i);  
  42.                       
  43.                     char[][] cArray = new char[n][n] ;  
  44.                     for(int col=0;col<6;col++){  
  45.                             for(int row=0;row<6;row++){       
  46.                             char a = array.charAt(i);  
  47.                             cArray[col][row] = a;  
  48.                             i++;  
  49.                             //System.out.println(i+"a:"+a);  
  50.                             if(i>array.length()) {  
  51.                                 System.out.println("Sorry,this maze is not N*N standard size");  
  52.                                 break;  
  53.                                 }                         
  54.                              }  
  55.                         }                         
  56.                     return cArray;            
  57.         } else {  
  58.             System.out.println("can't find the file");                
  59.         }  
  60.         } catch (Exception e) {// readLine() method must handle java.io.IOException Exception   
  61.             System.out.println("run error"+e.getMessage());  
  62.             e.printStackTrace();  
  63.         }  
  64.         return null;       
  65.     }  
  66.   
  67.     public static void main(String[] args) {  
  68.         String filePath = "E:\\files\\Maze.txt";          
  69.         int dirs[][] = Path.dir;  
  70.         Path path = new Path();  
  71.         Stack<Step> ss = new Stack<Step>();       
  72.         char maps[][] = readMap(filePath);        
  73.         if(maps == null || maps.length == 0)   
  74.             System.out.println("read map failed ");       
  75.           
  76.         path.printmaze(maps);  
  77.         path.setEntrance(maps);  
  78.         int inxs = path.inx;  
  79.         int inys = path.iny;  
  80.         path.findPath(maps, inxs, inys, dirs, ss);  
  81.   
  82.     }  
  83.       
  84.     public void setEntrance (char[][] map) {  
  85.         for(int i=0;i<N;i++)  
  86.             for(int j=0;j<N;j++)  
  87.                 if(map[i][j]=='E' || map[i][j]=='e') {  
  88.                     inx = j;  
  89.                     iny = i;  
  90.                 }         
  91.     }  
  92.       
  93.     public void printmaze(char[][] map){  
  94.         for(int i=0;i<N;i++)  
  95.         {  
  96.             for(int j=0;j<N;j++){  
  97.                 System.out.print(map[i][j]+" ");                  
  98.             }  
  99.             System.out.println("");   
  100.               
  101.         }  
  102.           
  103.     }  
  104.       
  105.     public void printmaze(int[][] map){  
  106.         for(int i=0;i<4;i++)  
  107.         {  
  108.             for(int j=0;j<2;j++){  
  109.                 System.out.print(map[i][j]+" ");                  
  110.             }  
  111.             System.out.println("");   
  112.               
  113.         }  
  114.           
  115.     }  
  116.       
  117.     public void findPath(char[][] map,int inx, int iny,int[][] dir,Stack<Step> s) {  
  118.   
  119.         Step temp = new Step(inx,iny);  
  120.         s.push(temp);         
  121.         while (!s.empty()) {  
  122.             //System.out.println("k = " + k++ );  
  123.             int d = 0;  
  124.             int mark = 0;//mark the first pushStack  
  125.             temp = s.pop();  
  126.             int x = temp.x;  
  127.             int y = temp.y;           
  128.             //System.out.println("temp.x = "+ temp.x+ ", temp.y = "+ temp.y);  
  129.               
  130.             while (d < 4) {  
  131.                 //System.out.println("d = " + d);  
  132.                 int j = x + dir[d][0];  
  133.                 int i = y + dir[d][1];  
  134.                   
  135.                 if(i < 0 || i >= N || j < 0 ||j >= N ) {  
  136.                     d++;                      
  137.                 } else if (map[i][j] == 'X' || map[i][j] == 'x'){  
  138.                     if(s.empty()){                        
  139.                         System.out.println("no valid path");      
  140.                         return;  
  141.                     }  
  142.                     else {  
  143.                         if(mark == 0){  
  144.                             temp = new Step(x,y);  
  145.                             s.push(temp);  
  146.                         }  
  147.                         System.out.print("("+j+","+i+") ");  
  148.                         while(!s.empty()){  
  149.                             Step stp = s.pop();  
  150.                             System.out.print("("+stp.x+","+stp.y+") ");  
  151.                         }                             
  152.                         return ;  
  153.                     }                     
  154.                 } else if (map[i][j]=='*') {                      
  155.                     if(mark == 0){  
  156.                         temp = new Step(x,y);  
  157.                         s.push(temp);  
  158.                     }                 
  159.                     x = j;  
  160.                     y = i;  
  161.                     temp = new Step(x,y);  
  162.                     s.push(temp);  
  163.                     d++;  
  164.                     mark=1;  
  165.                     map[i][j] = 'v';//change the status of the point which has been visited  
  166.                 } else   
  167.                     d++;  
  168.             }         
  169.         }  
  170.         if(s.empty()) System.out.println("no valid path");  
  171.     }  
  172.   
  173. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值