迷宫计划无法完成

时间:2015-09-27 03:08:37

标签: java algorithm maze

所以我正在编写一个生成然后解决迷宫的程序。除了这一件小事之外我一切都在工作。迷宫中的标记将进入完成迷宫的倒数第二步,然后停止然后移动到另一个方向。我一直在研究这个问题大约2个小时,跟踪我的代码并且似乎无法确定它在最后一步移动时的位置或原因。

迷宫的规则是X是墙,无法移动,O是可以移动的可能区域,.是起点。 -是我已经移动过的路径。标记可以在任何序数或基本方向上移动(北,东北等)。

我有两个矩阵,一个名为mat,用XO以及-显示给用户。我还有另一个名为visited的矩阵,其大小与mat相同,并且是一种幕后矩阵,用于跟踪我能够或不能移动到的坐标。它存储W墙,Y表示我已经访问过的坐标,N表示我可以访问的坐标。

我解决迷宫的方法是检查从北方开始的可能移动并在指南针周围逆时针移动,直到找到可能的移动。标记无法移动到已移动的位置。

如果我遇到一个包含多个可能移动的分割路径,我将该位置的坐标放在两个名为splitx的堆栈中,用于行,splity用于列,以便我可以回到它后来。如果我遇到一个死角,每个方格都是一个墙或已经访问过,我回溯到我的拆分路径堆栈中的最后一个坐标,关闭我刚刚移动到的路径,并弹出堆栈的顶部值。

我还有另一个名为visitStack的堆栈,它存储我为北方N所做的所有移动,为东北移动NE。这让我可以回溯我的动作并转到我遇到的最后一条分道。这是代码,下面是一些选择的输出。如果您对代码或输出有任何疑问或只是澄清一般,请随时提出。

import java.util.*;

public class MazeLab
{
    public static void main(String args[]) 
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter random starting seed  ===>>  ");
        int seed = input.nextInt();

        Maze maze = new Maze(seed);
        maze.displayMaze();
        maze.solveMaze();
    }
}

class Maze
{

    private char mat[][];               // 2d character array that stores the maze display
    private char visited[][];           // 2d character array that works behind to scenes to let me know where I can or can't move
    private Stack<String> visitStack;           // stack that stores every move I make as N, NE, E, SE, etc
    private int nowR, nowC;                     // coordinates for current position in the matrix
    private int startRow, startCol;             // coordinates for the starting position
    private boolean done = false;               // not that important
    private Stack<Integer> splitx = new Stack<Integer>(); // row coord for split paths
    private Stack<Integer> splity = new Stack<Integer>(); // col coord for split paths
    Random random = new Random();

    public Maze(int seed)
    // constructor which generates the random maze, random starting location
    // and initializes Maze class values.  If the random value equals 0 the maze
    // store an 'X' otherwise it store an 'O' in the maze.
    {
        Random randomize = new Random(seed);
        mat = new char[12][12];
        visited = new char[12][12];
        for (int r = 0; r < 12; r++)
            for (int c = 0; c < 12; c++)
            {
                if (r == 0 || c == 0 || r == 11 || c == 11)
                    mat[r][c] = 'X';
                else
                {
                    int rndInt = randomize.nextInt(2);
                    if (rndInt == 0)
                        mat[r][c] = 'X';
                    else
                        mat[r][c] = 'O';
                }
            }
        mat[0][0] = 'O';
        startRow = randomize.nextInt(12);
        startCol = 11;
        nowR = startRow;
        nowC = startCol;
        mat[startRow][startCol] = '.';
        visitStack = new Stack<String>();

        for(int i = 0; i < mat.length; i++)
            for(int k = 0; k < mat[i].length; k++)
                if(mat[i][k] == 'X')
                    visited[i][k] = 'W';
                else
                    visited[i][k] = 'N';
//                                      Avoid going back to the starting point
        visited[nowR][nowC] = 'Y';
    }


    void displayMaze()
    // displays the current maze configuration
    {
        for (int r = 0; r < 12; r++)
        {
            for (int c = 0; c < 12; c++)
                System.out.print(mat[r][c] + "  ");
            System.out.println();
        }
        System.out.println();

        if(done == false)
            pause();
    }


    public void solveMaze()
    {
    // for testing purposes, this is not the real method
        for(int i = 0; i < 15; i++)
        {
            getMove();
            displayMaze();
        }
    }

    private int numMoves()
    // This method determines if a position has a valid move or not       
    {
        int moves = 0;
        if(nowR != 0 && visited[nowR-1][nowC] == 'N')
            moves++;
        if(nowR != 0 && nowC != 11 && visited[nowR-1][nowC+1] == 'N')
            moves++;
        if(nowC != 11 && visited[nowR][nowC+1] == 'N')
            moves++;
        if(nowR != 11 && nowC != 11 && visited[nowR+1][nowC+1] == 'N')
            moves++;
        if(nowR != 11 && visited[nowR+1][nowC] == 'N')
            moves++;
        if(nowR != 11 && nowC != 0 && visited[nowR+1][nowC-1] == 'N')
            moves++;
        if(nowC != 0 && visited[nowR][nowC-1] == 'N')
            moves++;
        if(nowR != 0 && nowC != 0 && visited[nowR-1][nowC-1] == 'N')
            moves++;
        return moves;
    }

    private void getMove()
    {
        if(numMoves() > 1)
        {
//                                      checks for split paths
//                                      north
            if(nowR != 0 && visited[nowR-1][nowC] == 'N')
            {
                splitx.push(nowR);
                splity.push(nowC);
            }
//                                      northwest
            if(nowR != 0 && nowC != 0 && visited[nowR-1][nowC-1] == 'N')
            {
                splitx.push(nowR);
                splity.push(nowC);
            }
//                                      west
            if(nowC != 0 && visited[nowR][nowC-1] == 'N')
            {
                splitx.push(nowR);
                splity.push(nowC);
            }
//                                      southwest
            if(nowR != 11 && nowC != 0 && visited[nowR+1][nowC-1] == 'N')
            {
                splitx.push(nowR);
                splity.push(nowC);
            }
//                                      south
            if(nowR != 11 && visited[nowR+1][nowC] == 'N')
            {
                splitx.push(nowR);
                splity.push(nowC);
            }
//                                      southeast
            if(nowR != 11 && nowC != 11 && visited[nowR+1][nowC+1] == 'N')
            {
                splitx.push(nowR);
                splity.push(nowC);
            }
//                                      east
            if(nowC != 11 && visited[nowR][nowC+1] == 'N')
            {
                splitx.push(nowR);
                splity.push(nowC);
            }
//                                      northeast
            if(nowR != 0 && nowC != 11 && visited[nowR-1][nowC+1] == 'N')
            {
                splitx.push(nowR);
                splity.push(nowC);
            }
        }
        if(numMoves() > 0)
        {
//                                      moves the marker, oriented to the right
//                                      north
            if(nowR != 0 && visited[nowR-1][nowC] == 'N')
            {
                nowR--;
                visited[nowR][nowC] = 'Y';
                visitStack.push("N");
                mat[nowR][nowC] = '-';
//              System.out.println("PUSHING ON NORTH");
            }
//                                      northwest
            else if(nowR != 0 && nowC != 0 && visited[nowR-1][nowC-1] == 'N')
            {
                nowR--;
                nowC--;
                visited[nowR][nowC] = 'Y';
                visitStack.push("NW");
                mat[nowR][nowC] = '-';
//              System.out.println("PUSHING ON NORTHWEST");
            }   
//                                      west
            else if(nowC != 0 && visited[nowR][nowC-1] == 'N')
            {
                nowC--;
                visited[nowR][nowC] = 'Y';
                visitStack.push("W");
                mat[nowR][nowC] = '-';
//              System.out.println("PUSHING ON WEST");
            }
//                                      southwest
            else if(nowR != 11 && nowC != 0 && visited[nowR+1][nowC-1] == 'N')
            {
                nowR++;
                nowC--;
                visited[nowR][nowC] = 'Y';
                visitStack.push("SW");
                mat[nowR][nowC] = '-';
//              System.out.println("PUSHING ON SOUTHWEST");
            }
//                                      south
            else if(nowR != 11 && visited[nowR+1][nowC] == 'N')
            {
                nowR++;
                visited[nowR][nowC] = 'Y';
                visitStack.push("S");
                mat[nowR][nowC] = '-';
//              System.out.println("PUSHING ON SOUTH");
            }
//                                      southeast
            else if(nowR != 11 && nowC != 11 && visited[nowR+1][nowC+1] == 'N')
            {
                nowR++;
                nowC++;
                visited[nowR][nowC] = 'Y';
                visitStack.push("SE");
                mat[nowR][nowC] = '-';
//              System.out.println("PUSHING ON SOUTHEAST");
            }
//                                      east
            else if(nowC != 11 && visited[nowR][nowC+1] == 'N')
            {
                nowC++;
                visited[nowR][nowC] = 'Y';
                visitStack.push("E");
                mat[nowR][nowC] = '-';
//              System.out.println("PUSHING ON EAST");
            }
//                                      northeast
            else if(nowR != 0 && nowC != 11 && visited[nowR-1][nowC+1] == 'N')
            {
                nowR--;
                nowC++;
                visited[nowR][nowC] = 'Y';
                visitStack.push("NE");
                mat[nowR][nowC] = '-';
//              System.out.println("PUSHING ON NORTHEAST");
            }
        }
        if(numMoves() == 0)
        {
            while(nowR != splitx.peek() && nowC != splity.peek())
            {
                switch(visitStack.pop())
                {
//                                  have to backtrack the opposite direction i previously went
                    case "N":   visited[nowR][nowC] = 'N';
                                mat[nowR][nowC] = 'O';
                                nowR++;
                                break;
                    case "NE":  visited[nowR][nowC] = 'N';
                                mat[nowR][nowC] = 'O';
                                nowR++;
                                nowC--;
                                break;
                    case "E":   visited[nowR][nowC] = 'N';
                                mat[nowR][nowC] = 'O';
                                nowC--;
                                break;
                    case "SE":  visited[nowR][nowC] = 'N';
                                mat[nowR][nowC] = 'O';
                                nowR--;
                                nowC--;
                                break;
                    case "S":   visited[nowR][nowC] = 'N';
                                mat[nowR][nowC] = 'O';
                                nowR--;
                                break;
                    case "SW":  visited[nowR][nowC] = 'N';
                                mat[nowR][nowC] = 'O';
                                nowR--;
                                nowC++;
                                break;
                    case "W":   visited[nowR][nowC] = 'N';
                                mat[nowR][nowC] = 'O';
                                nowC++;
                                break;
                    case "NW":  visited[nowR][nowC] = 'N';
                                mat[nowR][nowC] = 'O';
                                nowR++;
                                nowC++;
                                break;
                    default: System.out.println("SOMETHING WENT WRONG WITH BACKTRACKING");
                }
            }
//                                      blocks off dead ends
//                                      north
            if(nowR != 0 && visited[nowR-1][nowC] == 'N')
                visited[nowR-1][nowC] = 'Y';
//                                      northwest
            else if(nowR != 0 && nowC != 0 && visited[nowR-1][nowC-1] == 'N')
                visited[nowR-1][nowC-1] = 'Y';  
//                                      west
            else if(nowC != 0 && visited[nowR][nowC-1] == 'N')
                visited[nowR][nowC-1] = 'Y';
//                                      southwest
            else if(nowR != 11 && nowC != 0 && visited[nowR+1][nowC-1] == 'N')
                visited[nowR+1][nowC-1] = 'Y';
//                                      south
            else if(nowR != 11 && visited[nowR+1][nowC] == 'N')
                visited[nowR+1][nowC] = 'Y';
//                                      southeast
            else if(nowR != 11 && nowC != 11 && visited[nowR+1][nowC+1] == 'N')
                visited[nowR+1][nowC+1] = 'Y';
//                                      east
            else if(nowC != 11 && visited[nowR][nowC+1] == 'N')
                visited[nowR][nowC+1] = 'Y';
//                                      northeast
            else if(nowR != 0 && nowC != 11 && visited[nowR-1][nowC+1] == 'N')
                visited[nowR-1][nowC+1] = 'Y';

            splitx.pop();
            splity.pop();
        }
    }

    private void pause() 
    {   
        Scanner input = new Scanner(System.in); 
        String dummy;
        System.out.print("\nPress <Enter> to continue  ===>>  ");                       
        dummy = input.nextLine();                               
    }
}

这是标记穿过它之前的迷宫。 enter image description here

这是最后的迷宫。注意当标记向西北移动以完成迷宫时,它如何在下一个转弯处停留在同一个位置,然后在转弯后向南移动。

enter image description here

1 个答案:

答案 0 :(得分:3)

所以,你处于位置(1,1)并找到两个动作:NW和S.

if(numMoves() > 1)会在堆栈中触发两个

if(numMoves() > 0)触发并移动NW移动,离开(0,0)。

if(numMoves() == 0)触发并开始回溯,因为(0,0)没有移动。

2个问题:

  • 检测到您找到退出的代码在哪里?
  • if(numMoves() == 0)正在使用坐标调用numMoves()。将其更改为else