Ecco le partite.
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <set>
#include <algorithm>
#include <iterator>
std::vector<int> listaTemporanea;
bool remove_double(std::set<std::pair<int,int>> &couples, int first, int second)
{
std::pair<int,int> t(second,first);
if(couples.find(t) != couples.end())
{
return true;
}
return false;
}
std::string GetNextCouple(std::set<std::pair<int,int>> &couples)
{
std::set<std::pair<int,int>>::iterator it;
for(it = couples.begin(); it != couples.end(); ++it)
{
bool first = false, second = false;
std::vector<int>::iterator vecIt = std::find(listaTemporanea.begin(),listaTemporanea.end(),it->first);
if(vecIt == listaTemporanea.end())
{
first = true;
vecIt = std::find(listaTemporanea.begin(),listaTemporanea.end(),it->second);
if(vecIt == listaTemporanea.end())
{
second = true;
}
else continue;
}
else
continue;
if(first && second)
{
listaTemporanea.push_back(it->first);
listaTemporanea.push_back(it->second);
std::stringstream ss;
ss << it->first << " - " << it->second;
couples.erase(it);
return ss.str();
}
}
return "";
}
void stampa_calendario(std::set<std::pair<int,int>> &couples)
{
while(!couples.empty())
{
listaTemporanea.clear();
std::cout << "P1 " << GetNextCouple(couples) << std::endl;
std::cout << "P2 " << GetNextCouple(couples) << std::endl;
std::cout << "P3 " << GetNextCouple(couples) << std::endl;
std::cout << "P4 " << GetNextCouple(couples) << std::endl << std::endl;
}
}
int main()
{
std::set<std::pair<int,int>> couples;
int players[] = {1,2,3,4,5,6,7,8};
do
{
for(int i = 0; i < 8; i+=2)
{
if(!remove_double(couples,players[i],players[i+1]))
couples.insert(std::make_pair(players[i],players[i+1]));
}
}while(std::next_permutation(players,players+8));
std::cout << "partite da giocare: " << couples.size() << std::endl;
for(std::set<std::pair<int,int>>::iterator it = couples.begin(); it != couples.end(); ++it)
{
if(it->second != -1)
std::cout << (it)->first << "-" << (it)->second << std::endl;
}
stampa_calendario(couples);
}
La gestione delle attese lo fai te. Devi solo creare una lista dove tieni traccia del numero di attese di ciascun giocatore.
Output
partite da giocare: 28
1-2
1-3
1-4
1-5
1-6
1-7
1-8
2-3
2-4
2-5
2-6
2-7
2-8
3-4
3-5
3-6
3-7
3-8
4-5
4-6
4-7
4-8
5-6
5-7
5-8
6-7
6-8
7-8
P1 1 - 2
P2 3 - 4
P3 5 - 6
P4 7 - 8
P1 1 - 3
P2 2 - 4
P3 5 - 7
P4 6 - 8
P1 1 - 4
P2 2 - 3
P3 5 - 8
P4 6 - 7
P1 1 - 5
P2 2 - 6
P3 3 - 7
P4 4 - 8
P1 1 - 6
P2 2 - 5
P3 3 - 8
P4 4 - 7
P1 1 - 7
P2 2 - 8
P3 3 - 5
P4 4 - 6
P1 1 - 8
P2 2 - 7
P3 3 - 6
P4 4 - 5