Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- int n;
- int x[100]; bool d1[100]={true}, d2[100]={true}; bool cot[100]={true};
- int chess [100][100];
- int res=0;
- void show(){
- for(int i=1;i<100;i++){
- for(int j=1;j<100;j++){
- chess[i][j]=0;
- }
- }
- for(int i=1;i<=n;i++){
- chess[i][x[i]]=1;
- }
- for(int i=1;i<=n;i++){
- for(int j=1;j<=n;j++){
- cout<<chess[i][j]<<" ";
- }
- cout<<endl;
- }
- }
- void backtrack (int i){
- // duyet het tat ca kha nang co the co cua x[i];
- if(i==n+1){
- show();
- cout<<endl;
- res++;
- return ;
- }
- else {
- for(int j=1;j<=n;j++){
- // quan hau toa do (i,j) se quan li cot=j , d1=i-j+n; d2=i+j-1;
- if (cot[j]==1 && d1[i-j+n]==1 && d2[i+j-1]==1){
- x[i]=j;
- cot[j]=d1[i-j+n]=d2[i+j-1]=0;
- backtrack(i+1);
- cot[j]=d1[i-j+n]=d2[i+j-1]=1;
- }
- }
- }
- }
- int main(){
- cin>>n;
- for(int i=1;i<=100;i++){
- cot[i]=d1[i]=d2[i]=1;
- }
- backtrack(1);
- cout<<res<<endl;
- return 0;
- }
Add Comment
Please, Sign In to add comment