Buongiorno, sto provando a risolvere il problema Italian Scopa, mi trovo a due test case dal 100% ma non riesco a capire il baco nella logica del mio programma.
Ho usato comb_mat in modo esplicito solo per velocizzare il programma a run-time.
Il codice:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
struct Card {
int rank;
char suit;
Card(int rank, char suit) {
this->rank = rank;
this->suit = suit;
}
bool operator==(const Card &other) const {
return suit == other.suit && rank == other.rank;
}
string to_string() const {
return ::to_string(rank) + string(1, suit);
}
};
int main() {
const int HAND = 3;
const int TABLE = 4;
vector<Card> hand;
vector<Card> table;
for (int i = 0; i < HAND; i++) {
int rank;
char suit;
scanf("%d%c", &rank, &suit);
hand.emplace_back(rank, suit);
}
for (int i = 0; i < TABLE; i++) {
int rank;
char suit;
scanf("%d%c", &rank, &suit);
table.emplace_back(rank, suit);
}
bool comb_mat[16][TABLE] = {
{false, false, false, false},
{false, false, false, true},
{false, false, true, false},
{false, false, true, true},
{false, true, false, false},
{false, true, false, true},
{false, true, true, false},
{false, true, true, true},
{true, false, false, false},
{true, false, false, true},
{true, false, true, false},
{true, false, true, true},
{true, true, false, false},
{true, true, false, true},
{true, true, true, false},
{true, true, true, true}
};
vector<Card> best;
Card settebello = Card(7, 'G');
int found_7s_out = 0;
bool settebello_out = false;
bool scopa_out = false;
for (int i = 0; i < HAND; i++) {
for (int j = 0; j < 16; j++) {
vector<Card> curr;
int found_7s_in = 0;
bool settebello_in = false;
bool scopa_in = false;
if (hand[i] == settebello) {
settebello_in = true;
}
if (j == 16 - 1) {
scopa_in = true;
}
int rank_sum = 0;
for (int k = 0; k < TABLE; k++) {
Card card = table[k];
if (comb_mat[j][k]) {
curr.push_back(card);
rank_sum += card.rank;
if (card == settebello) {
settebello_in = true;
}
if (card.rank == 7) {
found_7s_in++;
}
}
}
if (rank_sum == hand[i].rank) {
if (!(settebello_out && !settebello_in)) {
settebello_out = settebello_in;
if (!(scopa_out && !scopa_in)) {
scopa_out = scopa_in;
if (found_7s_in > found_7s_out || (found_7s_in == found_7s_out && curr.size() > best.size())) {
found_7s_out = found_7s_in;
curr.push_back(hand[i]);
best = curr;
}
}
}
}
}
}
for (const Card &card: best) {
cout << card.to_string() << ' ';
}
return 0;
}
Sicuramente il problema sta nel come trovo la miglior mossa (best).
Se qualcuno l’ha fatto, chiedo un aiutino