c++ - 我对骑士之旅的实现是无效的,可能是错误的递归调用

标签 c++ recursion knights-tour

<分区>

目前我正在尝试理解递归。在实现了一些简单的递归算法(如阶乘)后,我决定尝试一些更难的算法。

我发现了关于骑士巡回赛的问题,我尝试编写一个程序来找到有效的骑士巡回赛。

当然不行,输出是死循环。

我看不到错误。我正在自学,没有人可以问这个问题,所以我决定在这里问。

这是我的 C++ 代码:

#include<iostream>
using namespace std;
#define N 8
int tab[N][N];
void fill_with_zeros();
void display_array();
bool is_move_correct(int nx,int ny);
bool try_moving(int x,int y,int &nx,int &ny,int option);
bool knights_tour(int x,int y,int move_number);

int main()
{
    fill_with_zeros();
    knights_tour(0,0,1); //Passing coordinates and number of moves
    return 0;
}



bool is_move_correct(int nx,int ny) //This functions checks if coordinates are valid
{
    if(nx<N && ny<N && nx>=0 && ny>=0 && tab[nx][ny] == 0) return true;

    return false;
}


bool try_moving(int x,int y,int &nx,int &ny,int option)
{
    switch(option)
    {
    case 1 :  { if(is_move_correct(x+2,y+1)==true)  { nx = x +2,ny = y+ 1;return true;}}

    case 2 :  { if(is_move_correct(x+2,y-1)==true)  { nx = x +2,ny = y- 1;return true;}}

    case 3 :  { if(is_move_correct(x-2,y+1)==true)  { nx = x -2,ny = y + 1;return true;}}

    case 4 :  { if(is_move_correct(x-2,y-1)==true)  { nx = x -2,ny = y - 1;return true;}}

    case 5 :  { if(is_move_correct(x+1,y+2)==true)  { nx = x +1,ny = y +2;return true;}}

    case 6 :  { if(is_move_correct(x+1,y-2)==true)  { nx = x +1,ny = y -2;return true;}}

    case 7 :  { if(is_move_correct(x-1,y+2)==true)  { nx = x -1,ny = y +2;return true;}}

    case 8 :   { if(is_move_correct(x-1,y-2)==true)  { nx = x -1,ny = y -2;return true;}}

    }

    return false;
}


bool knights_tour(int x,int y,int move_number)
{
    tab[x][y] = move_number;

    if(move_number == N*N-1)
    {
        cout<<"Here is the knight's path : "<<endl;
        display_array();
        return true;
    }

// ******************************************************************************
//PROBLEM MUST BE IN THIS PART OF FUNCTION BUT I'M NOT ABLE TO TELL WHERE EXACTLY 
    else
    {
        //cout<<"Current move number : "<<move_number<<endl;
        int nx = 0,ny = 0;

        for(int w = 1; w<=8; w++)
        {
            if (    try_moving(x,y,nx,ny,w) == true )
            {
                if( knights_tour(nx,ny,move_number+1) == true )
                {
                    return true;
                }
            }
        }
    tab[x][y] = 0;
    }
    return false;
}
// ******************************************************************************
// These functions don't need any commentary : 

void fill_with_zeros()
{
    for(int i =0; i<N; i++)
    {
        for(int j = 0; j<N; j++)
        {
            tab[i][j] = 0;
        }
    }
}

void display_array()
{
    for(int i =0; i<N; i++)
    {
        for(int j = 0; j<N; j++)
        {
            cout<<tab[i][j]<<" ";
        }
        cout<<endl;
    }
}

最佳答案

我不认为这是无限的。但是,您正在运行具有 O(8^N) 迭代的指数递归函数。这将需要很长时间才能完成——数小时,甚至数年。你应该改进你的算法,看看你是否可以在多项式时间内完成。 (看起来@HansKlünder 指出了同样的事情。)

在网络上搜索“骑士之旅算法”会找到许多您可以尝试实现的好算法。 This question几天前发布了一个 Java 实现,作者主动提出提供帮助。

关于c++ - 我对骑士之旅的实现是无效的,可能是错误的递归调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27681684/

相关文章:

c++ - 回溯函数

c++ - undefined symbol 引用,命令行中缺少 DSO

Java:二叉搜索树递归的最小深度

c - 骑士之旅算法

javascript - 在 javascript 中递归构建 promise 链 - 内存注意事项

c - 在C中打印一系列数字

c - 骑士游算法C实现性能

g++ 3.4.6 中的 C++ 构造函数/复制构造函数问题

c++ - 如何使用 Windows Native API 访问 PE 资源?

c# - 如何从 C++ DLL 动态加载 C# dll