Untitled
raw download clone
C
views 27
,
size 990 b
#include<stdio.h>

int dia_L[30] = {0};
int dia_R[30] = {0};
int col[15] = {0};

int count = 0;

void check(int n,int row)
{
    if(row<=n)
    {
        for (int i = 1; i <= n; i++)
        {
            if ((col[i] == 0 && dia_L[i+row] == 0)&& dia_R[row-i+n]==0)
            {
                if (n==row)
                {
                    ++count;
                    //printf("<%d,%d,%d>\n",count,row,i);
                }else
                {
                    col[i] = 1;
                    dia_L[i+row] = 1;
                    dia_R[row-i+n] = 1;
                    //printf("<%d,%d,%d>\n",count,row,i);
                    check(n,row+1);
                    dia_R[row-i+n] = 0; 
                    dia_L[i+row] = 0;
                    col[i] = 0;
                }
            }
        }
    }
}

int main(int argc, char const *argv[])
{
    int n;
    scanf("%d",&n);
    check(n,1);
    printf("%d\n",count);
    return 0;
}
close fullscreen
Login or Register to edit or fork this paste. It's free.