2013-09-22 29 views
0
  20 7 
     xxxxxxxxxxxxxxxxxxEx 
     x  x  xxxx x 
     x xxxxx xxxxx xx x 
     x xxxxx xxxxxxx xx x 
     x   xx xx x 
     x xxxxxxxxxx xx x 
     xxxxxxxxxxxxSxxxxxxx 

Finding a way through the maze, S is the starting 6 12. And ends at E, 0 18. 

import java.util.*; 
import java.io.*; 

public class mazeSolver { 
boolean wall = false; 
char[][] maze; 
boolean solved; 


public mazeSolver(char[][] in_maze) { 
    maze = in_maze; 
} 

public void findPath(int row, int col) { 
    if (maze[row][col] == 'E') { 
     solved = true; 
     return; 
    } 

    maze[row][col] = 'b'; 

    if (maze[row + 1][col] == ' ' || maze[row + 1][col] == 'E') { 
     findPath(row + 1, col); 
    } 
    else if (maze[row][col + 1] == ' ' || maze[row][col + 1] == 'E') { 
     findPath(row, col + 1); 
    } 
    else if (maze[row - 1][col] == ' ' || maze[row - 1][col] == 'E') { 
     findPath(row -1, col); 
    } 
    else if (maze[row][col - 1] == ' ' || maze[row][col - 1] == 'E') { 
     findPath(row, col - 1); 
    } 
    else { 
     wall = true; 
     return; 
    } 

    if (wall) { 
     wall = false; 
     findPath(row, col); 
    } 

    if (solved) { 
     maze[row][col] = '+'; 
    } 

} 

public void printMaze(int rows, int cols) { 
    for (int i = 0; i < rows; i++) { 
     for (int j = 0; j < cols; j++) { 
      System.out.print(maze[i][j]); 
     } 

     System.out.println(); 
    } 
} 



public void solveCheck(int rows, int cols) { 
    boolean solveable = false; 
    for (int i = 0; i < rows; i++) { 
     for (int j = 0; j < cols; j++) { 
      if (maze[i][j] != '+') { 
       solveable = true; 
      } 
      break; 
     } 
     break; 
    } 

    if(!solveable){ 
     System.out.println("S is unreachable"); 
    } 
} 

}与Java代码中的错误,迷宫递归算法

也这是主要的类

 import java.util.Scanner; 
     import java.io.File; 

     public class ADTmaze { 
      public static void main(String[] args) { 
      try { 
     Scanner myScanner = new Scanner(
       new File("maze.txt")); 
     int numRows = myScanner.nextInt(); 
     int numCols = myScanner.nextInt(); 
     myScanner.nextLine(); 

     int startX = 0; 
     int startY = 0; 

     // New maze 
     char[][] maze = new char[numRows][numCols]; 

     System.out.println(numRows +","+ numCols); 



     for (int i = 0; i < numRows; i++) { 
      String nextLine = myScanner.nextLine(); 
      for (int j = 0; j < numCols; j++) { 
       char nextChar = nextLine.charAt(j); 
       maze[i][j] = nextChar; 
       System.out.print(nextChar); 
      } 
      System.out.println(); 
     } 

     // Solve the maze 
     mazeSolver newMaze = new mazeSolver(maze); 
     System.out.println(); 
     newMaze.findPath(startX, startY); 
     newMaze.printMaze(numRows, numCols); 


     // Find the starting point 
     for (int i = 0; i < numRows; i++) { 
      for (int j = 0; j < numCols; j++) { 
       if (maze[i][j] == 'S') { 
        System.out.println("Starting coordinates: " 
          + i + ", " + j); 
        startX = i; 
        startY = j; 
       } 
      } 
     } 


    } catch (Exception ex) { 
     System.out.println(ex); 
    } 
} 

}

得到一个错误,但它不会打印整个像上面的原始矩阵,我得到一个错误没有找到线。不知道它错在哪里。我不知道在哪里打印功能是怎么了?

 20,7 
    xxxxxxx 
    x  x 
    x xxxxx 
    x xxxxx 
    x  
    x xxxxx 
    xxxxxxx 
    java.util.NoSuchElementException: No line found 
+0

在Java删除此行\t xxxxxxxxxxxxxxxxxxEx XX XXXX X X为XXXXX XXXXX XX X X为XXXXX XXXXXXX XX X X XX XX X X XXXXXXXXXX XX X xxxxxxxxxxxxSxxxxxxx 查找通过的方式迷宫,S是开始的6 12.结束于E,0 18. –

+0

我建议注释掉主要的try/catch,以便从引发异常的位置获得堆栈转储。如果输入字符串的长度大于列的数量,则可以在print/build循环中获得一个异常。如果数据文件的迷宫超出了宣称的迷宫尺寸,我不确定你应该怎么做。如果没有额外的指示,我会忽略多余的字符,并打印一条消息,如果其中任何一个字符是非空白的。 –

回答

0

好,一方面是在maze.txt你的第一行是落后的,你应该有它7月20 7行(0直通6)和20列。

此外,您需要在执行递归之前找到S的位置,因此请将该块代码移到更高的位置。

最后,您需要在mazeSolver中找到数组的维数,然后测试两个如果测试块具有行+ 1和col + 1 - 也就是说,如果您从底部开始6,12,并且然后尝试并得到迷宫[7,12]元素(行+ 1)你的程序将被炸。