问题与我的递归迷宫遍历算法 [英] Issue with my recursive maze traversal algorithm

查看:337
本文介绍了问题与我的递归迷宫遍历算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的code的目的是创建一个程序,将读入迷宫并将其存储到一个二维数组并解决它。我得到了程序读取迷宫,并把它放入数组,但我的递归算法是行不通的,这是第三个不同的时间,我已经改变了它想要得到它的工作。所以,你能不能帮我把这个工作?

编辑:我发现我在我得到了它,解决了迷宫的感觉原算法的问题,但我已经遇到了另一个问题。该方案不打印出正确的事情。另外这只是我的好奇心。我已经发现了如何得到它的工作另一种方式。

迷宫code:

 公共静态布尔goNorth(){
        布尔成功;
        如果(迷宫[currCol] [currRow  -  1] ==迷宫[finishCol] [finishRow]){
            成功= TRUE;
            }
        如果(迷宫[currCol] [currRow  -  1] == CLEAR){
            迷宫[currCol] [currRow  -  1] =路径;
            currRow = currRow  -  1;
            成功= goNorth();
                如果(!成功){
                成功= goWest();
                    如果(!成功){
                    成功= goEast();
                        如果(!成功){
                            迷宫[currCol] [currRow] =欺压。
                            currRow = currRow + 1;
                            }
                        }
                    }
                    返回成功;
                } 其他 {
                    返回false;
            }
        }

    公共静态布尔goWest(){
        布尔成功;
        如果(迷宫[currCol  -  1] [currRow] ==迷宫[finishCol] [finishRow]){
            成功= TRUE;
            }
        如果(迷宫[currCol  -  1] [currRow] == CLEAR){
            迷宫[currCol  -  1] [currRow] =路径;
            currCol = currCol  -  1;
            成功= goWest();
                如果(!成功){
                成功= goSouth();
                    如果(!成功){
                    成功= goNorth();
                        如果(!成功){
                            迷宫[currCol] [currRow] =欺压。
                            currCol = currCol + 1;
                            }
                        }
                    }
                    返回成功;
                } 其他 {
                    返回false;
            }
        }

        公共静态布尔goEast(){
        布尔成功;
        如果(迷宫[currCol + 1] [currRow] ==迷宫[finishCol] [finishRow]){
            成功= TRUE;
            }
        如果(迷宫[currCol + 1] [currRow] == CLEAR){
            迷宫[currCol + 1] [currRow] =路径;
            currCol = currCol + 1;
            成功= goEast();
                如果(!成功){
                成功= goNorth();
                    如果(!成功){
                    成功= goSouth();
                        如果(!成功){
                            迷宫[currCol] [currRow] =欺压。
                            currCol = currCol  -  1;
                            }
                        }
                    }
                    返回成功;
                } 其他 {
                    返回false;
            }
        }

        公共静态布尔goSouth(){
        布尔成功;
        如果(迷宫[currCol] [currRow + 1] ==迷宫[finishCol] [finishRow]){
            成功= TRUE;
            }
        如果(迷宫[currCol] [currRow + 1] == CLEAR){
            迷宫[currCol] [currRow + 1] =路径;
            currRow = currRow + 1;
            成功= goSouth();
                如果(!成功){
                成功= goEast();
                    如果(!成功){
                    成功= goWest();
                        如果(!成功){
                            迷宫[currCol] [currRow] =欺压。
                            currRow = currRow  -  1;
                            }
                        }
                    }
                    返回成功;
                } 其他 {
                    返回false;
            }
        }

    }
 

迷宫求解code:

 公共类SolveMaze1
{
    公共静态无效的主要(字串[] args)
    {
        Maze1迷宫=新Maze1();
        maze.readMaze();
        maze.printMaze();
        maze.goNorth();
        maze.printMaze();
    }
}
 

期望的结果:

  xxxxxxxxxxxxxxxxxxFx
X X XXXX x
x XXXXX XXXXX XX x
x XXXXX XXXXXXX XX x
X XX XX x
x XXXXXXXXXX XX x
xxxxxxxxxxxxSxxxxxxx

xxxxxxxxxxxxxxxxxxFx
xVVVVVxPPPPPPPxxxxPx
xVxxxxxPxxxxxPPPxxPx
xVxxxxxPxxxxxxxPxxPx
xVVVVVVPPPPPPxxPxxPx
xVxxxxxxxxxxPxxPPPPx
xxxxxxxxxxxxSxxxxxxx
 

我的结果:

  xxxxxxxxxxxxxxxxxxFx
    xVVVVVxVVVVVVVxxxxVx
    xVxxxxxVxxxxxVVVxxVx
    xVxxxxxVxxxxxxxVxxVx
    xVVVVVVVVVVVVxxVxxVx
    xVxxxxxxxxxxVxxVVVVx
    xxxxxxxxxxxxSxxxxxxx
 

解决方案

有很多的意见从善如流;我只是尝试,总结他们,尽我所能。

MePePeR的建议是:用一个方法调用

将您的个人 goXXXX()调用到一个单一的去(DX,DY)通话。如果你正在尝试解决递归(你的问题建议),你也许应该是pretty的熟悉,先写你自己的方法。请注意,每个 goXXXX()方法几乎是相同的,但在每一个你修改 currCol currRow 变量。如果您通过在变化的参数(即对 currCol 更改为 DX 并变更<量code> currRow 为 DY ),你可以反复使用同样的方法,只是给它不同的值。一招东部将去(1,0),一个南下将去(0,-1)等。

如果你不是很舒服传递参数,只要给它一个尝试,发布更新code,我们可以引导您遇到的问题。

热利克的建议是:使用调试器

您已经迈出了第一步这里,你的 printMaze 方法。如果你没有在开发的IDE(我没当我刚开始学习),调试器可以稍微难用。在这种情况下,你需要简单地使用更多的的System.out.println 语句来获得你的code的执行的详细信息。如果你的的使用和IDE,开始尝试调试器。他们真的很容易使用,一旦你开始玩弄他们。再次,如果您遇到问题,跟我们谈,我们可以帮助指导你。

最后一个指针:注意到,你的迷宫打印有这样的:

  xVVx
PPVx
xPxx
 

由于您只应该去向北,向南,向东或向西到自由空间,你怎么样能够得到 V 在右上角?这是一个迹象表明,你的code是可能或者标记错误的细胞,或允许这一举动是非法的。

The purpose of my code is to create a program that will read in a maze and store it into a 2D array and solve it. I got the program to read the maze and put it into the array but my recursive algorithm is not working and this is the third different time I have changed it trying to get it to work. So could you help me get this to work?

Edit:I found out the problem with my original algorithm in the sense I got it to solve the maze but I have ran into another problem. The program is not printing out the correct things. Also this is just for my curiosity. I have already found out how to get it to work another way.

Maze Code:

 public static boolean goNorth(){
        boolean success;
        if (maze[currCol][currRow - 1] == maze[finishCol][finishRow]) {
            success = true;
            }
        if(maze[currCol][currRow - 1] == CLEAR){
            maze[currCol][currRow - 1] = PATH;
            currRow = currRow - 1;
            success = goNorth();
                if(!success){
                success = goWest();
                    if(!success){
                    success = goEast();
                        if(!success){
                            maze[currCol][currRow] = VISITED;
                            currRow = currRow + 1;
                            }
                        }
                    }
                    return success;
                } else {
                    return false;
            }
        }

    public static boolean goWest(){
        boolean success;
        if (maze[currCol - 1][currRow] == maze[finishCol][finishRow]) {
            success = true;
            }
        if(maze[currCol - 1][currRow] == CLEAR){
            maze[currCol - 1][currRow] = PATH;
            currCol = currCol - 1;
            success = goWest();
                if(!success){
                success = goSouth();
                    if(!success){
                    success = goNorth();
                        if(!success){
                            maze[currCol][currRow] = VISITED;
                            currCol = currCol + 1;
                            }
                        }
                    }
                    return success;
                } else {
                    return false;
            }
        }

        public static boolean goEast(){
        boolean success;
        if (maze[currCol + 1][currRow] == maze[finishCol][finishRow]) {
            success = true;
            }
        if(maze[currCol + 1][currRow] == CLEAR){
            maze[currCol + 1][currRow] = PATH;
            currCol = currCol + 1;
            success = goEast();
                if(!success){
                success = goNorth();
                    if(!success){
                    success = goSouth();
                        if(!success){
                            maze[currCol][currRow] = VISITED;
                            currCol = currCol - 1;
                            }
                        }
                    }
                    return success;
                } else {
                    return false;
            }
        }

        public static boolean goSouth(){
        boolean success;
        if (maze[currCol][currRow + 1] == maze[finishCol][finishRow]){
            success = true;
            }
        if(maze[currCol][currRow + 1] == CLEAR){
            maze[currCol][currRow + 1] = PATH;
            currRow = currRow + 1;
            success = goSouth();
                if(!success){
                success = goEast();
                    if(!success){
                    success = goWest();
                        if(!success){
                            maze[currCol][currRow] = VISITED;
                            currRow = currRow - 1;
                            }
                        }
                    }
                    return success;
                } else {
                    return false;
            }
        }

    }   

Maze Solver Code:

public class SolveMaze1
{
    public static void main (String[] args)
    {
        Maze1 maze = new Maze1();
        maze.readMaze();
        maze.printMaze();
        maze.goNorth();
        maze.printMaze();
    }
}

Desired outcome:

xxxxxxxxxxxxxxxxxxFx
x     x       xxxx x
x xxxxx xxxxx   xx x
x xxxxx xxxxxxx xx x
x            xx xx x
x xxxxxxxxxx xx    x
xxxxxxxxxxxxSxxxxxxx

xxxxxxxxxxxxxxxxxxFx
xVVVVVxPPPPPPPxxxxPx
xVxxxxxPxxxxxPPPxxPx
xVxxxxxPxxxxxxxPxxPx
xVVVVVVPPPPPPxxPxxPx
xVxxxxxxxxxxPxxPPPPx
xxxxxxxxxxxxSxxxxxxx

My Outcome:

    xxxxxxxxxxxxxxxxxxFx
    xVVVVVxVVVVVVVxxxxVx
    xVxxxxxVxxxxxVVVxxVx
    xVxxxxxVxxxxxxxVxxVx
    xVVVVVVVVVVVVxxVxxVx
    xVxxxxxxxxxxVxxVVVVx
    xxxxxxxxxxxxSxxxxxxx

解决方案

There's a lot of good advice in the comments; I'll just try and summarize them as best I can.

MePePeR's Advice: Use one method call

Transform your individual goXXXX() calls into a single go(dx, dy) call. If you are trying to tackle recursion (as your question suggests), you should probably be pretty familiar with writing your own methods first. Notice that each goXXXX() method is almost identical, except in each one you modify the currCol and currRow variables. If you pass in the amount of change as parameters (i.e., the change for currCol as dx and the change for currRow as dy), you can use the same method over and over, just giving it different values. A move east would be go(1, 0), a move south would be go(0, -1), etc.

If you aren't very comfortable with passing parameters, just give it a try, post the updated code and we can guide you through issues you encounter.

Hot Lick's advice: Use a debugger.

You've already taken the first step here with your printMaze method. If you aren't developing in an IDE (I didn't when I first started learning), debuggers can be a little hard to use. In that case, you'll need to simply use more System.out.println statements to get more information on the execution of your code. If you are using and IDE, start trying out the debugger. They're really easy to use once you start playing around with them. Once again, if you get stuck, talk to us and we can help guide you.

One last pointer: notice that your maze printout has this:

xVVx
PPVx
xPxx

Since you are only supposed to go north, south, east or west into a free space, how are you able to get a V in the upper right corner? It's an indication that your code is possibly either marking the wrong cell, or allowing a move that's illegal.

这篇关于问题与我的递归迷宫遍历算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆