-2

我自己有一个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;
}
4

2 回答 2

1

你不会像你期望的那样递归。
setqueen尝试所有可能性的调用setqueen,但如果失败,它只会尝试将皇后放在for(c)循环中的同一行,这将失败。在开头
调用看看算法在做什么,你会看到:prtablesetqueen

 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(因此它应该返回一个值)。

除此之外,在我看来,这countc同一件事,您可以在 for 循环中初始化 c 而不是之前(更具可读性),在检查每个 for 循环检查的内容(列,行,... ),并避免使用 ON 和 OFF 来检查返回值(不清楚这意味着什么)。

于 2014-06-30T08:57:42.793 回答
-1

In function void setqueen(int (*table)[SIZE], const int row) you should do static int count=0 because every time the function is called recursively count is set to 0 even if there are queens placed on board.

See the below code for assistance

#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);
}
于 2014-06-30T08:48:22.613 回答