在C中修改了N-Queens

时间:2015-11-24 08:38:15

标签: c

我试图将N-Queen拼图解算器修改为N-Empress解算器(其中的部件可以像车和骑士一样移动)

代码以不相互威胁的方式放置(或至少试图放置)大臣。并回溯打印所有可能的解决方案。但是,我无法输出正确数量的解决方案。它输出的当前输出是正确的,但它并没有输出所有输出。不知道我错过了什么条件。

#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);
        }
    }
}

2 个答案:

答案 0 :(得分:1)

地方法似乎并不涵盖所有情况。在骑士移动中,列的差异和行的差异总计为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
}

答案 1 :(得分:0)

针对骑士攻击的检查涉及测试与当前攻击相关的四个瓦片。 (有八种可能的骑士动作,但你只需要查看已经放置过大师的行。)

column中的

,您探测了图块rowboard[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;
}

虽然您需要检查所有行以攻击Rooks,但是对Knight移动的检查只进行一次。所以:

<?php
//just set a header before output
header('Content-Type:application/json;');
echo json_encode($myArray);