思路挺简单的,就是判断手牌的类型比较麻烦一点,核心思想是枚举手牌子集,然后把桌上的牌的上面的某几张牌和手牌子集中的牌凑足5张,所有的情况里面最优的组合就是最后的答案,注意子集为空集的时候的特殊情况。
#include <stdio.h>
#include <string.h>
#include <algorithm>
struct node
{
int value;
char color;
};
bool operator > (struct node &a, struct node&b)
{
int color_a, color_b;
switch(a.color)
{
case 'C':
color_a = 0;
break;
case 'D':
color_a = 1;
break;
case 'H':
color_a = 2;
break;
case 'S':
color_a = 3;
break;
}
switch(b.color)
{
case 'C':
color_b = 0;
break;
case 'D':
color_b = 1;
break;
case 'H':
color_b = 2;
break;
case 'S':
color_b = 3;
break;
}
return color_a*15+a.value > color_b*15+b.value;
}
struct node hand[5];
struct node deck[5];
struct node current[5];
char hand_str[5][5];
char deck_str[5][5];
int max_grade;
int cmp(const void *a, const void *b)
{
struct node *pa = (struct node *)a;
struct node *pb = (struct node *)b;
return pa->value - pb->value;
}
int get_grade(const struct node *v)
{
int i;
struct node temp[5];
bool same_color;
bool straight;
for(i=0; i<=4; i++)
{
temp[i].value = v[i].value;
temp[i].color = v[i].color;
}
//按照点数升序排序
qsort((void*)temp, 5, sizeof(struct node), cmp);
same_color = true;
for(i=1; i<=4; i++)
if(temp[i].color != temp[i-1].color)
{
same_color = false;
break;
}
straight = true;
for(i=1; i<=4; i++)
if(temp[i].value-temp[i-1].value != 1)
{
straight = false;
break;
}
if
(
temp[4].value == 14 &&
temp[0].value == 2 &&
temp[1].value == 3 &&
temp[2].value == 4 &&
temp[3].value == 5
)
straight = true;
//grade 9
if(same_color && straight)
return 9;
//grade 8
if(temp[0].value==temp[1].value)
{
if(temp[2].value==temp[1].value && temp[3].value==temp[2].value)
return 8;
}
else
{
if(temp[1].value == temp[2].value)
{
if(temp[2].value==temp[1].value && temp[3].value==temp[2].value && temp[4].value==temp[3].value)
return 8;
}
}
//grade 7
if
(
temp[0].value == temp[2].value &&
temp[1].value == temp[2].value &&
temp[3].value == temp[4].value
)
{
return 7;
}
if
(
temp[3].value == temp[2].value &&
temp[4].value == temp[2].value &&
temp[0].value == temp[1].value
)
{
return 7;
}
//grade 6
if(same_color)
return 6;
//grade 5
if(straight)
return 5;
//grade 4
if(temp[0].value == temp[2].value
&& temp[1].value == temp[2].value)
return 4;
if(temp[1].value == temp[3].value
&& temp[2].value == temp[3].value)
return 4;
if(temp[2].value == temp[4].value
&& temp[3].value == temp[4].value)
return 4;
//grade 3
if(temp[1].value == temp[2].value
&& temp[3].value == temp[4].value)
return 3;
if(temp[0].value == temp[1].value
&& temp[3].value == temp[4].value)
return 3;
if(temp[0].value == temp[1].value
&& temp[2].value == temp[3].value)
return 3;
//gread 2
if(temp[0].value == temp[1].value)
return 2;
if(temp[1].value == temp[2].value)
return 2;
if(temp[2].value == temp[3].value)
return 2;
if(temp[3].value == temp[4].value)
return 2;
return 1;
}
void dfs(int cur)
{
int i;
int ii, jj;
int grade;
if(cur >= 5)
return;
if(cur == 0)
{
for(i=0; i<=4; i++)
{
current[cur].color = hand[i].color;
current[cur].value = hand[i].value;
for(ii=cur+1,jj=0; ii<=4; ii++,jj++)
{
current[ii].color = deck[jj].color;
current[ii].value = deck[jj].value;
}
grade = get_grade(current);
/*
{
int j;
for(j=0; j<=4; j++)
printf("%d%c ", current[j].value, current[j].color);
printf("grade=%d\n", grade);
}
*/
if(max_grade < grade)
max_grade = grade;
dfs(cur+1);
}
}
else
{
for(i=0; i<=4; i++)
{
if(hand[i] > current[cur-1])
{
current[cur].color = hand[i].color;
current[cur].value = hand[i].value;
for(ii=cur+1,jj=0; ii<=4; ii++,jj++)
{
current[ii].color = deck[jj].color;
current[ii].value = deck[jj].value;
}
grade = get_grade(current);
/*
{
int j;
for(j=0; j<=4; j++)
printf("%d%c ", current[j].value, current[j].color);
printf("grade=%d\n", grade);
}
*/
if(max_grade < grade)
max_grade = grade;
dfs(cur+1);
}
}
}
}
void func()
{
int i;
max_grade = get_grade(deck);
dfs(0);
//Hand: TH JH QC QD QS Deck: QH KH AH 2S 6S Best hand: straight-flush
printf("Hand: ");
for(i=0; i<=4; i++)
printf("%s ", hand_str[i]);
printf("Deck: ");
for(i=0; i<=4; i++)
printf("%s ", deck_str[i]);
printf("Best hand: ");
switch(max_grade)
{
case 9:
printf("straight-flush\n");
break;
case 8:
printf("four-of-a-kind\n");
break;
case 7:
printf("full-house\n");
break;
case 6:
printf("flush\n");
break;
case 5:
printf("straight\n");
break;
case 4:
printf("three-of-a-kind\n");
break;
case 3:
printf("two-pairs\n");
break;
case 2:
printf("one-pair\n");
break;
case 1:
printf("highest-card\n");
break;
}
}
int main(void)
{
char buf[5];
int count;
int value;
char color;
//freopen("input.dat", "r", stdin);
count = 0;
while(1)
{
if(scanf("%s", buf) == EOF)
break;
switch(buf[0])
{
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
value = buf[0]-'0';
break;
case 'T':
value = 10;
break;
case 'J':
value = 11;
break;
case 'Q':
value = 12;
break;
case 'K':
value = 13;
break;
case 'A':
value = 14;
break;
}
color = buf[1];
if(count <= 4)
{
hand[count].value = value;
hand[count].color = color;
strcpy(hand_str[count], buf);
}
else
{
deck[count-5].value = value;
deck[count-5].color = color;
strcpy(deck_str[count-5], buf);
}
count++;
if(count == 10)
{
func();
count = 0;
}
}
return 0;
}