-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBacktracking_queen.c
61 lines (56 loc) · 1.2 KB
/
Backtracking_queen.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
58
59
60
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
bool isAttacking(int size, int b[size][size], int r, int c){
for(int i=0; i<size; i++){
for(int j=0; j<size; j++){
if(i==r || j==c || (i+j)==(r+c)||(i-j)==(r-c)){
if(b[i][j]==1)
return true;
}
}
}
return false;
}
bool N_Queen(int size, int b[size][size], int N){
if(N==0){
return true;
}
for(int i=0; i<size; i++){
for(int j=0; j<size; j++){
if(isAttacking(size, b, i, j)){
continue;
}
b[i][j]=1;
if(N_Queen(size, b, N-1)){
return true;
}
b[i][j]=0;
}
}
return false;
}
void display(int size, int a[size][size]){
for(int i=0; i<size; i++){
for(int j=0; j<size; j++){
printf("%d ", a[i][j]);
}
printf("\n");
}
}
int main()
{
int N;
scanf("%d", &N);
int size=N;
int b[N][N];
memset(b, 0, sizeof(b[0][0]) * size * size);
if(N_Queen(size, b, N)){
printf("YES\n");
display(size, b);
}
else{
printf("NO\n");
}
return 0;
}