-
Notifications
You must be signed in to change notification settings - Fork 0
/
QUEEN.CPP
77 lines (69 loc) · 1.29 KB
/
QUEEN.CPP
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include<stdio.h>
#include<conio.h>
#include<string.h>
// Structure Position
struct cordinates {
int row;
int col;
};
// solved N Queen Problem
int solveNQueen(int n,int row,struct cordinates *cor){
if(n==row){
return 1;
}
else
{
for( int col =0; col<n ; col++){
int safe = 1;
for(int queen =0; queen < row; queen++){
if((cor+queen)->col == col || (cor+queen)->row - (cor+queen)->col == row-col || (cor+queen)->row + (cor+queen)->col == row+col){
safe = 0;
break;
}
}
if(safe){
(cor+row)->row = row;
(cor+row)->col = col;
if(solveNQueen(n,row+1,cor)){
return 1;
}
}
}
}
return 0;
}
//Display
void display(int n,struct cordinates *cor){
printf("Co-Ordinate For Queen Placement.\n");
for(int i =0; i<n ; i++){
printf("[ %d , %d] \n",(cor+i)->row,(cor+i)->col);
}
}
int main(){
int k;
int j=1;
while(j!=2){
printf("1)Queen game\n");
printf("2)Logout\n");
scanf("%d",&j);
switch(j){
case 1:
printf("\nEnter Numbers Of Queen:\n");
scanf("%d",&k);
if(k==2){
printf("No solution possible\n\n\n");
break;
}
struct cordinates cor[50];
solveNQueen(k,0,cor);
display(k,cor);
j=1;
break;
case 2:
j=2;
break;
default:
printf("Not an Option\n");
}
}
}