-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathN_Queens.cpp
51 lines (48 loc) · 1.51 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
/**
The n-queens puzzle is the problem of placing n queens on an n x n chessboard such that no two queens attack each other.
Given an integer n, return all distinct solutions to the n-queens puzzle. You may return the answer in any order.
Each solution contains a distinct board configuration of the n-queens' placement, where 'Q' and '.' both indicate a queen and an empty space, respectively.
Input: n = 4
Output: [[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]]
**/
class Solution {
public:
vector<int>queens;
vector<string> numsToVString(int size){
vector<string>res(size,"");
for(int i = 0;i<size;i++){
res[i].resize(size,'.');
}
for(int i = 0;i<size;i++){
res[i][queens[i]]='Q';
}
return res;
}
bool safeAt(int row){
for(int i = 0;i<row;i++){
int diff = abs(queens[i]-queens[row]);
if (diff == 0 || diff == row-i)
return false;
}
return true;
}
void help(int row, int size, vector<vector<string>>&sol){
if(row == size){
sol.push_back(numsToVString(size));
return;
}
for(int i = 0;i<size;i++){
queens[row]=i;
if(safeAt(row)){
help(row+1,size,sol);
}
queens.pop_back();
}
}
vector<vector<string>> solveNQueens(int n) {
queens.resize(n,0);
vector<vector<string>>sol;
help(0,n,sol);
return sol;
}
};