gpt4 book ai didi

c++ - 骑士运动.... "how to output all possible moves. "

转载 作者:搜寻专家 更新时间:2023-10-31 01:59:53 25 4
gpt4 key购买 nike

以下是我编写的代码。我必须为 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com