-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStable Marriage.cpp
85 lines (61 loc) · 1.22 KB
/
Stable Marriage.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
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
#include <iostream>
using namespace std;
int mp[3][3]={
0,2,1,
0,2,1,
1,2,0};
int wp[3][3]={
2,1,0,
0,1,2,
2,0,1};
bool ok(int q[], int column){
int i;
for(i=0; i<column; i++){
if(q[column]==q[i])
return false;
}
for(i=0; i<column; i++){
if((mp[column][q[i]]<mp[column][q[column]]) && (wp[q[i]][column]<wp[q[i]][i]))
return false;
if((wp[q[column]][i]<wp[q[column]][column]) && (mp[i][q[column]]<mp[i][q[i]]))
return false;
}
return true;
}
void backtrack(int &column){
column--;
if(column==-1)
exit(1);
}
void print(int q[]){
cout<<"Man"<<" "<<"Woman"<<endl;
for(int i=0; i<3; i++){
cout<<" "<<i<<" "<<q[i]<<endl;
}
}
int main(){
int q[3];
q[0]=0;
int c=1;
bool from_backtrack=false;
while(1){
while(c<3){
if(!from_backtrack)
q[c]=-1;
from_backtrack=false;
while(q[c]<3){
q[c]++;
while(q[c]==3){
backtrack(c);
q[c]++;
}
if(ok(q,c))
break;
}
c++;
}
print(q);
backtrack(c);
from_backtrack=true;
}
}