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");
    }
}

}

这也是主要课程

      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
4

1 回答 1

0

好吧,一方面你在 maze.txt 中的第一行是向后的,你应该有 7 20 7 行(0 到 6)和 20 列。

此外,您需要在执行递归之前找到 S 的位置,因此将那段代码向上移动。

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

于 2013-10-04T04:41:20.533 回答