-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathASAPcpp_project.cpp
More file actions
131 lines (91 loc) · 1.56 KB
/
ASAPcpp_project.cpp
File metadata and controls
131 lines (91 loc) · 1.56 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
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
130
131
#include<iostream>
using namespace std;
int c=0;
bool isPossible(int board[100][100],int n,int i,int j)
{
//vertical case
for(int m=i-1;m>=0;m--)
{
if(board[m][j]==1)
{
return false;
}
}
//right diagonal
int q=i-1;
int r=j+1;
while(q>=0 and r<n)
{
if(board[q][r]==1)
{
return false;
}
q--;
r++;
}
//left diagonal
int u=i-1;
int v=j-1;
while(u>=0 and v>=0)
{
if(board[u][v]==1)
{
return false;
}
u--;
v--;
}
return true; //if all are not cutting
}
bool nqueen(int board[100][100],int n,int i)
{
//base case
if(i==n)
{
//print solution
cout<<endl;
c++;
for(int l=0;l<n;l++)
{
for(int m=0;m<n;m++)
{
cout<<board[l][m]<<" ";
}
cout<<endl;
}
return false;
}
//recursive case
for(int j=0;j<n;j++)
{
board[i][j]=1;
if(isPossible(board,n,i,j)==true)
{
bool curresult=nqueen(board,n,i+1);
if(curresult==true)
{
return true;
}
}
board[i][j]=0;//backtrack
}
return false;
}
int main()
{
int board[100][100]={0};
int n;
cout<<"Enter size of the board ";
cin>>n;
nqueen(board,n,0);
if(c>0)
{
cout<<"Yes solution Exist";
cout<<"\nNumber of total solutions "<<c;
}
else
{
cout<<"No solution Exist ";
}
return 0;
}