12179 - Queens
raw download clone
C
views 24
,
size 711 b
#include <stdio.h>

void queen(int x);

int n, ans = 0;
int y[15], diagonal1[30], diagonal2[30];

int main(void)
{
    scanf("%d", &n);
    queen(1);
    printf("%d\n", ans);

    return 0;
}

void queen(int x)
{
    if(x == n + 1)
    {
        ans++;
        return;
    }
    else
    {
        for(int i=1; i<=n; i++)
        {
            if(y[i] + diagonal1[x-i+15] + diagonal2[x+i] == 0)
            {
                y[i] = 1;
                diagonal1[x-i+15] = 1;
                diagonal2[x+i] = 1;
                queen(x+1);
                y[i] = 0;
                diagonal1[x-i+15] = 0;
                diagonal2[x+i] = 0;
            }
        }
    }
}
close fullscreen
Login or Register to edit or fork this paste. It's free.