-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHW#15_SM.cpp
More file actions
73 lines (57 loc) · 1.56 KB
/
Copy pathHW#15_SM.cpp
File metadata and controls
73 lines (57 loc) · 1.56 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
// Shitrit, Jonathan
// CS211 section 52 Homework #15
// Cross problem
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
static int mp[3][3] = { {0,2,1}, // Man#0's preferences
//so man#0 likes woman0 most, woman2 next,and woman1 least
{0,2,1}, // Man#1's preferences
{1,2,0}}; // Man#2's preferences
static int wp[3][3] = { {2,1,0},// Woman#0's preferences
//so woman#0 likes man2 most, man1 next,and man2 least
{0,1,2}, // Woman#1's preferences
{2,0,1}}; // Woman#2's preferences
bool ok(int * q, int c) {
for(int i = 0; i < c; i++)
{
int wi = q[i];
int wc = q[c];
if((q[i] == q[c]) ||
(mp[i][wc] < mp[i][wi] && wp[wc][i] < wp[wc][c]) ||
(mp[c][wi] < mp[c][wc] && wp[wi][c] < wp[wi][i]))
{
return false;
}
}
return true;
}
void print(int * q){
static int count = 0;
int i;
count++;
cout << "Solution # " << count << endl;
cout << "Man - Woman" << endl;
for(i = 0; i < 3; i++){
cout << " " << i << " ---- " << q[i] << endl;
}
cout<<endl;
}
void move(int* q, int i){ // try place in col i
if(i == 3){
print(q);
return;
}
for(int j = 0; j < 3; j++){ // j is the row value
q[i] = j;
if(ok(q,i)) // try and place a queen in row j of col i,if yes
move(q, i+1); // go to next column otherwise try next j
}
}
// no value of j worked so return; };
int main() {
int q[3];
move(q,0);
system("pause");
}