-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathN-queens with c++
96 lines (90 loc) · 2.2 KB
/
N-queens with 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
class Solution {
public:
bool isSafe(int board[][10], int i, int j, int n) {
//colmun
for(int row=0;row<i;row++)
{
if(board[row][j]==1)
{
return false;
}
}
//left diagonal
int x=i;
int y=j;
while(x>=0 && y>=0)
{
if(board[x][y]==1)
{
return false;
}
x--;
y--;
}
//right diagonal
x=i;
y=j;
while(x>=0 && y<n)
{
if(board[x][y]==1)
{
return false;
}
x--;
y++;
}
return true;
}
bool NQueen(int board[][10],int i,int n, vector<vector<string>>& v) {
//base
if(i==n)
{
vector<string>curr;
for (int row=0;row<n;row++)
{
string rowStr;
for(int col=0;col<n;col++)
{
if(board[row][col]==1)
{
rowStr+='Q';
}
else
{
rowStr+='.';
}
}
curr.push_back(rowStr);
}
v.push_back(curr);
return false;
}
//rec case
//try to place queen in curr row and call on remaining
for(int j=0;j<n;j++)
{
//check if (i,j) is safe to place
if(isSafe(board,i,j,n)== true)
{
//place the queen, assuming (i,j) is right pos
board[i][j]=1;
bool NextQueen=NQueen(board,i+1,n, v);
if(NextQueen==true)
{
return true;
}
// out assumpton is wrong
board[i][j]=0;
}
}
// tried for all pos but could not place the queen
return false;
}
vector<vector<string>> solveNQueens(int n) {
int board[10][10];
vector<vector<string>>v;
vector<string> curr;
NQueen(board,0,n,v);
return v;
}
};