-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheight_queens.cpp
122 lines (113 loc) · 2.62 KB
/
eight_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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <iostream>
#include <stdio.h>
#include <vector>
#include <stack>
#include <string>
#include <string.h>
#include <algorithm>
using namespace std;
#define N 8
int m[N+1][N+1];
vector<int> container;
typedef pair<int, int> P;
void addOne(int i, int j) {
int a, b;
for (a = 1; a<=N; a++)
if (a!=i)
m[a][j]++;
for (b=1; b<=N; b++)
if (b!=j)
m[i][b]++;
for (a=i-1, b=j-1; a>0 && b>0; a--, b--)
m[a][b]++;
for (a=i+1, b=j+1; a<=N && b<=N; a++, b++)
m[a][b]++;
for (a=i-1, b=j+1; a>0 && b<=N; a--, b++)
m[a][b]++;
for (a=i+1, b=j-1; a<=N && b>0; a++, b--)
m[a][b]++;
m[i][j]++;
}
void delH(int i, int j) {
if (m[i][j] > 0)
m[i][j]--;
}
void delOne(int i, int j) {
int a, b;
for (a = 1; a<=N; a++)
if (a!=i)
delH(a,j);
for (b=1; b<=N; b++)
if (b != j)
delH(i,b);
for (a=i-1, b=j-1; a>0 && b>0; a--, b--)
delH(a,b);
for (a=i+1, b=j+1; a<=N && b<=N; a++, b++)
delH(a,b);
for (a=i-1, b=j+1; a>0 && b<=N; a--, b++)
delH(a,b);
for (a=i+1, b=j-1; a<=N && b>0; a++, b--)
delH(a,b);
delH(i, j);
}
void solution() {
int a, j, step = 0;
bool direction = true; // true: forward, false: back
stack<P> path;
path.push(P(1, 1));
addOne(1, 1);
step = 1;
do {
P pre = path.top();
a = pre.first;
j = pre.second;
// cout<<a<<", "<<j<<endl;
if (a == N && direction) {
container.push_back(step);
// cout<<step<<endl;
direction = false;
}
else {
if (direction) {
a = a+1;
j = 1;
}
else {
path.pop();
delOne(a, j);
step /= 10;
j++;
}
for (; j<=N; j++) {
if (!m[a][j]) {
path.push(P(a, j));
addOne(a, j);
step = step*10+j;
direction = true;
break;
}
}
if (j > N) direction = false;
}
} while (!path.empty());
}
void dfs(int step, int i) {
if (i>N) {
container.push_back(step);
return;
}
for (int j=1; j<=N; j++)
if (!m[i][j]) {
addOne(i, j);
dfs(step*10+j, i+1);
delOne(i, j);
}
}
int main() {
// dfs(0, 1);
solution();
for (int i=0; i<container.size(); i++)
cout<<container[i]<<endl;
cout<<container.size()<<endl;
return 0;
}