forked from Hawstein/cracking-the-coding-interview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8.8.cpp
36 lines (35 loc) · 722 Bytes
/
8.8.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
#include <iostream>
using namespace std;
int c[20], n=8, cnt=0;
void print(){
for(int i=0; i<n; ++i){
for(int j=0; j<n; ++j){
if(j == c[i]) cout<<"1 ";
else cout<<"0 ";
}
cout<<endl;
}
cout<<endl;
}
void search(int r){
if(r == n){
print();
++cnt;
return;
}
for(int i=0; i<n; ++i){
c[r] = i;
int ok = 1;
for(int j=0; j<r; ++j)
if(c[r]==c[j] || r-j==c[r]-c[j] || r-j==c[j]-c[r]){
ok = 0;
break; //只要与前面有一个皇后有冲突就break
}
if(ok) search(r+1);
}
}
int main(){
search(0);
cout<<cnt<<endl;
return 0;
}