-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8queen.cpp
More file actions
91 lines (78 loc) · 1.82 KB
/
8queen.cpp
File metadata and controls
91 lines (78 loc) · 1.82 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
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
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;
pair<int,int> p;
vector< pair<int,int> >ans;
vector< pair<int,int> >arr;
int flag[10];
int mod,md = 8;
int cnt = 0;
int test(int col,int row){
int dx,dy;
for(int i=0;i<ans.size();i++){
dx = abs(ans[i].first - col);
dy = abs(ans[i].second - row);
if(dx==dy) return -1;
}
return 1;
}
void funct(int col,int row){
// cout<<"ok 1"<<endl;
p.first = col;
p.second = row;
ans.push_back(p);
//cout<<" exact "<<col+1<<" "<<row+1<<endl;
flag[row] = 1;
//base case
if(col==mod){
cnt++;
//cout<<cnt<<" ";
arr = ans;
sort(arr.begin(),arr.end());
/*int a = arr[0].first+1;
cout<<a;
for(int i=1;i<arr.size();i++){
a = arr[i].first+1;
cout<<" "<<a;
}
cout<<endl;*/
int a = arr[0].second+1;
cout<<a;
for(int i=1;i<arr.size();i++){
a = arr[i].second+1;
cout<<" "<<a;
}
cout<<endl;
return ;
}
//cout<<"ok 2"<<endl;
// have to write
for(int i=0;i<8;i++){
if((i<row-1||i>row+1)&&flag[i]!=1&&test((col+1)%md,i)==1){
//cout<<i+1<<" "<<col+2<<endl;
flag[i] = 1;
funct((col+1)%md,i);
int l = ans.size();
// cout<<" back "<<ans[l-1].second+1<<endl;
flag[i] = 0;
ans.pop_back();
}
}
//cout<<"ok 3"<<endl;
return;
}
int main(){
//freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
int n,m;
cin>>n>>m;
if(n-1>0) mod = n-2;
else mod = 7;
cnt = 0;
funct(n-1,m-1);
//for(int i=0;i<8;i++) flag[i] = 0;
//ans.clear();
}