C# 如何检查迷宫C上搜索的上一条路径#

C# 如何检查迷宫C上搜索的上一条路径#,c#,console-application,C#,Console Application,我正在尝试编写一个解决迷宫问题的算法,但要正确应用它,我面临一些困难 在找到有效点后,该算法将在墙上运行,而不是改变方向 我不清楚如何检查上一个点,然后从该点检查下一个有效移动 有人能帮我告诉我该往哪个方向走吗 class MapPathFinder { public bool[,] correctPath = new bool[12,12]; public int[,] previousPoint = new int[12, 12]; public bool star

我正在尝试编写一个解决迷宫问题的算法,但要正确应用它,我面临一些困难

在找到有效点后,该算法将在墙上运行,而不是改变方向

我不清楚如何检查上一个点,然后从该点检查下一个有效移动

有人能帮我告诉我该往哪个方向走吗

class MapPathFinder
{
    public bool[,] correctPath = new bool[12,12];
    public int[,] previousPoint = new int[12, 12];
    public bool startPointFound = false;
    public bool nextValidMove(MapFile map, int y, int x)
    {
        if ((y == map.width) && (x == map.height)) { 

            return false; //Checks if at the edge and terminates the method
        }

        if ((map.Matrix[y, x]) == 1 ) {
            return true; // check if at a wall and terminate the method
        }

        if (y != 0)
        {
            if (nextValidMove(map, y-1,x))
            {
                map.Matrix[y, x] = 9; //changes the color of the position
                correctPath[y, x] = true;
                return correctPath[y, x];
            }

            if (y != map.width - 1) //check if at the limit of the map
            {
                if (nextValidMove(map,y + 1, x))
                {
                    map.Matrix[y, x] = 9;
                    correctPath[y, x] = true;
                    return correctPath[y, x];
                }       
            }

            if (x != 0)
            {
                if (nextValidMove(map, y, x - 1))
                {
                    map.Matrix[y, x] = 9;
                    correctPath[y, x] = true;
                    return correctPath[y, x];
                }
            }

            if (x != map.height - 1)
            {
                if (nextValidMove(map, y, x + 1))
                {
                    map.Matrix[y, x] = 9;
                    correctPath[y, x] = true;

                    return correctPath[y, x];
                }
            }
        }
        return false;
    }

    public bool PathFinder(MapFile map)
    {
        for (int y = 1; y < map.width; y++)
        {
            for (int x = 1; x < map.height; x++)
            {
               var status = MapDisplay.DisplayMap(map);
                 if (status)
               {
                   nextValidMove(map, x, y);
               }
            }           
        }
        return true;
    }
我真的不明白为什么我要检查nextValidMove(y-1,x),因为在我的if语句开始时它已经是真的:

if(map.Matrix[y-1, x] == 2 && !nextValidMove(y-1,x))
我想一起检查上一点,如下所示:

if(nextValidMove(map, y - 1, x)&&!previousPoint[y-1,x])

但是我得到了一个堆栈溢出错误。我再也不知道如何离开那里了。

你的墙是由任何相邻的单元中有一个
或地板上有一个
决定的,
S
开始和
F
结束

在这种情况下,你只需要在一个旋转的基础上检查,从,比如说,北方开始,然后移动到下一轮的位置,直到你回到北方。每一张支票都应该被推到堆栈上,当它一无所获时就会弹出。这样,至少你每次都可以追溯到过去

//  Test to see if we've found Utopia...
if(map.Matrix[x, y] == 'F') return true;
//  Test the North wall...
if(y-1>=0 &&          map.Matrix[x, y-1]=='.' && !nextValidMove(map, x, y-1)) return false;
//  Test the East wall...
if(x+1<=map.width  && map.Matrix[x+1, y]=='.' && !nextValidMove(map, x+1, y)) return false;
//  Test the South wall...
if(y+1<=map.height && map.Matrix[x, y+1]=='.' && !nextValidMove(map, x, y+1)) return false;
//  Test the West wall...
if(x-1>=0 &&          map.Matrix[x-1, y]=='.' && !nextValidMove(map, x-1, y)) return false;
//测试一下我们是否找到了乌托邦。。。
如果(映射矩阵[x,y]='F')返回true;
//测试北墙。。。
如果(y-1>=0&&map.Matrix[x,y-1]='.&&!nextValidMove(map,x,y-1))返回false;
//测试东墙。。。

如果(x+1我已经重写了你的MapPathFinder类,使它能够工作

class MapPathFinder
{
    public const byte WALL = 1;
    public const byte ROAD = 2;
    public const byte START = 3;
    public const byte FINISH = 5;
    public const byte ALREADY_THERE = 9;

    public bool NextValidMove(MapFile map, int x, int y)
    {
        // Check edges
        if (x < 0 || x > map.width || y < 0 || y > map.height)
            return false;

        byte currentPosition = map.Matrix[x, y];

        // Check walls or already there
        if (currentPosition == WALL || currentPosition == ALREADY_THERE)
            return false;

        // Print
        var status = MapDisplay.DisplayMap(map);

        if (status)
        {
            // Check finish
            if (currentPosition == FINISH)
            {
                return true; // We've arrived!
            }

            // Road
            //
            // Set ALREADY THERE
            map.Matrix[x, y] = ALREADY_THERE;

            // Left
            if (NextValidMove(map, x - 1, y))
                return true;

            // Right
            if (NextValidMove(map, x + 1, y))
                return true;

            // Up
            if (NextValidMove(map, x, y - 1))
                return true;

            // Down
            if (NextValidMove(map, x, y + 1))
                return true;

            // Not the correct path.. 
            map.Matrix[x, y] = ROAD;
        }

        return false;
    }

    public bool PathFinder(MapFile map)
    {
        // Looking for start point
        for (int x = 0; x < map.width; x++)
        {
            for (int y = 0; y < map.width; y++)
            {
                if (map.Matrix[x, y] == START)
                    return NextValidMove(map, x, y);
            }
        }

        return false;
    }
}
类MapPathFinder
{
公共常量字节墙=1;
公共建筑字节道路=2;
公共常量字节开始=3;
公共常量字节完成=5;
public const byte已经在那里=9;
公共bool NextValidMove(映射文件映射,int x,int y)
{
//检查边缘
if(x<0 | | x>map.width | | y<0 | | y>map.height)
返回false;
字节currentPosition=映射矩阵[x,y];
//检查墙壁或已经在那里
如果(currentPosition==墙壁| | currentPosition==已经在那里)
返回false;
//印刷品
var status=MapDisplay.DisplayMap(地图);
如果(状态)
{
//检查饰面
如果(当前位置==完成)
{
return true;//我们到了!
}
//道路
//
//已经准备好了吗
map.Matrix[x,y]=已经存在;
//左
if(NextValidMove(map,x-1,y))
返回true;
//对
如果(下一次有效移动(地图,x+1,y))
返回true;
//向上
if(NextValidMove(map,x,y-1))
返回true;
//向下
if(下一次验证移动(地图,x,y+1))
返回true;
//不是正确的路径。。
地图矩阵[x,y]=道路;
}
返回false;
}
公共布尔探路者(地图文件地图)
{
//寻找起点
对于(int x=0;x

然而,我给你留下了一些工作:

  • 没有存储正确的路径
  • 如果有两条正确的路径,该算法不会总是选择较短的路径,而是选择最先找到的路径

代码中描述其背后思想的一些注释将非常有用。请让能够帮助您的人的思考过程不要太困难。不应该
if((y==map.width)&&&(x==map.height))
实际上是
if((y==map.width)|(x==map.height))
,如果你在检查地图的极端情况?为什么你真的需要这样做?每个单元格都应该设置墙以防止向特定方向移动。我想它确实阻止了例程中其余代码的执行,并使其以毫秒的速度加快了……@Paul在这里进行注释开发后,可以得到e迷宫里老鼠的感觉;)@MongZhu:总是向左转!总是向左转。。。就像说实话一样。。。我现在觉得自己很笨,因为我到现在还不能完成这项工作。我贴了一个问题的编辑,试图解释一下。谢谢你帮我,对不起?我不明白你在那里说了什么-你的意思是我的回答确实有帮助还是让你感到困惑?这个答案帮助我进一步解决了我的问题。我只是觉得自己很愚蠢,因为我似乎不明白如何从那里解决这个问题。用你学到的知识更新你的问题,这样我们都能明白你的意思。
//  Test to see if we've found Utopia...
if(map.Matrix[x, y] == 'F') return true;
//  Test the North wall...
if(y-1>=0 &&          map.Matrix[x, y-1]=='.' && !nextValidMove(map, x, y-1)) return false;
//  Test the East wall...
if(x+1<=map.width  && map.Matrix[x+1, y]=='.' && !nextValidMove(map, x+1, y)) return false;
//  Test the South wall...
if(y+1<=map.height && map.Matrix[x, y+1]=='.' && !nextValidMove(map, x, y+1)) return false;
//  Test the West wall...
if(x-1>=0 &&          map.Matrix[x-1, y]=='.' && !nextValidMove(map, x-1, y)) return false;
class MapPathFinder
{
    public const byte WALL = 1;
    public const byte ROAD = 2;
    public const byte START = 3;
    public const byte FINISH = 5;
    public const byte ALREADY_THERE = 9;

    public bool NextValidMove(MapFile map, int x, int y)
    {
        // Check edges
        if (x < 0 || x > map.width || y < 0 || y > map.height)
            return false;

        byte currentPosition = map.Matrix[x, y];

        // Check walls or already there
        if (currentPosition == WALL || currentPosition == ALREADY_THERE)
            return false;

        // Print
        var status = MapDisplay.DisplayMap(map);

        if (status)
        {
            // Check finish
            if (currentPosition == FINISH)
            {
                return true; // We've arrived!
            }

            // Road
            //
            // Set ALREADY THERE
            map.Matrix[x, y] = ALREADY_THERE;

            // Left
            if (NextValidMove(map, x - 1, y))
                return true;

            // Right
            if (NextValidMove(map, x + 1, y))
                return true;

            // Up
            if (NextValidMove(map, x, y - 1))
                return true;

            // Down
            if (NextValidMove(map, x, y + 1))
                return true;

            // Not the correct path.. 
            map.Matrix[x, y] = ROAD;
        }

        return false;
    }

    public bool PathFinder(MapFile map)
    {
        // Looking for start point
        for (int x = 0; x < map.width; x++)
        {
            for (int y = 0; y < map.width; y++)
            {
                if (map.Matrix[x, y] == START)
                    return NextValidMove(map, x, y);
            }
        }

        return false;
    }
}