骑士运动....“如何输出所有可能的动作。”

时间:2010-04-04 02:56:16

标签: c++

以下是我写的代码..我必须为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++;
        }
    }
}

谢谢!

3 个答案:

答案 0 :(得分:1)

首先,一些编译器很挑剔,如果函数声明和定义中的变量名称不同,它们会给你一个警告。

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指针传递给骑士(参数count),那么您将使用int *count。如果您要在knight中使用递归,那么最好将count声明为全局,或者(如此处所示)如果count参数仅对该特定有效调用树,您可能不想使用指针,只需为每个调用提供自己的count

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

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

答案 1 :(得分:1)

这些都是代码中不同问题的提示:

if ( x == n-1 && y == n-1)执行的程序中,i有什么价值?它是如何获得这个价值的?

这个循环for ( i = 0; i < n*n; i++) 实际有什么影响?

当你有y-2 < n这样的表达式时,你真正想要的是什么? y-2

答案 2 :(得分:1)

我使用以下代码得到了答案:

#include <iostream>
#include <iomanip>

using namespace std;

bool move(int *p[] ,int x, int y,int n);
void knights (int *p[],int x,int y,int n, int count, int &pmt);
void output(int *p[],int n);

int main(char argc, char *argv[])
{

    int count = 1;
    int pmt = 0;
    int n;  //for size of board
    int x,y; // starting pos
    int **p; // to hold no. of combinations

    if ( argc != 4)
    {
        cout << "Very few arguments. Please try again.";
        cout << endl;
            return 0;
    }

    n = atoi(argv[1]);
    if( argv[1] <= 0 )
    {
        cout << " Invalid board size. ";
        return 0;
    }

    x = atoi(argv[2]);
    y = atoi(argv[3]);

    cout << "board size: " << n << ", "<< n << endl;
    cout << "starting pos: " << x << ", " << y << endl;


    //dynamic allocation of arrays to hold permutation
    p = new int *[n];
    for (int i = 0; i < n; i++)
        p[i] = new int [n];    

    //initializing board
    int i, j;
    for (i=0; i<n; i++)
    {
        for (j=0; j<n; j++)
        {
            p[i][j] = 0;
        }
    }

    output(p,n); // check output format
    knights(p,x,y,n,count,pmt);
    cout << "no. perm " << pmt << endl;


    return 0;
}

void output(int *p[],int n)
{
    int i = 0;
    int j;

    while ( i != n)
    {
        for ( j=0; j<n; j++)
        {
            cout << setw(10) << p[i][j];
        }
        cout << endl;
        i++;
    }
}

bool move(int *p[],int x, int y,int n)
{

    if (x < 0 || x >= n)
    {
        return false;
    }
    if ( y < 0 || y >= n)
    {
        return false;
    }

    if( p[x][y] != 0 )
    {
        return false;
    }

    return true;
}

void knights (int *p[], int x,int y,int n ,int count , int &pmt)
{    
    if(!move(p,x,y,n))
    {
        return;
    }

    if (count == n*n)    
    {
        cout << "New solution found: " << endl <<endl;
        p[x][y]=count;
        output(p,n);
        p[x][y]= 0;
        pmt++;
    }

    if ( p[x][y] == 0 )
    {
        p[x][y] = count;

        //move 1
        knights (p,  x-1, y-2, n, count+1, pmt);

        //move 2
        knights (p,  x+1, y-2, n, count+1, pmt);

        //move 3
        knights (p,  x+2, y-1, n, count+1, pmt);

        //move 4
        knights (p,  x+2, y+1, n, count+1, pmt);

        //move 5
        knights (p,  x+1, y+2, n, count+1, pmt);

        //move 6
        knights (p,  x-1, y+2, n, count+1, pmt);

        //move 7
        knights (p,  x-2, y+1, n, count+1, pmt);

        //move 8
        knights (p,  x-2, y-1, n, count+1, pmt);

        p[x][y] = 0; //setting square back to zero
    }        
}