-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8960 - N Queens Problem.c
129 lines (96 loc) · 3.5 KB
/
8960 - N Queens Problem.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/**********************************************************************************************************
NAME: CANDIDA RUTH NORONHA
CLASS: SE COMPS B
ROLL NO. : 8960
BATCH: C
TITLE: N QUEENS PROBLEM
SUBMISSION DATE : 12th APRIL, 2021
**********************************************************************************************************/
#include<stdio.h>
#include<math.h>
int board[20], count;
int main()
{
int n, i, j;
void queen(int row, int n);
printf("\n------------------------------------------------------------------------------------------------\n");
printf("\n\t\t\t\t N Queens Problem Using Backtracking\n");
printf("\n------------------------------------------------------------------------------------------------\n");
printf("\n Enter the Number of Queens : ");
scanf("%d", &n);
printf("\n------------------------------------------------------------------------------------------------\n");
queen(1, n);
return 0;
}
//function for printing the solution
void print(int n)
{
int i, j;
printf("\n Solution %d : \n\n", ++count);
for (i = 1; i <= n; ++i)
printf("\t%d", i);
for (i = 1; i <= n; ++i)
{
printf("\n\n%d", i);
for (j = 1; j <= n; ++j) //for nxn board
{
if (board[i] == j)
printf("\tQ"); //queen at i,j position
else
printf("\t-"); //empty slot
}
}
printf("\n------------------------------------------------------------------------------------------------\n");
}
/*function to check conflicts If no conflict for desired position returns 1 otherwise returns 0*/
int place(int row, int column)
{
int i;
for (i = 1; i <= row - 1; ++i) {
//checking column and diagonal conflicts
if (board[i] == column)
return 0;
else
if (abs(board[i] - column) == abs(i - row))
return 0;
}
return 1; //no conflicts
}
//function to check for proper positioning of queen
void queen(int row, int n)
{
int column;
for (column = 1; column <= n; ++column)
{
if (place(row, column))
{
board[row] = column; //no conflicts so place queen
if (row == n) //dead end
print(n); //printing the board configuration
else //try queen with next position
queen(row + 1, n);
}
}
}
/**********************************************************************************************************
OUTPUT:
------------------------------------------------------------------------------------------------
N Queens Problem Using Backtracking
------------------------------------------------------------------------------------------------
Enter the Number of Queens : 4
------------------------------------------------------------------------------------------------
Solution 1 :
1 2 3 4
1 - Q - -
2 - - - Q
3 Q - - -
4 - - Q -
------------------------------------------------------------------------------------------------
Solution 2 :
1 2 3 4
1 - - Q -
2 Q - - -
3 - - - Q
4 - Q - -
------------------------------------------------------------------------------------------------
**********************************************************************************************************/