-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathn_queens.c
70 lines (63 loc) · 1.9 KB
/
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// https://www.hackerearth.com/practice/basic-programming/recursion/recursion-and-backtracking/tutorial/
#include <stdio.h>
#include <stdlib.h>
int is_attacked(int x, int y, int **chess_board, int chess_board_size) {
for (int i=0; i<chess_board_size; i++) { // check if other Queens are in same row or column
if (chess_board[x][i] || chess_board[i][y]) {
return 1;
}
}
for (int i=0; i<chess_board_size; i++) {
for (int j=0; j<chess_board_size; j++) {
if ((x+y == i+j) || (x-y == i-j)) {
if (i == x && j == y) {
continue;
}
if (chess_board[i][j] == 1) {
return 1;
}
}
}
}
return 0;
}
int n_queens(int **chess_board, int chess_board_size, int queens_count) {
if (queens_count == 0) {
return 1;
}
for (int i=0; i<chess_board_size; i++) {
for (int j=0; j<chess_board_size; j++) {
if ((!is_attacked(i, j, chess_board, chess_board_size)) && (chess_board[i][j] != 1)) {
chess_board[i][j] = 1;
if (n_queens(chess_board, chess_board_size, queens_count-1)) {
return 1;
}
chess_board[i][j] = 0;
}
}
}
return 0;
}
int main(){
int n, **chess_board;
scanf("%d", &n);
chess_board = (int **) calloc(n, sizeof(int **));
for (int i=0; i<n; i++) {
chess_board[i] = (int *) calloc(n, sizeof(int *));
}
if (chess_board == NULL) {
printf("calloc failed\n");
return 1;
}
if (n_queens(chess_board, n, n)) {
printf("YES\n");
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
printf("%d ", chess_board[i][j]);
}
printf("\n");
}
} else {
printf("NO\n");
}
}