-
Notifications
You must be signed in to change notification settings - Fork 13
/
rock-paper-scissors-lizard-spock.cpp
93 lines (79 loc) · 2.2 KB
/
rock-paper-scissors-lizard-spock.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
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <list>
using namespace std;
map<char, list<char> > wins = {
{ 'R', {'L', 'C'} },
{ 'P', {'R', 'S'} },
{ 'C', {'L', 'P'} },
{ 'L', {'S', 'P'} },
{ 'S', {'C', 'R'} }
};
struct Player
{
Player(int p_num = 0,
char p_sign = ' ') :
num (p_num ),
sign (p_sign ),
rounds(vector<int>())
{}
bool operator< (Player& p2)
{
rounds.push_back(p2.num);
p2.rounds.push_back(num);
if ( find(wins[sign].begin(),
wins[sign].end (),
p2.sign) != wins[sign].end() )
{
return true;
}
else if ( find(wins[p2.sign].begin(),
wins[p2.sign].end (),
sign) != wins[p2.sign].end() )
{
return false;
}
else
{
return ( num < p2.num );
}
}
int num;
char sign;
vector<int> rounds;
};
int main()
{
int N, num;
string sign;
vector< Player > contestants;
cin >> N; cin.ignore();
contestants.reserve(N);
for (int i = 0; i < N; i++)
{
cin >> num >> sign; cin.ignore();
contestants.emplace_back( Player(num, sign[0]) );
}
while ( contestants.size() > 1 )
{
vector< Player > current;
for ( int i = 0; i < contestants.size()-1; i+=2 )
{
Player& p1 = contestants.at(i );
Player& p2 = contestants.at(i+1);
if ( p1 < p2 ) { current.push_back(p1); }
else { current.push_back(p2); }
}
contestants = current;
}
const Player& winner = contestants.at(0);
cout << winner.num << endl;
for( int i = 0; i < winner.rounds.size()-1; i++ )
{
cout << winner.rounds.at(i) << " ";
}
cout << winner.rounds.at(winner.rounds.size()-1) << endl;
}