-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnqueen.c
More file actions
54 lines (42 loc) · 1.12 KB
/
nqueen.c
File metadata and controls
54 lines (42 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include<stdio.h>
int a[20];
void nq(int a[],int n,int r)
{
int i,j,legal;
if(r==n+1&&n>3)
{
for(i=1;i<=n;i++)
{
printf("%d",a[i]);
}
printf("\n\n");
}
else
{
for(j=1;j<=n;j++)
{
legal=1;
for(i=1;i<=r-1;i++)
{
if((a[i]==j)||(a[i]-i)==(r-j))||(a[i]+i)==(r+j)))
{
legal=0;
}
}
if(legal==1)
{
a[r]=j;
nq(a,n,r+1);
}
}
}
}
int main()
{
int n;
int row=1;
printf("enter board size\n");
scanf("%d",&n);
nq(a,n,row);
return 0;
}