Algorithm


problem Link : https://onlinejudge.org/index.php?option=onlinejudge&Itemid=8&page=show_problem&problem=1090 

The game of Yahtzee involves 5 dice, which are thrown in 13 rounds. A score card contains 13 categories; each round may be scored in a category of the player’s choosing, but each category may be scored only once in the game. The 13 categores are scored as follows: • ones - sum of all ones thrown • twos - sum of all twos thrown • threes - sum of all threes thrown • fours - sum of all fours thrown • fives - sum of all fives thrown • sixes - sum of all sixes thrown • chance - sum of all dice • three of a kind - sum of all dice, provided at least three have same value • four of a kind - sum of all dice, provided at least four have same value • five of a kind - 50 points, provided all five dice have same value • short straight - 25 points, provided four of the dice form a sequence (that is, 1,2,3,4 or 2,3,4,5 or 3,4,5,6) • long straight - 35 points, provided all dice form a sequence (1,2,3,4,5 or 2,3,4,5,6) • full house - 40 points, provided three of the dice are equal and the other two dice are also equal. (for example, 2,2,5,5,5) Each of the last six categories may be scored as 0 if the criteria are not met. The score for the game is the sum of all 13 categories plus a bonus of 35 points if the sum of the first six categores (ones to sixes) is 63 or greater. Your job is to compute the best possible score for a sequence of rounds. Input Each line of input contains 5 integers between 1 and 6, indicating the values of the five dice thrown in each round. There are 13 such lines for each game, and there may be any number of games in the input data. Output Your output should consist of a single line for each game containing 15 numbers: the score in each category (in the order given), the bonus score (0 or 35), and the total score. If there is more than categorization that yields the same total score, any one will do.

Sample Input 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 1 1 1 1 6 6 6 6 6 6 6 6 1 1 1 1 1 2 2 1 1 1 2 3 1 2 3 4 5 1 2 3 4 6 6 1 2 6 6 1 4 5 5 5 5 5 5 5 6 4 4 4 5 6 3 1 3 6 3 2 2 2 4 6 Sample Output 1 2 3 4 5 0 15 0 0 0 25 35 0 0 90 3 6 9 12 15 30 21 20 26 50 25 35 40 35 327

Code Examples

#1 Code Example with C Programming

Code - C Programming

 #include <bits/stdc++.h>
using namespace std;

int count_bit(int bits){
    int cnt = 0;
    while(bits){
        cnt++;
        bits &= (bits-1);
    }
    return cnt;
}

int get_score(vector<int> dice, int cat) {
    int score = 0;
    if(cat < 7) {
        for(auto& d : dice)
            if(d == cat)
                score += cat;
    } else if(cat == 7) {
        score = accumulate(dice.begin(), dice.end(), 0);
    } else if(cat == 8) {
        if(dice[0] == dice[2] || dice[1] == dice[3] || dice[2] == dice[4])
            score = accumulate(dice.begin(), dice.end(), 0);
    } else if(cat == 9) {
        if(dice[0] == dice[3] || dice[1] == dice[4])
            score = accumulate(dice.begin(), dice.end(), 0);
    } else if(cat == 10) {
        if(dice[0] == dice[4])
            score += 50;
    } else if(cat == 11> {
        vector < bool> hasVal(7);
        for(auto& d : dice)
            hasVal[d] = true;
        for(int i=1;i < =3;i++) {
            if(hasVal[i] && hasVal[i+1] && hasVal[i+2] && hasVal[i+3])
                score = 25;
        }
    } else if(cat == 12) {
        for(int i=0;i<4;i++) {
            if(dice[i]+1 != dice[i+1])
                return 0;
        }
        score += 35;
    } else {
        if(dice[0] == dice[1] && dice[2] == dice[4])
            score += 40;
        else if(dice[0] == dice[2] && dice[3] == dice[4])
            score += 40;
    }
    return score;
}

int dp[1<<13][64];
int path_bonus[1<<13][64];
int path_cat[1<<13][64];

int main() {
    int v;
    while(true) {
        vector < vector<int>> rounds;
        vector < vector<int>> scores;

        for(int i=0;i < 13;i++){
            vector<int> dice;
            for(int j=0;j < 5;j++){
                if(scanf("%d",&v) == EOF) return 0;
                dice.push_back(v);
            }
            sort(dice.begin(), dice.end());
            rounds.push_back(dice);

            vector<int> score;
            for(int j=0;j < 13;j++){
                score.push_back(get_score(dice,j+1));
            }
            scores.push_back(score);
        }
        memset(dp, -1, sizeof dp);
        dp[0][0] = 0;

        for(int i=0;i < 13;i++){
            for(int j=0;j < (1<<13);j++)
            if(count_bit(j) == i) {
                for(int k=0;k < 13;k++){
                    if(j&(1<<k)) continue;
                    int val = scores[i][k];
                    int bonus = k < 6? val : 0;
                    int next_j = j | (1<<k);
                    for(int l=0;l < 64;l++){
                        if(dp[j][l] == -1) continue;
                        int next_bonus = min(bonus+l,63);
                        if(dp[next_j][next_bonus]  <  dp[j][l]+val) {
                            dp[next_j][next_bonus] = dp[j][l]+val;
                            path_bonus[next_j][next_bonus] = l;
                            path_cat[next_j][next_bonus] = k;
                        }
                    }
                }
            }
        }
        int best=0, bonus=0, b_score=0;
        for(int i=0;i < =63;i++){
            int val = dp[(1<<13)-1][i];
            if(val == -1) continue;
            if(i==63> val += 35;
            if(val > best) {
                best = val;
                bonus = i;
                b_score = i==63 ? 35 : 0;
            }
        }
        vector<int> res(15);
        res[14] = best;
        res[13] = b_score;
        int cur = (1<<13)-1;
        int cur_bonus = bonus;
        int rounds_left = 12;
        while(cur){
            int prev_bonus = path_bonus[cur][cur_bonus];
            int prev_cat = path_cat[cur][cur_bonus];
            res[prev_cat] += scores[rounds_left--][prev_cat];
            cur ^= (1 i&It prev_cat);
            cur_bonus = prev_bonus;
        }
        for(auto it=res.begin(); it!=res.end();it++){
            if(it != res.begin()) printf(" ");
            printf("%d",*it);
        }
        printf("\n");
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 1 1 1 1
6 6 6 6 6
6 6 6 1 1
1 1 1 2 2
1 1 1 2 3
1 2 3 4 5
1 2 3 4 6
6 1 2 6 6
1 4 5 5 5
5 5 5 5 6
4 4 4 5 6
3 1 3 6 3
2 2 2 4 6

Output

x
+
cmd
1 2 3 4 5 0 15 0 0 0 25 35 0 0 90
3 6 9 12 15 30 21 20 26 50 25 35 40 35 327
Advertisements

Demonstration


UVA Online Judge solution - 10149 - Yahtzee - UVA Online Judge solution in C,C++,Java

Previous
UVA Online Judge solution - 10147 - Highways - UVA Online Judge solution in C,C++,Java
Next
UVA Online Judge solution - 10152-ShellSort - UVA Online Judge solution in C,C++,Java