在C语言中,八皇后在递归中不打印任何内容
我自己也在上C课。当我运行我的代码时,什么都没有发生。打印什么都没有,我也找不到问题所在。 我发现我的检查功能很笨拙,我该如何改进它,使它变得纤细和简单? 这是我的密码在C语言中,八皇后在递归中不打印任何内容,c,n-queens,C,N Queens,我自己也在上C课。当我运行我的代码时,什么都没有发生。打印什么都没有,我也找不到问题所在。 我发现我的检查功能很笨拙,我该如何改进它,使它变得纤细和简单? 这是我的密码 #include <stdio.h> #define ON 1 #define OFF 0 #define SIZE 8 void initial(int (*table)[SIZE]); void setqueen(int (*table)[SIZE], const int row); int
#include <stdio.h>
#define ON 1
#define OFF 0
#define SIZE 8
void initial(int (*table)[SIZE]);
void setqueen(int (*table)[SIZE], const int row);
int check(const int (*table)[SIZE], const int row, const int col);
void prtable(const int (*table)[SIZE]);
int main(void)
{
int table[SIZE][SIZE];
int row = 0;
initial(table);
setqueen(table, row);
return 0;
}
void initial(int (*table)[SIZE])
{
int row, col;
for (row = 0; row < SIZE; row++)
for (col = 0; col < SIZE; col++)
table[row][col] = OFF;
}
/*
place a queen(set value = 1) in the first column of the first row and
check there is no conflict. if there is a conflict, count and move to
next column. if there is conflict in every column(count = 8), return.
if there is no conflict, call it recursively. when it place all queens,
print the table.
*/
void setqueen(int (*table)[SIZE], const int row)
{
int c = 0;
int count = 0;
for ( ; c < SIZE; c++) {
table[row][c] = ON;
if (check(table, row, c) == ON) {
table[row][c] = OFF;
count++;
continue;
}
if (count == SIZE)
return;
if (row != SIZE - 1)
setqueen(table, row + 1);
else
prtable(table);
}
}
void prtable(const int (*table)[SIZE])
{
int row, col;
for (row = 0; row < SIZE; row++) {
for (col = 0; col < SIZE; col++)
printf("%2d", table[row][col]);
putchar('\n');
}
putchar('\n');
}
int check(const int (*table)[SIZE], const int row, const int col)
{
int r = 0;
int c = 0;
for (r = 0; r < SIZE; r++)
if (r != row && table[r][col] == ON)
return ON;
for (c = 0; c < SIZE; c++)
if (c != col && table[row][c] == ON)
return ON;
for (r = row + 1, c = col + 1;
(r >= 0 && r < SIZE) || (c >= 0 && c < SIZE);
r++, c++)
if (table[r][c] == ON)
return ON;
for (r = row + 1, c = col - 1;
(r >= 0 && r < SIZE) || (c >= 0 && c < SIZE);
r++, c--)
if (table[r][c] == ON)
return ON;
for (r = row - 1, c = col + 1;
(r >= 0 && r < SIZE) || (c >= 0 && c < SIZE);
r--, c++)
if (table[r][c] == ON)
return ON;
for (r = row - 1, c = col - 1;
(r >= 0 && r < SIZE) || (c >= 0 && c < SIZE);
r--, c--)
if (table[r][c] == ON)
return ON;
return OFF;
}
在函数void setqueenint*table[SIZE]中,const int row应该执行静态int count=0,因为每次递归调用函数时,count都设置为0,即使板上放置了皇后
请参阅下面的代码以获取帮助
#include<stdio.h>
#include<stdlib.h>
#define N 8
#define true 1
#define false 0
void printsol(int sol[][N])
{
int i,j;
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
printf("%d ",sol[i][j]);
}
printf("\n");
}
}
int issafe(int sol[][N],int x,int y)
{
int i,j;
//Check Row
for(i=x,j=y-1;j>=0;j--)
{
if(sol[i][j]==1)
return false;
}
//Check Upper left diagonal
for(i=x-1,j=y-1;i>=0 && j>=0;i--,j--)
{
if(sol[i][j]==1)
return false;
}
//Check Lower Left Diagonal
for(i=x+1,j=y-1;i<N && j>=0;i++,j--)
{
if(sol[i][j]==1)
return false;
}
return true;
}
int solve(int sol[][N],int x,int y)
{
int i,j=y;
if(y==N)
{
printsol(sol);
return 1;
}
for(i=0;i<N;i++)
{
//printf("LOL");
if(issafe(sol,i,y)==true)
{
sol[i][y]=1;
if(solve(sol,0,y+1)==true)
{
return true;
}
else
sol[i][y]=0;
}
}
return false;
}
int main()
{
int sol[N][N];
int i,j;
for(i=0;i<N;i++)
for(j=0;j<N;j++)
sol[i][j]=0;
solve(sol,0,0);
}
你不会像你期望的那样重现。 setqueen调用setqueen,它会尝试所有的可能性,但如果失败,它只会尝试将Queen放在forc循环的同一行上,这将失败。 在setqueen开始时调用prtable以查看算法在做什么,您将看到: 然后,算法将尝试在第5行放置皇后,但失败,但不会尝试移动先前放置的皇后。 setqueen还应删除queen表[row][c]=OFF;当对setqueen的递归调用失败时,移动到下一步,因此它应该返回一个值。
除此之外,在我看来,count和c是一样的,你可以在for循环中初始化c,而不是在可读性更强之前,在检查每个for循环检查的列、行、…,中添加注释,并避免使用on和OFF for check返回值不清楚这意味着什么。我找不出问题所在。你都试了些什么?你是否验证了你的prtable函数被调用了?你在调试时发现了什么?我还是个新手,还没有学会如何使用gdb之类的调试工具,所以“我找不到问题”,我的意思是,我无法从逻辑上找出问题。欢迎来到so。现在正是尝试使用gdb或其他工具的最佳时机。即使您没有这样做,您也可以使用由来已久的添加printfs的方法,在寻求帮助之前更好地了解正在发生的事情。-您是否尝试过ragonci的代码和建议的修复方案?感谢您的解决方案和建议。
1 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0
0 0 0 0 1 0 0 0
0 1 0 0 0 0 0 0
0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0