-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetcode第51题.cpp
More file actions
42 lines (42 loc) · 1.1 KB
/
Leetcode第51题.cpp
File metadata and controls
42 lines (42 loc) · 1.1 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
class Solution {
private:
vector<string>path;
vector<vector<string>> ans;
string template_row;
bool Column_used[9];
bool Hypo_used[18];
bool Hypo_right_used[18];
int ROW;
void BackTracking(int row=0)
{
if(row==ROW){
ans.push_back(path);
return;
}
string row_string=template_row;
for(int i=0;i<ROW;++i)
{
if(!Column_used[i]&&!Hypo_used[ROW+row-i]
&&!Hypo_right_used[row+i]){
row_string[i]='Q';
Column_used[i]=true;
Hypo_used[ROW+row-i]=true;
Hypo_right_used[row+i]=true;
path.push_back(row_string);
BackTracking(row+1);
row_string[i]='.';
Column_used[i]=false;
Hypo_used[ROW+row-i]=false;
Hypo_right_used[row+i]=false;
path.pop_back();
}
}
}
public:
vector<vector<string>> solveNQueens(int n) {
template_row=string(n,'.');
ROW=n;
BackTracking();
return ans;
}
};