-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathp3.cpp
More file actions
63 lines (54 loc) · 1.13 KB
/
Copy pathp3.cpp
File metadata and controls
63 lines (54 loc) · 1.13 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
55
56
57
58
59
60
61
62
63
#include "ADTStack.cpp"
#include<cmath>
using namespace std;
const int N=8;
int line[N];
int tot;
void show(){
cout<<"No. "<<(++tot)<<":\n";
for(int i=0;i<N;i++) for(int j=0;j<N;j++)
cout<<"+@"[line[i]==j]<<" \n"[j==N-1];
cout<<endl;
}
bool valid(int i,int j){
for(int k=0;k<i;k++)
if(line[k]==j or abs(line[k]-j)==i-k)
return false;
return true;
}
struct stack_status{
int i,j,pc;
};
void solve(){
Stack<stack_status>s; s.push({0,-1,0});
while(s.length()){
stack_status tmp; s.pop(tmp);
auto &[i,j,pc]=tmp;
switch(pc){
case 0:
if(i>=N) pc=1;
else pc=2;
s.push(tmp);
break;
case 1:
show();
break;
case 2:
for(++j;j<N;j++)
if(valid(i,j)){
line[i]=j;
s.push(tmp);
s.push({i+1,-1,0});
break;
}
break;
default:
cerr<<"invaild pc!";
exit(-1);
}
}
}
int main(){
solve();
return 0;
}