Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/155.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 我的骑士';s-tour算法可能正在无限循环上运行_C++_Algorithm_Recursion_Backtracking - Fatal编程技术网

C++ 我的骑士';s-tour算法可能正在无限循环上运行

C++ 我的骑士';s-tour算法可能正在无限循环上运行,c++,algorithm,recursion,backtracking,C++,Algorithm,Recursion,Backtracking,这是我写的代码 #include "genlib.h" #include <iostream> #include <math.h> #include "vector.h" struct square { int x; int y; }; bool knighttour(square start,int &counter,int cb[][8]); Vector <square> generatemoves (square sta

这是我写的代码

#include "genlib.h"
#include <iostream>
#include <math.h>
#include "vector.h"

struct square
{
    int x;
    int y;

};


bool knighttour(square start,int &counter,int cb[][8]);
Vector <square> generatemoves (square start);
void Marksquare(int &cb,int ctr);
void Unmarksquare(int &cb);
bool IsLegal(square a,int cb[][8]);




int main() 
{
    int chessboard[8][8];

    for (int i=0;i<8;i++)
        for (int j=0;j<8;j++)
            chessboard[i][j]=-1;

    int counter=1;

    for (int i=0;i<8;i++){
        for (int j=0;j<8;j++){
            square temp;
            temp.x=i;
            temp.y=j;
            if (knighttour(temp,counter,chessboard))
            {
                for (int k=0;k<8;k++){
                    cout<<chessboard[k][0]<<chessboard[k][1]<<chessboard[k][2]<<chessboard[k][3]<<chessboard[k][4]<<chessboard[k][5];
                    cout<<chessboard[k][6]<<chessboard[k][7]<<endl;}


            }

        }
    }


    return 0;
}


bool knighttour(square pt,int &counter,int cb[][8])
{

    Marksquare(cb[pt.x][pt.y],counter);
    if (counter==64)
        return true;

    counter++;

    Vector <square> temp = generatemoves(pt);

    for (int i=0;i<temp.size();i++)
    {
        if (IsLegal(temp[i],cb))
            knighttour(temp[i],counter,cb);
    }

    Unmarksquare(cb[pt.x][pt.y]);
    counter--;
    return false;

}



Vector <square> generatemoves (square start)
{
    Vector <square> temp;
    Vector <square> temp2;

        square mv1;
        mv1.x=start.x+2;
        mv1.y=start.y+1;
        temp.add(mv1);

        square mv2;
        mv2.x=mv1.x;
        mv2.y=start.y-1;
        temp.add(mv2);


        square mv3;
        mv3.y=start.y+2;
        mv3.x=start.x+1;
        temp.add(mv3);

        square mv4;
        mv4.y=start.y+2;
        mv4.x=start.x-1;
        temp.add(mv4);

        square mv5;
        mv5.x=start.x-2;
        mv5.y=start.y+1;
        temp.add(mv5);

        square mv6;
        mv6.x=start.x-2;
        mv6.y=start.y-1;
        temp.add(mv6);

        square mv7;
        mv7.y=start.y-2;
        mv7.x=start.x-1;
        temp.add(mv7);

        square mv8;
        mv8.y=start.y-2;
        mv8.x=start.x+1;
        temp.add(mv8);


        for (int i=0;i<temp.size();i++)
            if (temp[i].x>=0 && temp[i].x<=7 && temp[i].y>=0 && temp[i].y<=7)
                temp2.add(temp[i]);




        return temp2;
}



void Marksquare(int &a,int ctr)
{
    a=ctr;

}



void Unmarksquare(int &a)
{
    a=-1;
}


bool IsLegal(square a,int cb[][8])
{
    if (cb[a.x][a.y]==-1)
        return true;
    else 
        return false;
}
#包括“genlib.h”
#包括
#包括
#包括“vector.h”
结构广场
{
int x;
int-y;
};
布尔奈特图尔(广场起点、内部和柜台、内部cb[][8]);
向量生成器移动(方形开始);
void Marksquare(内部和cb、内部中心);
无效取消标记方(int和cb);
布尔·伊斯莱格尔(正方形a,内部cb[][8]);
int main()
{
国际棋盘[8][8];
对于(int i=0;i)

…重要的是要注意,穷举蛮力方法(迭代所有可能的移动序列)永远不能应用于骑士之旅问题(非常小的棋盘尺寸除外)。对于常规8x8棋盘,大约有4×1051个可能的移动序列[9]而要反复进行如此大量的动作,需要花费难以估量的时间

因此,为了确保您的程序正常运行,请尝试使用较小的电路板尺寸(例如4x4)

为了确保您的程序在合理的时间内运行8x8,您必须更改算法。除了列出的算法外,还有许多算法

--编辑--

此外,为了确保您的程序正在执行某些操作,在您仍在开发程序时添加一些跟踪始终是一个好主意

例如

bool knighttour(方形pt、内部和计数器、内部cb[][8]){
printf(“\r%d”,计数器);//:

…重要的是要注意,穷举蛮力方法(迭代所有可能的移动序列)永远不能应用于骑士之旅问题(非常小的棋盘尺寸除外)。对于常规8x8棋盘,大约有4×1051个可能的移动序列[9]而要反复进行如此大量的动作,需要花费难以估量的时间

因此,为了确保您的程序正常运行,请尝试使用较小的电路板尺寸(例如4x4)

为了确保您的程序在合理的时间内运行8x8,您必须更改算法。除了列出的算法外,还有许多算法

--编辑--

此外,为了确保您的程序正在执行某些操作,在您仍在开发程序时添加一些跟踪始终是一个好主意

例如

bool knighttour(方形pt、内部和计数器、内部cb[][8]){

printf(“\r%d”,counter);//我看到的一件事是,尽管在knighttour中当
counter==64
时返回true,但它不会被传播,调用它的函数将返回false..因此在main()中您永远不会注意到它


也就是说,即使你修正了你的算法,它也可能不会在你的有生之年完成。

我看到的一件事是,尽管你在knighttour中
计数器==64时返回true
,但它不会传播,调用它的函数将返回false。。因此你永远不会在main()中注意到它


也就是说,即使你修改了算法,它也可能在你的有生之年无法完成。

此代码可能会尝试在骑士之旅中查找所有可能的路线,并将返回最后找到的路线

而不是

for (int i=0;i<temp.size();i++)
{
    if (IsLegal(temp[i],cb))
        knighttour(temp[i],counter,cb);
}

for(inti=0;i此代码可能试图在骑士之旅中找到所有可能的路线,并将返回最后找到的路线

而不是

for (int i=0;i<temp.size();i++)
{
    if (IsLegal(temp[i],cb))
        knighttour(temp[i],counter,cb);
}

用于(int i=0;我将进入指数算法的世界。是否我没有给它足够的时间来运行?启动程序,启动gdb并将其连接到正在运行的pid,然后执行ctrl-c来中断执行。使用
bt full
调查堆栈。如果你不明白发生了什么,请回到这里,你可以简单地使用
>knighttour
每次输入时打印
计数器的值,以便观察发生了什么。欢迎来到指数算法的世界。是否可能我没有给它足够的时间来运行??启动程序,启动gdb并将其连接到运行的pid,然后执行ctrl-c来中断执行。调查堆栈using
bt full
。如果您不明白发生了什么,请回到这里。您可以让
knighttour
在每次输入时打印
计数器的值,以便观察发生了什么。
for (int i=0;i<temp.size();i++)
{
    if (IsLegal(temp[i],cb))
    {
        if(knighttour(temp[i],counter,cb))
        { 
             return true;
        }
    }

}