24h購物| | PChome| 登入
2013-03-02 19:18:27| 人氣769| 回應0 | 上一篇 | 下一篇

[UVA][dp][第二種] 10149 - Yahtzee

推薦 0 收藏 0 轉貼0 訂閱站台

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



這一個 dp 的方式,是按照分類方式依序放入
然後位元壓縮則是代表哪幾個已經使用過了。

因此只有狀態 dp[1<<13] 需要記錄,當放入第六種分類方法時,
同時計數是否可以得到 bonus。

#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
#define STATE 8192 //1<<13
#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], dp_bonus[STATE]; // = max score
int score[DSIZE][DSIZE]; // score[i][j] = DICE[i] cat j
int arg_dp[STATE]; // choose j
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, dp_bonus[0] = 0;
int bs, s, nstate;
for(k = 0; k < 13; k++) { // add category
for(i = 0; i < STATE; i++) {// i = STATE mark
if(count_bit(i) == k) {
for(j = 0; j < 13; j++) { // using j dice
if(!((i>>j)&1)) {
nstate = i|(1<<j);
s = dp[i] + score[j][k];
bs = 0;
if(k == 5 && s >= 63)
bs = 35;
if(dp[nstate] < s+bs) {
dp[nstate] = s+bs;
dp_bonus[nstate] = bs;
arg_dp[nstate] = j;
}
}
}
}
}
}
nstate = STATE-1;
int category[13], bonus = 0;
for(i = 12; i >= 0; i--) {
category[i] = score[arg_dp[nstate]][i];
if(i == 5)
bonus = dp_bonus[nstate];
nstate ^= 1<<arg_dp[nstate];
}
for(i = 0; i < 13; i++)
printf("%d ", category[i]);
printf("%d %d\n", bonus, dp[STATE-1]);

}
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);
}
sol_dp();
}
return 0;
}
 

台長: Morris
人氣(769) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][SA] 11107 - Life Forms
此分類上一篇:[UVA][SA後綴數組] 10234 - Frequent Substrings

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文