-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueen.cpp
More file actions
74 lines (64 loc) · 1.42 KB
/
queen.cpp
File metadata and controls
74 lines (64 loc) · 1.42 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
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;
pair<int,int> p;
vector< pair<int,int> >ans;
int flag[10];
int cnt;
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<<" "<<row<<endl;
flag[row] = 1;
//base case
if(col==7){
cnt++;
cout<<cnt<<" "<<endl;
for(int i=0;i<ans.size();i++){
int a = ans[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,i)==1){
//cout<<i+1<<" "<<col+2<<endl;
flag[i] = 1;
funct(col+1,i);
int l = ans.size();
//cout<<" back "<<ans[l-1].second<<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;
cnt = 0;
// p.first = n;
//p.second = m;
//ans.push_back(p);
funct(n-1,m-1);
}