-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8queensnew.cpp
More file actions
91 lines (85 loc) · 2.01 KB
/
8queensnew.cpp
File metadata and controls
91 lines (85 loc) · 2.01 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
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
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int chess_board[20], count;
int display(int);
void queen_function(int, int);
int placeholder(int, int);
void queen_function(int row_value, int limit)
{
int column_value;
for(column_value = 1; column_value <= limit; column_value++)
{
if(placeholder(row_value, column_value))
{
chess_board[row_value] = column_value;
if(row_value == limit)
{
display(limit);
}
else
{
queen_function(row_value + 1, limit);
}
}
}
}
int placeholder(int row_value, int column_value)
{
int count;
for(count = 1; count <= row_value - 1; count++)
{
if(chess_board[count] == column_value)
{
return 0;
}
else
{
if(abs(chess_board[count] - column_value) == abs(count - row_value))
{
return 0;
}
}
}
return 1;
}
int display(int limit)
{
int m, n;
printf("\n\n\tPossible Solution %d:\n\n", ++count);
for(m = 1; m <= limit; m++)
{
printf("\t[%d]", m);
}
for(m = 1; m <= limit; m++)
{
printf("\n\n[%d]", m);
for(n = 1; n <= limit; n++)
{
if(chess_board[m] == n)
{
printf("\tQ");
}
else
{
printf("\t*");
}
}
}
}
int main()
{
int limit;
printf("\nEnter Number of Queens:\t");
scanf("%d", &limit);
if(limit <= 3)
{
printf("\nNumber should be greater than 3 to form a Matrix\n");
}
else
{
queen_function(1, limit);
}
printf("\n\n");
return 0;
}