我自己有一门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 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;
}
答案 0 :(得分:1)
你没有像你期望的那样递归
setqueen
调用setqueen
尝试所有可能性,但如果失败,则只是尝试将皇后放置在for(c)
循环的同一行,这将失败。
在prtable
开头调用setqueen
,看看算法在做什么,你会看到:
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
然后算法会尝试在第5行放置一个女王,但是没有尝试移动先前放置的女王。
当setqueen
的递归调用失败时,table[row][c] = OFF;
也应该删除女王setqueen
并转到下一个(因此它应返回一个值)。
除此之外,我认为count
和c
是相同的东西,你可以在for循环中而不是之前(更可读)初始化c,在检查中添加注释循环检查(列,行,...),并避免使用ON和OFF来检查返回值(不清楚这意味着什么)。
答案 1 :(得分:-1)
在函数void setqueen(int (*table)[SIZE], const int row)
中,你应该static 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);
}