-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathN-Queens.txt
63 lines (54 loc) · 1.28 KB
/
N-Queens.txt
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
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.
Given an integer n, return all distinct solutions to the n-queens puzzle.
Each solution contains a distinct board configuration of the n-queens' placement, where 'Q' and '.' both indicate a queen and an empty space respectively.
For example,
There exist two distinct solutions to the 4-queens puzzle:
[
[".Q..", // Solution 1
"...Q",
"Q...",
"..Q."],
["..Q.", // Solution 2
"Q...",
"...Q",
".Q.."]
]
-------------------------------------------------------------
bool check(int row, int *place)
{
for(int i = 0; i < row; ++i)
{
int diff = abs(place[i] - place[row]);
if(diff == 0 || diff == row - i)
return false;
}
return true;
}
void placeQueens(int row, int n, int *place, vector<vector<string> > &ret)
{
if(row == n)
{
vector<string> tmp;
for(int i = 0; i < row; ++i)
{
string s(n, '.');
s[place[i]] = 'Q';
tmp.push_back(s);
}
ret.push_back(tmp);
return;
}
for(int i = 0; i < n; ++i)
{
place[row] = i;
if(check(row, place))
placeQueens(row + 1, n, place, ret);
}
}
vector<vector<string> > solveNQueens(int n)
{
vector<vector<string> > ret;
int *place = new int[n];
placeQueens(0, n, place, ret);
return ret;
}