-
Notifications
You must be signed in to change notification settings - Fork 0
/
Backtracking.cpp
93 lines (75 loc) · 1.44 KB
/
Backtracking.cpp
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
#include<iostream>
#include<stdlib.h>
#include<iomanip>
using namespace std;
int S,t=0;
static int cnt;
//void Nqueens(int chessBoard[][100],int,int);
//int spacePos(int chessBoard[][100],int,int);
void Display(int chessBoard[][100],int);
int spacePos(int chessBoard[][100] , int i , int j)
{
if(i==1)
return 1;
int a=i-1,b=j-1;
while(a>0 && b>0)
{
if(chessBoard[a--][b--]==1)
return 0;
}
a=i-1;
b=j+1;
while(a>0&&b<=S)
{
if(chessBoard[a--][b++]==1)
return 0;
}
for (a=1;a<i;a++)
{
if(chessBoard[a][j]==1)
return 0;
}
return 1;
}
void Nqueens(int chessBoard[][100],int N,int n)
{
if(n==N+1)
{
cnt++;
return ;
}
for (int i=1;i<=N;i++)
{
if(spacePos(chessBoard,n,i))
{
chessBoard[n][i] = 1;
Nqueens(chessBoard,N,n+1);
if(n==N)
Display(chessBoard,N);
}
chessBoard[n][i] = 0;
}
}
void Display(int chessBoard[][100] , int N)
{ t++;
cout << endl << endl << "Solution number is : " << t << endl;
for(int i=1;i<=N;i++)
{
for(int j=1;j<=N;j++)
{
cout << chessBoard[i][j] << " " ;
if(j==N)
cout << endl;
}
}
}
int main()
{
int N;
cout << "Enter the number of rows for square matrix : " ;
cin >> N;
S = N;
int chessBoard[100][100];
Nqueens(chessBoard,N,1);
return 0;
}