C中的修正N皇后

C中的修正N皇后,c,C,我正在尝试将N-Queen谜题解算器修改为N-Empress谜题解算器(其中的棋子可以像rook和knight一样移动) 《准则》规定(或至少试图规定)总理们不会相互威胁。和回溯以打印所有可能的解决方案。但是,我无法让它输出正确数量的解决方案。它输出的当前值是正确的,但它不会输出所有值。不确定我错过了什么情况 #include<stdio.h> #include<math.h> /* N=4:8 Solutions N=5:20 Solutions N=

我正在尝试将N-Queen谜题解算器修改为N-Empress谜题解算器(其中的棋子可以像rook和knight一样移动)

《准则》规定(或至少试图规定)总理们不会相互威胁。和回溯以打印所有可能的解决方案。但是,我无法让它输出正确数量的解决方案。它输出的当前值是正确的,但它不会输出所有值。不确定我错过了什么情况

#include<stdio.h>
#include<math.h>
 /*
 N=4:8    Solutions
 N=5:20   Solutions
 N=8:2766 Solutions
 */
int board[20],count;

int main()
{
    int n,i,j,numPuzzle;
    void queen(int row,int n);
    printf("Enter Number of Queens:");
    scanf("%d", &n);
    queen(1,n);
    return 0;
}

//function for printing the solution
void print(int n)
{
    int i,j;
    printf("\n\nSolution %d:\n\n",++count);

    for(i=1;i<=n;++i)
        printf("\t%d",i);

    for(i=1;i<=n;++i)
    {
        printf("\n\n%d",i);
        for(j=1;j<=n;++j) //for nxn board
        {
            if(board[i]==j)
                printf("\tQ"); //queen at i,j position
            else
                printf("\t-"); //empty slot
        }
    }
}

/*funtion to check conflicts
If no conflict for desired postion returns 1 otherwise returns 0*/
int place(int row,int column)
{
int i;
    for(i=1;i<=row-1;++i)
    {
    //checking column and digonal conflicts
        //printf("\nboard[i]=%d column=%d\n",board[i],column);

        if(board[i]==column)
        {
        return 0;
        }

        if( (abs(board[i]-(column+3))==abs(i-row))  )
        {
        return 0;
        }

        if( (abs(board[i]-(column-3))==abs(i-row))  )
        {
        return 0;
        }

        if( (abs(board[i]+(column-3))==abs(i-row))  )
        {
        return 0;
        }

        if( (abs(board[i]+(column+3))==abs(i-row))  )
        {
        return 0;
        }


    }
    return 1; //no conflicts
}

//function to check for proper positioning of queen
void queen(int row,int n)
{
    int column;

    for(column=1;column<=n;++column)
    {
        if(place(row,column))
        {
            board[row]=column; //no conflicts so place queen
            if(row==n) //dead end
            print(n); //printing the board configuration
            else //try queen with next position
            queen(row+1,n);
        }
    }
}
#包括
#包括
/*
N=4:8溶液
N=5:20溶液
N=8:2766溶液
*/
int板[20],计数;
int main()
{
int n,i,j,numPuzzle;
无效皇后(国际行,国际n);
printf(“输入皇后数量:”);
scanf(“%d”和“&n”);
皇后(1,n);
返回0;
}
//用于打印解决方案的函数
无效打印(int n)
{
int i,j;
printf(“\n\n解决方案%d:\n\n”++计数);

对于(i=1;i针对骑士攻击的检查包括测试与当前一个相关的四个棋盘。(当然,有八种可能的骑士招式,但你只需查看你已经放置了大臣的棋盘。)

place
中,您将探测平铺
,因此您应该进行检查

board[row - 1] != column ± 2     (only if row -1 is on the board)
board[row - 2] != column ± 1     (only if row - 2 is on the board)
当你需要检查所有行的攻击车时,骑士移动的检查只做一次。因此:

int place(int row, int column)
{
    int i;

    if (row > 1 && abs(column - board[row - 1]) == 2) return 0;
    if (row > 2 && abs(column - board[row - 2]) == 1) return 0;

    for (i = 1; i < row; ++i) {
        if (board[i] == column) return 0;
    }

    return 1;
}
int位置(int行,int列)
{
int i;
if(row>1&&abs(column-board[row-1])==2)返回0;
if(row>2&&abs(column-board[row-2])==1)返回0;
对于(i=1;i
放置方法似乎并不能覆盖所有情况。在骑士招式中,列差和行差总计为3

int place(int row,int column)
{
int i;
    for(i=1;i<=row-1;++i)
    {
    //checking column and digonal conflicts
        //printf("\nboard[i]=%d column=%d\n",board[i],column);

        if(board[i]==column)
        {
        return 0;
        }

        if(abs(board[i]-column)+abs(row-i)==3  )
        {
        return 0;
        }
    }
    return 1; //no conflicts
}
int位置(int行,int列)
{
int i;

对于(i=1;i以下内容是什么?if((abs(board[i]-(column+3))==abs(i-row)){return 0;}我正试图捕捉骑士在这种情况下的移动。我不确定这是否正确,因为骑士移动检查需要两个坐标相等比较。你在这里肯定做错了。怎么办?我很困惑,过去4个小时我一直在研究这种情况。哦,我的天啊,谢谢!我一直在想f在解决方案一直如此简单的情况下添加常量和内容!