-
Notifications
You must be signed in to change notification settings - Fork 1
/
2013 J5-S3.cpp
142 lines (100 loc) · 3.83 KB
/
2013 J5-S3.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
131
132
133
134
135
136
137
138
139
140
141
142
/*
2013 J5/S3 - Chances of Winning
Difficulty: Easy
This question doesn't require any special algorithm.
To solve, you can quite literaly just generate every possible scenario and count each one.
I will be doing this via recursion
*/
#include <iostream>
#include <vector>
#include <utility>
std::vector<std::pair<int, int>> gamesToPlay; //Games to be played, each pair <i, j> indicates i and j must play a game together
std::vector<int> score (5, 0); //Keep track of score, note that 1 indexing so there's a filler number at the beginning
int possibilities = 0; //Answer, number of possibilites
int remainingGames; //Remaining games needed to be played
int T, G;
//Recurse through all posibilites
void future (char result, int gameNumber = 1){
//If all games have been completed, note that my recursion is quite inefficient and accidentally duplicates 3 times since i call the function 3 times each recurse, therefore i will divide my answer by 3 at the end
if (gameNumber > remainingGames){
bool winner = true; //If team T has won
for (int i = 1; i <= 4; i++){
//Remember that team T must have the highest score, ties are not allowed
if (score[i] >= score[T] && i != T){
winner = false;
}
}
if (winner == true){
possibilities++;
}
}
else{
//Remember to undo changes after recursing
//if i have the pair <a, b>, if team a wins
if (result == 'a'){
score[gamesToPlay[gameNumber - 1].first] += 3;
future('a', gameNumber + 1);
future('b', gameNumber + 1);
future('t', gameNumber + 1);
score[gamesToPlay[gameNumber - 1].first] -= 3;
}
//If team b wins
else if (result == 'b'){
score[gamesToPlay[gameNumber - 1].second] += 3;
future('a', gameNumber + 1);
future('b', gameNumber + 1);
future('t', gameNumber + 1);
score[gamesToPlay[gameNumber - 1].second] -= 3;
}
//If they tie, that's what 't' stands for
else{
score[gamesToPlay[gameNumber - 1].first]++;
score[gamesToPlay[gameNumber - 1].second]++;
future('a', gameNumber + 1);
future('b', gameNumber + 1);
future('t', gameNumber + 1);
score[gamesToPlay[gameNumber - 1].first]--;
score[gamesToPlay[gameNumber - 1].second]--;
}
}
}
int main(){
std::cin >> T >> G;
std::vector<std::vector<bool>> played (5, std::vector<bool> (5, false)); //Determine which teams have already played each other
for (int i = 0; i < G; i++){
int teamA, teamB, scoreA, scoreB;
std::cin >> teamA >> teamB >> scoreA >> scoreB;
played[teamA][teamB] = true;
played[teamB][teamA] = true; //2-D list moment, just marking it true on the other end as well
//If team a wins
if (scoreA > scoreB){
score[teamA] += 3;
}
//If they tie
else if (scoreA == scoreB){
score[teamA]++;
score[teamB]++;
}
//If team b wins
else{
score[teamB] += 3;
}
}
//Determine which games have yet to play
for (int i = 1; i <= 4; i++){
for (int j = 1; j <= 4; j++){
//i cannot be equal to j because a team cannot play itself
if (played[i][j] == false && i != j){
gamesToPlay.push_back(std::make_pair(i, j));
played[i][j] = true;
played[j][i] = true; //Since this is a 2-D list, i need to make sure [j][i] are also marked as true
}
}
}
remainingGames = gamesToPlay.size(); //Obtain remaining games
future('a');
future('b');
future('t');
std::cout << possibilities / 3;
return 0;
}