-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathnqueen.c
More file actions
executable file
·61 lines (51 loc) · 1.12 KB
/
nqueen.c
File metadata and controls
executable file
·61 lines (51 loc) · 1.12 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAXSIZE 10
bool place(int board[], int row, int column);
void print(int board[], int N);
unsigned nqueen(int row, int N);
int main()
{
int N;
printf("Enter N: ");
scanf("%d", &N);
int nSols = nqueen(1,N);
printf("Total solutions= %d\n", nSols);
}
bool place(int board[], int row, int column)
{
for(int i = 1; i < row; i++)
{
if( (board[i] == column) || \ // if column is same
abs(board[i]-column) == abs(i-row) ) // if present diagonally
return false; // can't be placed!
}
return true; // if no comflict, can be placed.
}
void print(int board[], int N)
{
for(int i = 0; i < N; i++)
printf("%d ", board[i]);
printf("\n");
}
unsigned nqueen(int row, int N)
{
static unsigned nSolutions = 0;
static unsigned board[MAXSIZE];
for(int column = 1; column <= N; column++)
{
if(place(board, row, column))
{
board[row]=column; // place it if can be placed
if(row == N) // if all rows filled, print the solution
{
print(board, N);
nSolutions++;
}
else
nqueen(row+1, N); // otherwise backtrack
}
}
return nSolutions;
}