C 8皇后对角线检查

C 8皇后对角线检查,c,n-queens,C,N Queens,目前,我正在创建一个程序,要求用户为8皇后问题放置皇后。现在我几乎已经创建了这个程序,但是我一直在研究如何让程序按对角线进行检查 这是(未完成的)代码: #包括 #包括 #包括 整数校验(整数**象棋,整数*p,整数N,整数M) { 整数倍=0; 对于(int i=0;i检查皇后放置的对角线的逻辑是(p,q)检查所有i位于(p+i,q+i)的所有位置,其中p+i和q+i都在板内。对于负极,使用(p-i,q-i)。“我卡住了”不是一个很好的问题描述。你的问题具体是什么?你尝试过什么吗?以什么方式不

目前,我正在创建一个程序,要求用户为8皇后问题放置皇后。现在我几乎已经创建了这个程序,但是我一直在研究如何让程序按对角线进行检查

这是(未完成的)代码:

#包括
#包括
#包括
整数校验(整数**象棋,整数*p,整数N,整数M)
{
整数倍=0;

对于(int i=0;i检查皇后放置的对角线的逻辑是(p,q)检查所有i位于(p+i,q+i)的所有位置,其中p+i和q+i都在板内。对于负极,使用(p-i,q-i)。

“我卡住了”不是一个很好的问题描述。你的问题具体是什么?你尝试过什么吗?以什么方式不起作用?
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int check_r_c(int**chess,int*p,int N,int M)
{
    int times=0;
    for(int i=0; i<N; i++)
    {
        times=0;
        for(int j=0; j<M; j++)
        {
            if(chess[i][j] == 1)
                times++;

        }
        if( times != 1)
        {
            *p=1;
            return 1;
        }
    }
    for(int j=0; j<M; j++)
    {
        times=0;
        for(int i=0; i<N; i++)
        {
            if(chess[i][j] == 1)
                times++;
        }
        if( times != 1)
        {
            *p=1;
            return 1;
        }
    }
    *p=0;
    return 0;
}
int main()
{
    int N,M;
    printf("Give the number of rows: \n");
    scanf("%d",&N);
    printf("Give the number of columns: \n");
    scanf("%d",&M);
    int**chess = malloc(N*sizeof(int));
    int i,j,ch;
    int*ptr;
    ptr=&ch;
    if(chess==NULL)
    {
        printf("\nMemory cannot be allocated\n");
        return 1;
    }
    for(i=0; i<N; i++)
    {
        if((chess[i] = malloc(M*sizeof(int)))==NULL)
        {
            printf("\nMemory cannot be allocated\n");
            return 1;
        }
    }
    for(int i=0; i<N; i++)
        for(int j=0; j<M; j++)
            chess[i][j]= 0;
    for(int k=0; k<N; k++)
    {
        printf("Give the position of the %d queen\n",k+1);
        scanf("%d",&i);
        scanf("%d",&j);
        if(chess[i][j] == 1)
        {
            printf("You cant put 2 queens in the same place!!!\n" );
            return 0;
        }
        chess[i][j] = 1;
    }
    check_r_c(chess,ptr,N,M);
    if(ch == 0)
        printf("Solution is correct!");
    else
        printf("Solution is incorrect!");
    for(int i=0; i<N; i++)
        free(chess[i]);
    free(chess);
}