-
Notifications
You must be signed in to change notification settings - Fork 0
/
jar_of_water.cpp
130 lines (117 loc) · 3.36 KB
/
jar_of_water.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <bits/stdc++.h>
using namespace std;
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define f first
#define s second
#define ll long long
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fll;
class Hand {
public:
Hand(){}
bool wild_card = false;
bool this_round;
map<char, int> hand;
bool first_is_smaller(char f, char s){
map<char, int> order;
order['A'] = 1;
order['2'] = 2;
order['3'] = 3;
order['4'] = 4;
order['5'] = 5;
order['6'] = 6;
order['7'] = 7;
order['8'] = 8;
order['9'] = 9;
order['D'] = 10;
order['Q'] = 11;
order['J'] = 12;
order['K'] = 13;
if(order[f] < order[s]){
return true;
}
return false;
}
bool check_if_won(){
if(wild_card) return false;
for (auto i = hand.begin(); i != hand.end(); i++) if(i->second == 4){
return true;
}
return false;
}
void recieve_card(char c){
if(c == 'W'){
// cout << " recebeu wildcard ";
wild_card = true, this_round = true;
}
else {
// cout << " recebeu " << c;
hand[c]++;
}
}
char discard(){
if(wild_card and !this_round){
// cout << " e descartou wildcard" << endl;
wild_card = false;
return 'W';
}
this_round = false;
auto min = hand.begin();
while(!min->second) min++;
for (auto i = hand.begin(); i != hand.end(); i++) if(i->second){
if(min->second == i->second and first_is_smaller(i->first, min->first)) min = i;
else if(min->second > i->second) min = i;
}
hand[min->first] -= 1;
// cout << " e descartou " << min->first << endl;
return min->first;
}
void print_hand(){
for (auto i = hand.begin(); i != hand.end(); i++){
for(int j = 0; j < i->second;j++){
cout << i->first;
}
}
if(wild_card) cout << 'W';
}
};
int main (){
int n, k; cin >> n >> k;
string str;
vector<Hand> pl(n);
for(int i = 0; i < n; i++){
Hand h = Hand();
char aux;
for(int j = 0; j < 4; j++){
cin >> aux;
h.hand[aux]++;
}
pl[i] = h;
}
char c = 'W'; int i = k - 1;
for(int i = 0; i < n; i++){
if(i != k - 1){
if(pl[i].check_if_won()){
cout << i + 1 << endl;
return 0;
}
}
}
while(true){
// cout << i << " - ";
// pl[i].print_hand();
pl[i].recieve_card(c);
if(pl[i].check_if_won()){
cout << i + 1 << endl;
break;
}
c = pl[i].discard();
if(pl[i].check_if_won()){
cout << i + 1 << endl;
break;
}
i++;
if(i == n) i = 0;
}
return 0;
}