UVa 131 - The Psychic Poker Player

Contents

  1. 1. Problem
  2. 2. Solution
  3. 3. Code

Problem

題目網址
中文網址

Solution

很麻煩的一題。
用遞迴去取每種牌的組合,然後看此牌組的等級是啥。
計算等級的部分,直接暴力去找,可以利用記下 每種數字(1~13)的數量,來加快進行。

Code

UVa 131
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include<cstdio>

char card[10][3];
bool used[5], all[4][14];
int cardNum[14], best;
void choose(int n);
int countScore();
inline int toNum(char c)
{
if (c == 'T')
return 10;
else if (c == 'J')
return 11;
else if (c == 'Q')
return 12;
else if (c == 'K')
return 13;
else if (c == 'A')
return 1;
else return c - '0';
}
inline int toColor(char c)
{
if (c == 'C')
return 0;
else if (c == 'D')
return 1;
else if (c == 'H')
return 2;
else if (c == 'S')
return 3;
}
int main()
{
const char *kind[9] = { "straight-flush", "four-of-a-kind", "full-house", "flush", "straight", "three-of-a-kind", "two-pairs", "one-pair", "highest-card" };

while (scanf("%s", card[0]) != EOF)
{
int i;
for (i = 1; i < 10; i++)
scanf("%s", card[i]);
best = 8;
choose(0);

printf("Hand: ");
for (i = 0; i < 5; i++)
printf("%s ", card[i]);
printf("Deck: ");
for (i = 5; i < 10; i++)
printf("%s ", card[i]);
printf("Best hand: ");
puts(kind[best]);
}

return 0;
}
void choose(int n)
{
if (!best)
return;

int i;
if (n == 5)
{
int count = 0, num, color;
for (i = 0; i < 5; i++)
if (used[i])
{
num = toNum(card[i][0]);
color = toColor(card[i][1]);
cardNum[num]++;
all[color][num] = true;
count++;
}

for (i = 5; count < 5; count++, i++)
{
num = toNum(card[i][0]);
color = toColor(card[i][1]);
cardNum[num]++;
all[color][num] = true;
}

int score = countScore();
if (best > score)
best = score;

//init
for (i = 0; i < 4; i++)
for (int j = 0; j < 14; j++)
all[i][j] = false;
for (i = 0; i < 14; i++)
cardNum[i] = 0;
}
else
for (i = n; i < 5; i++)
{
used[i] = true;
choose(n + 1);
used[i] = false;
choose(n + 1);
}

}
int countScore()
{
//one = 7,two = 6,three = 5,house = 2,four = 1
int best = 8, pair = 0, three = 0, i;
for (i = 1; i <= 13; i++)
{
if (cardNum[i] == 4)
{
best = 1;
break;
}
else if (cardNum[i] == 3)
three = 1;
else if (cardNum[i] == 2)
pair++;
}

if (three&&pair)
best = 2;
else if (three)
best = 5;
else if (pair == 2)
best = 6;
else if (pair == 1)
best = 7;

if (best < 8)
return best;
//straight = 4,straight flush = 0
if (cardNum[1] && cardNum[10] && cardNum[11] && cardNum[12] && cardNum[13])
{
best = 4;
for (i = 0; i < 4; i++)
if (all[i][1] && all[i][10] && all[i][11] && all[i][12] && all[i][13])
best = 0;
}
for (i = 1; i <= 9 && best; i++)
if (cardNum[i] && cardNum[i + 1] && cardNum[i + 2] && cardNum[i + 3] && cardNum[i + 4])
{
best = 4;
for (int j = 0; j < 4; j++)
if (all[j][i] && all[j][i+1] && all[j][i+2] && all[j][i+2] && all[j][i+4])
best = 0;
}

if (best < 8)
return best;

//flush = 3
int count=0;
for (i = 0; i < 4; i++, count = 0)
{
for (int j = 1; j <= 13; j++)
if (all[i][j])
count++;
if (count == 5)
return 3;
}

return 8;
}