java - 在java中使用堆栈解决迷宫

标签 java stack maze

所以我的任务是使用 Java 中的堆栈来解决迷宫问题。我有一些代码,但我一直遇到同样的错误,而且我不确定出了什么问题。

这是我的代码:

/**
 * @author Zackie Nisar
 */
import java.io.*; 
import java.util.*; 
/**
     * Reads a file called maze.txt. 
     * In the file, a maze composed of @ signs, $ signs, periods, and hashtag exists.
     * The @ sign is the beginning of the maze, the hashtags are the walls, the $ sign the end, and the periods ways to navigate through the maze.
     * This program finds a way to navigate through that maze.
     * If the text file doesn't exist, the program will quit and exit. 
     * @param args an array of strings which contains command-line arguments in Java
     */

public class MazeSolver
{ 
    private static char maze[][];    
    private static Stack<Character> stack = new Stack<Character>(); 
        public static void main(String[] args) 
        { 
                File textFile = new File("/c:/Temp/maze.txt"); 
                String line; 
                int row = 0; 
                try
                {
                    FileReader fileReader = new FileReader(textFile); 
                    BufferedReader bufferedReader = new BufferedReader(fileReader); 
                    maze = new char[Integer.parseInt(bufferedReader.readLine())][Integer.parseInt(bufferedReader.readLine())]; 
                    while ((line = bufferedReader.readLine()) != null)
                    { 
                        maze[row] = line.toCharArray(); 
                        row++; 
                    } 
                    process(1,1); 
                } 
                catch (FileNotFoundException e)
                { 
                    System.err.println("FileNotFound: " + e.getMessage()); 
                }
                catch (IOException e)
                { 
                    System.err.println("IOException: " + e.getMessage()); 
                } 
        } 
        public static void process(int row, int column)
        { 
                displayArray(); 
                System.out.println(row + ", " + column); 
                System.out.println("size is: " + stack.size() + "\n"); 
                if (maze[row][column] == '$')
                { 
                    displayStack(row,column); 
                } 
                else
                { 
                    if (maze[row - 1][column] == '.' && (stack.isEmpty() || stack.peek() != 'd'))
                    { 
                        stack.push('u'); 
                        process(row - 1,column); 
                    } 
                    else if (maze[row + 1][column] == '.' && (stack.isEmpty() || stack.peek() != 'u'))
                    { 
                        stack.push('d'); 
                        process(row + 1,column); 
                    } 
                    else if (maze[row][column + 1] == '.' && (stack.isEmpty() || stack.peek() != 'l'))
                    { 
                        stack.push('r'); 
                        process(row,column+1); 
                    } 
                    else if (maze[row][column - 1] == '.' && (stack.isEmpty() || stack.peek() != 'r'))
                    { 
                        stack.push('l'); 
                        process(row,column - 1); 
                    } 
                    else 
                    { 
                        backtrack(row,column); 
                    } 
                }                 
        } 

        public static void displayStack(int row,int column)
        { 
                if (!stack.isEmpty())
                { 
                    System.out.print("(" + row + ", " + column + ") "); 
                    char temp = stack.pop(); 
                    if (temp == 'd')
                    { 
                        displayStack(row + 1,column); 
                    } 
                    else if (temp == 'u')
                    { 
                        displayStack(row - 1,column); 
                    } 
                    else if (temp == 'l')
                    { 
                        displayStack(row,column + 1); 
                    } 
                    else
                    { 
                        displayStack(row,column - 1); 
                    }
                }                 
        } 

        public static void onlyOne(int row, int column, char pos)
        { 
                boolean branch = false; 
                if (maze[row + 1][column] == ' ' && pos != 'u')
                { 
                        branch = true; 
                }
                else if  (maze[row - 1][column] == ' ' && pos != 'd')
                { 
                        branch = true; 
                }
                else if  (maze[row][column + 1] == ' ' && pos != 'l')
                { 
                        branch = true; 
                } 
                else if (maze[row][column - 1] == ' ' && pos != 'r')
                { 
                        branch = true; 
                }
                else if  (!branch)
                { 
                    // destroys backtracked location as there was only one exit 
                    System.out.println("terminating : " + row + "," + column + " size of stack is: " + stack.size()); 
                    maze[row][column] = '#'; 
                }                
        } 


        public static void backtrack(int row, int column)
        { 
                if (!stack.isEmpty())
                { 
                    char temp = stack.pop(); 
                    onlyOne(row,column,temp); 
                    if (temp == 'u')
                    { 
                        process(row + 1,column); 
                    } 
                    else if (temp == 'd')
                    { 
                        process(row - 1,column); 
                    } 
                    else if (temp == 'l')
                    { 
                        process(row,column + 1); 
                    } 
                    else if (temp == 'r')
                    { 
                        process(row,column - 1); 
                    } 
                }
                else 
                { 
                    System.out.print("Maze has no solution."); 
                } 
        } 

        public static void displayArray()
        { 
                for (int x = 0; x < maze.length; x++)
                { 
                        for (int y = 0; y < maze[x].length; y++)
                        { 
                                System.out.print(maze[x][y]); 
                        } 
                        System.out.println(); 
                } 
                System.out.println(); 
        } 

}





/* 
MY MAZE
@ = START
$ = END
# = WALLS
. = PATH
# # # # # # # # # # # #
# . . . # . . . . . . #
@ . # . # . # # # # . #
# # # . # . . . . # . #
# . . . . # # # . # . $
# # # # . # . # . # . #
# . . # . # . # . # . #
# # . # . # . # . # . #
# . . . . . . . . # . #
# # # # # # . # # # . #
# . . . . . . # . . . #
# # # # # # # # # # # #
 */

我不断收到同样的错误:

Exception in thread "main" java.lang.NumberFormatException: For input string: "# # # # # # # # # # # #"
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at MazeSolver.main(MazeSolver.java:28)

一些帮助和指导将不胜感激。

最佳答案

您输入 String "# # # # # # # # # # # #",然后尝试解析它以获取 int 值。 Integer.parseInt 只能解析字符串,包含数字——整数,它不能通过 ASCII 中的数字转换其他符号。

关于java - 在java中使用堆栈解决迷宫,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40117778/

相关文章:

delphi - 我怎样才能看到我的delphi应用程序当前使用了多少堆栈空间?

python迷宫使用递归

java - 无法使用 Selenium Webdriver 定位元素

java - 如何在 HashMap 中添加、删除和保存值

java - 通过删除堆栈的中间元素来打印元素

java - 迷宫,使用堆栈寻找最佳路径

c++ - 使用 Prim 算法的六角形迷宫

java - 在安卓模拟器中访问谷歌地图

java - Maven 不解析父 pom.xml 文件中的属性

用于计算表达式的 Java 算法