-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathn - queens.cpp
55 lines (52 loc) · 1.39 KB
/
n - queens.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
class Solution {
public:
vector<vector<string>> ans;
//This is to check if the given row and column are safe for queen to sit.
bool isSafe(int r, int c, int n, vector<vector<int>> &v){
for(int i=0;i<c;i++)
{
if(v[r][i])
return 0;
}
for(int i=r,j=c;i>=0&&j>=0;i--,j--)
{
if(v[i][j])
return 0;
}
for(int i=r,j=c;j>=0&&i<n;i++,j--)
{
if(v[i][j])
return 0;
}
return 1;
}
bool rec(int c, int n, vector<vector<int>> &v) {
if(c==n) return 1;
for(int i=0;i<n;i++)
{
if(isSafe(i,c,n,v)){
v[i][c]=1;
if(rec(c+1,n,v)){
vector<string> v2(n,string(n,'.'));
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(v[i][j]==1)
v2[i][j]='Q';
}
}
ans.push_back(v2);
}
v[i][c]=0;
}
}
return 0;
}
vector<vector<string>> solveNQueens(int n) {
vector<vector<int>> v(n,vector<int>(n,0));
// We will start from col=0 here:
bool an=rec(0,n,v);
return ans;
}
};