-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
54 lines (45 loc) · 1.2 KB
/
test.cpp
File metadata and controls
54 lines (45 loc) · 1.2 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
#include <bits/stdc++.h>
using namespace std;
bool isvalid(vector<vector<int>>& path, int row, int col) {
// int tmp = path[row][col];
for (int i = 0; i < row; ++i) {
if (path[i][col] == 1) return false;
}
// 左对角线
for (int i = row - 1, j = col -1; i >= 0 && j >= 0; i--, j--) {
if (path[i][j] == 1) return false;
}
for (int i = row - 1, j = col + 1; i >= 0 && j < path[0].size(); i--, j++) {
if (path[i][j] == 1) return false;
}
return true;
}
void backtracking(int startIdx, int n, int cnt, vector<vector<int>>& path) {
if (cnt == n ) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cout << path[i][j] << " ";
}
cout << endl;
}
cout << endl;
return;
}
for (int i = 0; i < n; ++i) {
path[cnt][i] = 1;
if (isvalid(path, cnt, i)) {
backtracking(i + 1, n, cnt + 1, path);
}
path[cnt][i] = 0;
}
}
void isNqueen(int n) {
// vector<vector<int>> res;
vector<vector<int>> path(n, vector<int>(n));
backtracking(0, n, 0, path);
return;
}
int main() {
isNqueen(4);
return 0;
}