以下是我编写的代码。我必须为nXn编写它,但是为了简便起见,我尝试对其进行5X5测试。它不显示我的输出...有人可以告诉我以下代码有什么问题吗:

{
#include <iostream>
#include <iomanip>

using namespace std;

void knight ( int startx, int starty, int n, int p[][5], int used [][5], int &count);

int main ( )
{
    const int n = 5; // no. of cloumns and rows
    int startx = 0;
    int starty = 0;
    int p[5][5];
    int used[5][5];
    int count = 1;

    int i= 0;
    int j = 0;

    //initializing the array
    for ( i = 0; i < 5; i++)
    {
        for ( j = 0; j < 5; j++)
        {
            p[i][j] = 0;
            used [i][j] = 0;
        }
    }

    //outputting the initialized array..
        i=0;
        while ( i< 5)
        {
            for ( j = 0; j < 5; j++)
            {
                cout << setw(3) << p[i][j];
            }
            i++;
            cout << endl;
        }
        knight (startx,starty,n,p,used,count);

    return 0;
}

void knight ( int x, int y, int n, int p[][5], int used [][5], int &count)
{

        int i = 0;

    //knight (x,y,n,p,used,count)
    for ( i = 0; i < n*n; i++)
    {
        if ( used [x][y] == 0 )
        {
            used[x][y] = 1; // mark it used;
            p[x][y] += count; //inserting step no. into the solution

            //go for the next possible steps;

            //move 1
            //2 squares up and 1 to the left
            if (x-1 < 0 && y+2 < n && p[x-1][y+2] == 0)
            {
                used[x-1][y+2] = 1;
                p[x-1][y+2] += count;
                knight (x-1,y+2,n,p,used,count);
                used[x-1][y+2] = 0;
            }

            //move 2
            //2 squares up and 1 to the right
            if ( x+1 < n && y+2 < n && p[x+1][y+2] == 0 )
            {
                used[x+1][y+2] = 1;
                p[x+1][y+2] += count;
                knight (x+1,y+2,n,p,used,count);
                used[x+1][y+2] = 0;
            }

            //move 3
            //1 square up and 2 to the right
            if ( x+2 < n && y+1 < n && p[x+2][y+1] == 0 )
            {
                used[x+2][y+1] = 1;
                p[x+2][y+1] += count;
                knight (x+2,y+1,n,p,used,count);
                used[x+2][y+1] = 0;
            }

            //move 4
            //1 square down and 2 to the right
            if ( x+2 < n && y-1 < n && p[x+2][y-1] == 0 )
            {
                used[x+2][y-1] = 1;
                p[x+2][y-1] += count;
                knight (x+2,y-1,n,p,used,count);
                used[x+2][y-1] = 0;
            }

            //move 5
            //2 squares down and 1 to the right
            if ( x+1 < n && y-2 < n && p[x+1][y-2] == 0 )
            {
                used[x+1][y-2] = 1;
                p[x+1][y-2] += count;
                knight (x+1,y-2,n,p,used,count);
                used[x+1][y-2] = 0;
            }

            //move 6
            //2 squares down and 1 to the left
            if ( x-1 < n && y-2 < n && p[x-1][y-2] == 0 )
            {
                used[x-1][y-2] = 1;
                p[x-1][y-2] += count;
                knight (x-1,y-2,n,p,used,count);
                used[x-1][y-2] = 0;
            }

            //move 7
            //1 square down and 2 to the left
            if ( x-2 < n && y-1 < n && p[x-2][y-1] == 0 )
            {
                used[x-2][y-1] = 1;
                p[x-2][y-1] += count;
                knight (x-2,y-1,n,p,used,count);
                used[x-2][y-1] = 0;
            }

            //move 8
            //one square up and 2 to the left
            if ( x-2 < n && y+1< n && p[x-2][y+1] == 0 )
            {
                used[x-2][y+1] = 1;
                p[x-2][y+1] += count;
                knight (x-2,y+1,n,p,used,count);
                used[x-2][y+1] = 0;
            }
        }
    }

    if ( x == n-1 && y == n-1)
    {
        while ( i != n)
        {
            for ( int j = 0; j < n; j++)
                cout << setw(3) << p[i][j];
            i++;
        }
    }
}

谢谢!!

最佳答案

首先,一些编译器会挑剔,如果变量声明在函数声明和定义中不同,则会向您发出警告。

void knight ( int startx, int starty, int n, int p[][5], int used [][5], int &count);
[...]
void knight ( int x, int y, int n, int p[][5], int used [][5], int &count) { ... }

其次,如果您尝试将int指针传递给knight(参数count),则可以使用int *count。如果要在knight中使用递归,则最好将count声明为全局变量,或者(如果看起来像这里一样)如果count参数仅对该特定调用树有效,则可能不希望这样做使用指针,并给每个调用自己的count

第三,文件顶部的流氓{是什么?你是说把那放在那里吗?

该程序在运行时会做什么?您是否尝试过插入在代码的各个部分打印出来的调试消息,以便可以看到错误或无限循环的发生位置?

关于c++ - 骑士运动… “how to output all possible moves. ”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2573404/

10-11 22:13
查看更多