Problem A - Yahtzee
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.
The 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. The 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
Output for Sample Input
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
光是看懂分方式就很浪了。
"sum of all ones thrown" 到底什意思呢?所有骰到 1 的合,推。
那要怎做呢?每骰子只一分方式,合就有 13!,又有特的 bonus 分算,
前六分方式和 pt >= 63 外加 35 分,最高分多少?
了加快速度,做法有跟背包相似,逐步放入一骰子,然後行移。
在必,[0000000000010] // 代表第二分方式已被使用。
然後 dp[分可使用的][前六分的分] = 最高分。
什要多一 [前六分的分] 是因到後面要算 bonus 的候要用的,
既然大於等於 63 都有 bonus,那 0-63 就可以了。
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
#define STATE 8192 //1<<13
#define STATE2 64 // bonus use
#define DSIZE 13
int DICE[13][5];
int score_cat(int dice[], int cat) {
int tot = 0, i;
int D[7];
switch(cat) {
case 1:case 2:case 3:case 4:case 5:case 6:
for(i = 0; i < 5; i++)
if(dice[i] == cat)
tot += cat;
break;
case 7: // chance
for(i = 0; i < 5; i++)
tot += dice[i];
break;
case 8: // three of a kind
if(dice[0] == dice[2] || dice[1] == dice[3] ||
dice[2] == dice[4])
for(i = 0; i < 5; i++)
tot += dice[i];
break;
case 9: // four of a kind
if(dice[0] == dice[3] || dice[1] == dice[4])
for(i = 0; i < 5; i++)
tot += dice[i];
break;
case 10: // five of a kind
if(dice[0] == dice[4])
tot = 50;
break;
case 11: // short straight
for(i = 0; i <= 6; i++)
D[i] = 0;
for(i = 0; i < 5; i++)
D[dice[i]] = 1;
for(i = 1; i <= 3; i++) {
if(D[i] && D[i+1] && D[i+2] && D[i+3])
tot = 25;
}
break;
case 12: // long straight
for(i = 0; i < 4; i++) {
if(dice[i] != dice[i+1]-1)
return 0;
}
tot = 35;
break;
case 13: //full house
if(dice[0] == dice[1] && dice[2] == dice[4])
tot = 40;
if(dice[0] == dice[2] && dice[3] == dice[4])
tot = 40;
break;
}
return tot;
}
int count_bit(int n) {
static int i, j;
for(i = 0, j = 0; i < 13; i++)
j += (n>>i)&1;
return j;
}
int dp[STATE][STATE2]; // = max score
int score[DSIZE][DSIZE]; // score[i][j] = DICE[i] cat j
int arg_dp[STATE][STATE2][2]; //[0] category, [1] pre(1-6) total score
void sol_dp() {
int i, j, k, p, q;
for(i = 0; i < 13; i++)
for(j = 0; j < 13; j++)
score[i][j] = score_cat(DICE[i], j+1);
memset(dp, -1, sizeof(dp));
dp[0][0] = 0;
int s, nstate, a, b;
for(k = 0; k < 13; k++) { // add DICE[k]
for(i = 0; i < STATE; i++) {// i = STATE mark
if(count_bit(i) == k) {
for(j = 0; j < 13; j++) {//set category
if(!((i>>j)&1)) {
s = score[k][j];
nstate = i|(1<<j); // new state
a = j < 6 ? s : 0;
for(p = 0; p < STATE2; p++) {
if(dp[i][p] >= 0) {
b = (p+a < 63) ? p+a : 63;
if(dp[nstate][b] < dp[i][p]+s) {
dp[nstate][b] = dp[i][p]+s;
arg_dp[nstate][b][0] = j;
arg_dp[nstate][b][1] = p;
}
}
}
}
}
}
}
}
int mx = 0, bouns = 0;
for(i = 0; i < 63; i++)
if(dp[STATE-1][i] > mx) {
mx = dp[STATE-1][i];
b = i;
}
if(dp[STATE-1][63] >= 0 && dp[STATE-1][63]+35 > mx) {
mx = dp[STATE-1][63]+35;
b = 63;
bouns = 35;
}
// backtracking
int category[13], state = STATE-1;
int ans[13];
for(i = 12; i >= 0; i--) {
category[i] = arg_dp[state][b][0];
b = arg_dp[state][b][1];
state ^= 1<<category[i];
ans[category[i]] = score[i][category[i]];
}
for(i = 0; i < 13; i++)
printf("%d ", ans[i]);
printf("%d %d\n", bouns, mx);
}
int main() {
int i, j;
while(1) {
for(i = 0; i < 13; i++) {
for(j = 0; j < 5; j++) {
if(scanf("%d", &DICE[i][j]) != 1)
return 0;
}
sort(DICE[i], DICE[i]+5);
}
//puts("==== START");
sol_dp();
}
return 0;
}
文章定位: