C++ C+中的迷宫求解算法+;

C++ C+中的迷宫求解算法+;,c++,C++,我正在写一个算法,通过粘在墙上,按如下顺序移动,在迷宫中找到自己的路:下-右-上-左,直到找到出口。但有时,它会陷入无限循环,无法继续。几个小时来,我一直在想到底出了什么问题,但我一点运气都没有。这是密码 #include <iostream> #include <windows.h> const int MazeWidth = 30; const int MazeHeight = 20; const char MazeExit = '$'; const char Wal

我正在写一个算法,通过粘在墙上,按如下顺序移动,在迷宫中找到自己的路:下-右-上-左,直到找到出口。但有时,它会陷入无限循环,无法继续。几个小时来,我一直在想到底出了什么问题,但我一点运气都没有。这是密码

#include <iostream>
#include <windows.h>
const int MazeWidth = 30;
const int MazeHeight = 20;
const char MazeExit = '$';
const char Wall = '#';
const char Free = ' ';
const unsigned char SomeDude = 254;
COORD MazeExitCoords;
COORD StartingPoint;

using namespace std;
char Maze [MazeHeight][MazeWidth];




void FillDaMaze(){

    MazeExitCoords.X = MazeWidth - 20;
    MazeExitCoords.Y = 2;
    StartingPoint.X = 3;
    StartingPoint.Y = MazeHeight - 3;

    for(int i = 0; i < MazeHeight; i++){

        for(int ii = 0; ii < MazeWidth; ii++){

            if(i == 0 || i == MazeHeight - 1 || ii == 0 || ii == MazeWidth - 1){
                Maze[i][ii] = Wall;
            }
            else{
            Maze[i][ii] = Free;
            }

            if(i == MazeExitCoords.Y && ii == MazeExitCoords.X){
                    Maze[i][ii] = MazeExit;
            }
            else if(i == StartingPoint.Y && ii == StartingPoint.X){
                    Maze[i][ii] = SomeDude;
            }
        }
    }
}
void PrintDaMaze(int color){
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),color);

    for(int i = 0; i < MazeHeight; i++){

        for(int ii = 0; ii < MazeWidth;ii++){

            cout << Maze[i][ii];
        }
        cout << endl;
    }
}
void FindYourWayThroughTheMaze(){



        if(Maze[StartingPoint.Y + 1][StartingPoint.X] != Wall && Maze[StartingPoint.Y + 1][StartingPoint.X ] != SomeDude){
        StartingPoint.Y++;



        }
        else if(Maze[StartingPoint.Y][StartingPoint.X + 1] != Wall && Maze[StartingPoint.Y][StartingPoint.X + 1] != SomeDude){
            StartingPoint.X++;



        }
        else if(Maze[StartingPoint.Y - 1][StartingPoint.X] != Wall && Maze[StartingPoint.Y - 1][StartingPoint.X ] != SomeDude){
            StartingPoint.Y--;





        }
        else if(Maze[StartingPoint.Y][StartingPoint.X - 1] != Wall && Maze[StartingPoint.Y][StartingPoint.X - 1] != SomeDude){
            StartingPoint.X--;



        }


    Maze[StartingPoint.Y][StartingPoint.X] = SomeDude;

}
int main(){

FillDaMaze();
PrintDaMaze(10);
while(StartingPoint.X != MazeExitCoords.X || StartingPoint.Y != MazeExitCoords.Y){
    FindYourWayThroughTheMaze();
    system("CLS");
    PrintDaMaze(10);
    Sleep(50);
}


}
#包括
#包括
常数int MazeWidth=30;
常数int MazeHeight=20;
const char MazeExit='$';
const char Wall='#';
const char Free='';
const unsigned char SomeDude=254;
库德·马泽西库德;
库德起点;
使用名称空间std;
炭迷宫[MazeHeight][MazeWidth];
void FillDaMaze(){
MazeExitCoords.X=MazeWidth-20;
MazeExitCoords.Y=2;
起始点X=3;
起始点Y=MazeHeight-3;
对于(int i=0;icout正如Luchian已经发布的那样,该算法(即使实现正确)不适合在所有类型的迷宫中找到出路:如果你的迷宫中有一些回路,你可能会绕着这堵环形墙跑

而且,看起来,你并没有真正生成一个迷宫,而是生成一个大的区域,边界上有墙,里面有“出口”。如果出口不靠近墙(同样,目前只在“迷宫”的边界上),一个真正“粘在墙上”的算法将永远找不到出口

因为你没有移除
SomeDude
s,即你已经去过的位置,你对待
SomeDude
就像对待
Wall
一样,你在慢慢地用某种“SomeDude Wall”填满迷宫:你一直往下走,直到到达边界,然后沿着场地逆时针旋转,留下
SomeDude
s的痕迹

取决于你的起点和出口,你可以很容易地遇到这样的情况,即所有四个方向都被一堵“真实”的墙或你之前离开那里的某个
SomeDude
堵住了。然后,四个
if
-语句都没有执行,你就有了一个无限循环(因为循环体内部没有任何更改)

对于一个算法,wich粘在墙上(因此能够找到走出某些迷宫的方法),我建议以下步骤:

  • 首先,朝一个方向走,直到你撞到一堵墙
  • 设置当前方向,使墙位于右侧
  • 按照您当前的方向(不要忘记删除您的
    SomeDude
    -跟踪),直到
    • 你找到出口了
    • 在你的右边没有墙:在这种情况下,向右拐,向前走一步
    • 或者,在你前面有一堵墙。在这种情况下,向左拐,直到你前面的路空闲为止
这样,你就可以确保在你的右边总是有一堵“相同”的墙,所以你就“坚持”在那堵墙上

请记住,如果出口位于某个自由空间内,则此算法无法找到出口(因为它总是粘在墙上,出口也必须靠近要找到的墙)

对于一个能从所有可能的迷宫中找到出路的算法来说,你需要做一些回溯:记住每一点,在那里你有很多选择要继续。选择一种方法,并遵循它。如果它是一条死胡同,回到最后一个决定点,然后做下一个选择。如果没有任何方法可以通向出口,则转到上一个最后一点这是一种递归方法,在图论中被称为“深度优先搜索”(我相信,你会找到很多关于这方面的资料:)…)

嗯 马丁

要有机会解决此问题,您必须:

  • 创建
    Solve()
    例程并递归调用自身:
    • 如果第一、第二、第三……为真,
      Solve
      已成功找到解决方案
    • 如果第1、第2、第3……项包含false,则必须回溯并找到另一种方法
  • 你需要为你去过的地方建立一个缓冲区,以避免无限循环
    • 当你移动的时候,它需要密切注意
    • 当我们遇到死胡同时,我们需要消除糟糕的动作
    • 我们可以通过在猜测中燃烧并在错误时删除它来实现上述功能
以下是基于上述概念的粗略实现:

#include "stdafx.h"
#include <stdio.h>

const int MazeHeight = 9;
const int MazeWidth = 9;

char Maze[MazeHeight][MazeWidth + 1] =
{
    "# #######",
    "#   #   #",
    "# ### # #",
    "# #   # #",
    "# # # ###",
    "#   # # #",
    "# ### # #",
    "#   #   #",
    "####### #",
};

const char Wall = '#';
const char Free = ' ';
const char SomeDude = '*';

class COORD
{
public:
    int X;
    int Y;
    COORD(int x = 0, int y = 0) { X = x, Y = y; }
    COORD(const COORD &coord) { X = coord.X; Y = coord.Y; }
};

COORD StartingPoint(1, 0);
COORD EndingPoint(7, 8);

void PrintDaMaze()
{
    for (int Y = 0; Y < MazeHeight; Y++)
    {
        printf("%s\n", Maze[Y]);
    }
    printf("\n");
}

bool Solve(int X, int Y)
{
    // Make the move (if it's wrong, we will backtrack later.
    Maze[Y][X] = SomeDude;

    // If you want progressive update, uncomment these lines...
    //PrintDaMaze();
    //Sleep(50);

    // Check if we have reached our goal.
    if (X == EndingPoint.X && Y == EndingPoint.Y)
    {
        return true;
    }

    // Recursively search for our goal.
    if (X > 0 && Maze[Y][X - 1] == Free && Solve(X - 1, Y))
    {
        return true;
    }
    if (X < MazeWidth && Maze[Y][X + 1] == Free && Solve(X + 1, Y))
    {
        return true;
    }
    if (Y > 0 && Maze[Y - 1][X] == Free && Solve(X, Y - 1))
    {
        return true;
    }
    if (Y < MazeHeight && Maze[Y + 1][X] == Free && Solve(X, Y + 1))
    {
        return true;
    }

    // Otherwise we need to backtrack and find another solution.
    Maze[Y][X] = Free;

    // If you want progressive update, uncomment these lines...
    //PrintDaMaze();
    //Sleep(50);
    return false;
}

int _tmain(int argc, _TCHAR* argv[])
{
    if (Solve(StartingPoint.X, StartingPoint.Y))
    {
        PrintDaMaze();
    }
    else
    {
        printf("Damn\n");
    }

    return 0;
}
#包括“stdafx.h”
#包括
常数int MazeHeight=9;
常数int MazeWidth=9;
字符迷宫[MazeHeight][MazeWidth+1]=
{
"# #######",
"#   #   #",
"# ### # #",
"# #   # #",
"# # # ###",
"#   # # #",
"# ### # #",
"#   #   #",
"####### #",
};
const char Wall='#';
const char Free='';
const char SomeDude='*';
班级合作
{
公众:
int X;
int-Y;
坐标(intx=0,inty=0){x=x,y=y;}
坐标(const-COORD&COORD){X=COORD.X;Y=COORD.Y;}
};
坐标起始点(1,0);
坐标终点(7,8);
void PrintDaMaze()
{
对于(int Y=0;Y