uvaoj 131
In 5-card draw poker, a player is dealt a hand of five cards (which may be looked at). The player may then discard between zero and five of his or her cards and have them replaced by the same number of cards from the top of the deck (which is face down). The object is to maximize the value of the final hand. The different values of hands in poker are given at the end of this problem.
Normally the player cannot see the cards in the deck and so must use probability to decide which cards to discard. In this problem, we imagine that the poker player is psychic and knows which cards are on top of the deck. Write a program which advises the player which cards to discard so as to maximize the value of the resulting hand.
Input will consist of a series of lines, each containing the initial five cards in the hand then the first five cards on top of the deck. Each card is represented as a two-character code. The first character is the face-value (A=Ace, 2-9, T=10, J=Jack, Q=Queen, K=King) and the second character is the suit (C=Clubs, D=Diamonds, H=Hearts, S=Spades). Cards will be separated by single spaces. Each input line will be from a single valid deck, that is there will be no duplicate cards in each hand and deck.
Each line of input should produce one line of output, consisting of the initial hand, the top five cards on the deck, and the best value of hand that is possible. Input is terminated by end of file.
Use the sample input and output as a guide. Note that the order of the cards in the player's hand is irrelevant, but the order of the cards in the deck is important because the discarded cards must be replaced from the top of the deck. Also note that examples of all types of hands appear in the sample output, with the hands shown in decreasing order of value.
TH JH QC QD QS QH KH AH 2S 6S 2H 2S 3H 3S 3C 2D 3D 6C 9C TH 2H 2S 3H 3S 3C 2D 9C 3D 6C TH 2H AD 5H AC 7H AH 6H 9H 4H 3C AC 2D 9C 3S KD 5S 4D KS AS 4C KS AH 2H 3C 4H KC 2C TC 2D AS AH 2C 9S AD 3C QH KS JS JD KD 6C 9C 8C 2D 7C 2H TC 4C 9S AH 3D 5S 2H QD TD 6S KH 9H AD QH
Hand: TH JH QC QD QS Deck: QH KH AH 2S 6S Best hand: straight-flush Hand: 2H 2S 3H 3S 3C Deck: 2D 3D 6C 9C TH Best hand: four-of-a-kind Hand: 2H 2S 3H 3S 3C Deck: 2D 9C 3D 6C TH Best hand: full-house Hand: 2H AD 5H AC 7H Deck: AH 6H 9H 4H 3C Best hand: flush Hand: AC 2D 9C 3S KD Deck: 5S 4D KS AS 4C Best hand: straight Hand: KS AH 2H 3C 4H Deck: KC 2C TC 2D AS Best hand: three-of-a-kind Hand: AH 2C 9S AD 3C Deck: QH KS JS JD KD Best hand: two-pairs Hand: 6C 9C 8C 2D 7C Deck: 2H TC 4C 9S AH Best hand: one-pair Hand: 3D 5S 2H QD TD Deck: 6S KH 9H AD QH Best hand: highest-card
好吧,写这个我整个人都不好了..
这个题好坑呀..
题目意思就是让你 从自己手里的五张牌中,随便去掉一些和桌子上的五张牌进行交换,组成尽可能好的结果(同花顺啊 什么的)
1.要知道一些规则
Straight-Flush 同花顺(同样花色加顺子)>four-of-a-kind 四带一 (四个数字一样) >full-house 三带一对(三个一样的 + 两个一样的) > flush 同花(同样的花色) > straight 顺子 > three-of-a-kind 三张相同的 > two-pairs 两对 > one-pair 一对 > highest-card 最大牌
C (方片) < D (梅花)< H (红心)< S(黑桃)
2.还有一个致命的就是 注意顺序,它决定了你的深搜的条件。
Note that the order of the cards in the player's hand is irrelevant, but the order of the cards in the deck is important because the discarded cards must be replaced from the top of the deck.意思是你必须按顺序去抽桌子上的牌(自顶向下),自己手里的可以无序随便扔。
#include<stdio.h>
#include<string.h>
char besthand[10][20]={ "straight-flush",
"four-of-a-kind",
"full-house",
"flush",
"straight",
"three-of-a-kind",
"two-pairs",
"one-pair",
"highest-card"};
typedef struct node
{
int number;
int color;
char n;
char c;
}node;
int res;
int c[5];
node str[5];
node nd[10];
void sort(node p[])
{
int i,j;node t;
for(i=0;i<4;i++)
{
for(j=0;j<4-i;j++)
{
if(p[j].number <p[j+1].number )
{
t = p[j];
p[j] = p[j+1];
p[j+1]=t;
}
}
}
}
int re_count()
{
int i;
node p[5];
for(i=0;i<5;i++)
p[i] = str[i];
sort(p);
int nu_count[14],co_count[5];
memset(nu_count,0,sizeof(nu_count));
memset(co_count,0,sizeof(co_count));
for(i=0;i<5;i++)
co_count[p[i].color]++;
for(i=0;i<5;i++)
nu_count[p[i].number]++;
for(i=0;i<4;i++)
{
if(p[i].number-p[i+1].number!=1)break;
}
int ok0 = 0;
if(i==4 ||( (p[0].number==13)&&(i==3)&&(p[4].number==1)))ok0=1; //注意KQJTA也是顺子哦
int ok =0;
for(i=1;i<5;i++)
if(co_count[i]==5)ok=1;
if(ok0 && ok)return 0;
for(i= 1;i<14;i++)
{
if(nu_count[i] == 4)return 1;
}
int ok3=0,ok2 =0;
for(i= 1;i<14;i++)
{
if(nu_count[i] == 3)ok3 = 1;
else if(nu_count[i] == 2)ok2 =1;
}
if(ok3 && ok2)return 2;
for(i=1;i<5;i++)
{
if(co_count[i] == 5)return 3;
}
if(ok0)return 4;
if(ok3==1)return 5;
ok2 = 0;
for(i=1;i<14;i++)
if(nu_count[i] == 2)ok2 ++;
if(ok2==2)return 6;
if(ok2 ==1)return 7;
return 8;
}
int dfs(int n) //将 选桌子上的牌设置为 按层,由于不会发生跳层,那么就一定是按有序的抽牌
{
int i,t;node temp;
t = re_count();
res = res>t?t:res;
if(n==5)return 0;
else
{
for(i = 0;i<5;i++)
{
if(c[i]==0)
{
temp = str[i];
c[i] = 1;
str[i]=nd[5+n];
dfs(n+1);
c[i]=0; //回退要恢复
str[i] = temp;
}
}
}
}
int main()
{
int i,ok; //郁闷 uva 检测时不能在循环里单独设置变量
while(1)
{
ok =0;
for(i=0;i<10;i++)
{
char c;
if( (scanf("%c",&c))==EOF){ok = 1;break;}
nd[i].n =c;
if(c=='T')nd[i].number=10;
else if(c=='J')nd[i].number=11;
else if(c=='Q')nd[i].number=12;
else if(c=='K')nd[i].number=13;
else if(c=='A')nd[i].number =1;
else nd[i].number=c-'0';
scanf("%c",&c);
nd[i].c = c;
if(c=='C')nd[i].color = 1;
else if(c=='D')nd[i].color = 2;
else if(c=='H')nd[i].color = 3;
else nd[i].color = 4;
getchar(); //用getchar()的同学要细心哈
}
if(ok==1)break;
memset(c,0,sizeof(c));
memset(str,0,sizeof(str));
res = 10;
for(i=0;i<5;i++)
str[i] = nd[i];
dfs(0);
for(i=0;i<10;i++)
{
if(i==0)printf("Hand: ");
else if(i==5)printf("Deck: ");
printf("%c%c ",nd[i].n ,nd[i].c);
}
printf("Best hand: ");
printf("%s\n",besthand[res]);
}
return 0;
}
不要管我,让我哭会~~~