-
Notifications
You must be signed in to change notification settings - Fork 0
/
N_Queens.c
53 lines (53 loc) · 845 Bytes
/
N_Queens.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
#include<stdio.h>
#define n 4
char mat[n][n];
int marked_column(int row){
int i;
for(i=0;i<n;i++){
if(mat[row][i] == 'Q')
return i;
}
}
int safe(int row,int col){
int i,j,mark;
//we need to get the marked col for that row and check if can be placed or not
for(i=0;i<n;i++){
mark = marked_column(i);
if( mark==col || abs(row-i)==abs(col-mark) )
return 0;
}
return 1;
}
void nqueens(int k){//k=current queen or row
int i,j;
if(k<n){
for(i=0;i<n;i++){
if( safe(k,i) ){
mat[k][i] = 'Q';
nqueens(k+1);
mat[k][i] = '.';
}
}
}
else{
printf("\nSolution:\n");
print_mat();
}
}
void print_mat(){
int i,j;
for(i=0;i<n;i++){
for(j=0;j<n;j++){
printf("%c\t",mat[i][j]);
}
printf("\n");
}
}
int main(){
int i,j;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
mat[i][j] = '.';
nqueens(0);
return 0;
}