-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathN-Queens.h
More file actions
59 lines (55 loc) · 1.23 KB
/
N-Queens.h
File metadata and controls
59 lines (55 loc) · 1.23 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
class Solution {
public:
vector<vector<string>> solveNQueens(int n) {
for(int i = 0;i<n;i++)
board.push_back(-1); //initialization for values into -1.
backTrack(0,n);
return result;
}
private:
vector<int>board;
vector<vector<string>> result;
void backTrack(int k, int n)
{
for(int i = 0;i<n;i++) //for k rows, check all columns.
{
if(place(k,i) == true)
{
if(k==(n-1))
save_result(n);
backTrack(k+1,n);
}
}
}
bool place(int k,int i)
{
//check previous rows and cols for placed queens.
for(int row = 0; row<k;row++)
{
int col = board[row]; //placed queens(row,col)
if(k==row) return false;
if(i==col) return false;
if(abs(k-row) == abs(i-col)) return false;
}
board[k] = i;
return true;
}
void save_result(int n)
{
vector<string>ans;
for(int i = 0; i< n;i++)
{
int col = board[i];
string tmp;
for(int j = 0;j<n;j++)
{
if(j==col)
tmp.push_back('Q');
else
tmp.push_back('.');
}
ans.push_back(tmp);
}
result.push_back(ans);
}
};