-
Notifications
You must be signed in to change notification settings - Fork 0
/
nqueen.c
57 lines (57 loc) · 1.47 KB
/
nqueen.c
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
54
55
56
57
#include<stdio.h>
#include<string.h>
int main()
{
int **ptr,rs=4,cm=4,rows=0,cols=0,ro,cs,r,c,i,flag=0,diagonal=0;
ptr=(int **)malloc(r * sizeof(int*));
for(i=0;i<rs;i++)
ptr[i]=(int*)malloc(c*sizeof(int));
ptr[0][3]=1;
ptr[1][2]=1;
ptr[2][1]=1;
ptr[3][0]=1;
for(r=0;r<rs;r++){
rows=0;
cols=0;
diagonal=0;
for(c=0;c<cm;c++){
if(ptr[r][c]==0)
rows++;
if(ptr[c][r]==0)
cols++;
if(r==0 && c==0 && ptr[r][c]==1){
if(ptr[r+c][c]==1)
diagonal++;
}
if(r==0 && c==(cm-1) && ptr[r][c]==1){
if(ptr[r+c][(cm-1)-c]==1)
diagonal++;
}
if(r==(rs-1) && c==0 && ptr[r][c]==1){
if(ptr[r-c][c]==1)
diagonal++;
}
if(r==(rs-1) && c==(cm-1) && ptr[r][c]==1){
if(ptr[r-c][(cs-1)-c]==1)
diagonal++;
}
}
if(rows==3 && cols==3 && diagonal==1 )
continue;
else{
flag=1;
break;
}
}
for(ro=0;ro<rs;ro++){
for(cs=0;cs<cm;cs++){
printf("%d ",ptr[ro][cs]);
}
printf("\n");
}
if(flag)
printf("Invalid");
else
printf("Valid");
return 0;
}