UVa 131 Solitaire player with super power

Source: Internet
Author: User

Test instructions: The title description is too simple to guess. Look at other people's problems and know the meaning of the topic. Roughly meaning, five cards in the hand, you can discard 0~5 Zhang, and then from the pile of 5 cards in the top of the same number of cards, so that the value of the largest. The evaluation of value really is to guess ... In turn:

Straight-flush Flush
Four-of-a-kind Bomb
full-house three Zhang Tongju cards plus a pair
Flush with flowers
Straight (Note A can connect 2 can also receive K)
Three-of-a-kind Three cards of the same card
two-pairs two pair pair
One-pair pair of pairs
Highest-card Single-sheet max
Idea: Knowing the meaning of the topic is relatively simple. All the subsets of 5 cards in a hand, not less than 5, are taken from the top of the pile to make up 5 pieces. Then use these 5 sheets to judge. Since this is only 5 sheets, it is smaller, so it is possible to judge directly. Before you want to record the next 5 in a few 5 with, 4 with, 3 with, 2 with, too troublesome, directly in turn from the highest point in the beginning to judge. After 5 cards are obtained, it is possible to determine whether the same color, sorting after easy to determine the CIS and pairs and so on.

Code:

#include <stdio.h> #include <stdlib.h>void solve (), void subset (int *a, int cur), int process (int cur), int cmp_ int (const void *_a, const void *_b), char besthd[][22]={"Straight-flush", "Four-of-a-kind", "Full-house", "Flush", "St Raight "," Three-of-a-kind "," Two-pairs "," One-pair "," Highest-card "};char cards[10][5];int value[10];int A[6];int min;  int main () {//freopen ("131.in", "R", stdin);      Freopen ("131.out", "w", stdout);    for (int i=0;i<10;++i) {if (scanf ('%s ', cards[i])!=1) break;    if (i<9) continue;    printf ("Hand:");    for (int j=0;j<5;++j) printf ("%s", Cards[j]);    printf ("Deck:");    for (int j=5;j<10;++j) printf ("%s", Cards[j]);    printf ("Best hand:");    Solve ();  I=-1;    }}void solve () {for (int i=0;i<10;++i) {if (cards[i][0]== ' T ') value[i]=10;    else if (cards[i][0]== ' J ') value[i]=11;    else if (cards[i][0]== ' Q ') value[i]=12;    else if (cards[i][0]== ' K ') value[i]=13;    else if (cards[i][0]== ' A ') value[i]=1; else Value[i]=cards[i][0]-' 0 ';  } min=8;  Subset (a,0); printf ("%s\n", Besthd[min]);}  void subset (int *a, int cur) {int temp=process (cur);  min=temp<min?temp:min; int s=cur?  a[cur-1]+1:0;    for (int i=s;i<5;++i) {a[cur]=i;        Subset (A,CUR+1);  }}int process (int cur) {int num[5];    int ts=1;//with color for (int i=0;i<cur;++i) {num[i]=value[a[i]];  if (cards[a[i]][1]!=cards[a[0]][1]) ts=0;  } for (int i=cur;i<5;++i) {num[i]=value[5+i-cur];//is 5, not 6 if (cards[5+i-cur][1]!=cards[a[0]][1]) ts=0;    } qsort (Num,5,sizeof (num[0]), cmp_int); int sz=1;//But 1,10,11,12,13 is also cis for (int i=0;i<4;++i) {if (i==0 && num[i]==1 && num[4]==13) cont    Inue;  if (num[i]-num[i+1]!=-1) {sz=0; break;}  } if (Sz&&ts) return 0;  if ((Num[0]==num[1] | | num[3]==num[4]) && num[1]==num[2] && num[2]==num[3]) return 1;  if (Num[0]==num[1] && num[1]==num[2] && num[3]==num[4]) return 2; if (Num[0]==num[1] && num[2]==num[3] &AMP;&Amp  NUM[3]==NUM[4]) return 2;  if (TS) return 3;  if (SZ) return 4;  if (Num[0]==num[1] && num[1]==num[2]) return 5;  if (num[1]==num[2] && num[2]==num[3]) return 5;  if (Num[3]==num[4] && num[4]==num[5]) return 5; if (Num[0]==num[1] && (num[2]==num[3]| |  NUM[3]==NUM[4])) return 6;  if (num[1]==num[2] && num[3]==num[4]) return 6;  if (Num[0]==num[1] | | num[1]==num[2] | | num[2]==num[3] | | num[3]==num[4]) return 7; else return 8;}  int cmp_int (const void *_a, const void *_b) {return * (int*) _a-* (int*) _b; }
Code2:

Non-recursive form, binary enumeration subset #include <stdio.h> #include <stdlib.h>void solve (), void subset (int *a, int cur), int Process (int cur), int cmp_int (const void *_a, const void *_b), char besthd[][22]={"Straight-flush", "Four-of-a-kind", " Full-house "," flush "," straight "," Three-of-a-kind "," Two-pairs "," One-pair "," Highest-card "};char cards[10][5];int VA  Lue[10];int a[6];int Min;int Main () {freopen ("131.in", "R", stdin);      Freopen ("131.out", "w", stdout);    for (int i=0;i<10;++i) {if (scanf ('%s ', cards[i])!=1) break;    if (i<9) continue;    printf ("Hand:");    for (int j=0;j<5;++j) printf ("%s", Cards[j]);    printf ("Deck:");    for (int j=5;j<10;++j) printf ("%s", Cards[j]);    printf ("Best hand:");    Solve ();  I=-1;    }}void solve () {for (int i=0;i<10;++i) {if (cards[i][0]== ' T ') value[i]=10;    else if (cards[i][0]== ' J ') value[i]=11;    else if (cards[i][0]== ' Q ') value[i]=12;    else if (cards[i][0]== ' K ') value[i]=13;    else if (cards[i][0]== ' A ') value[i]=1; Else value[i]=cards[i][0]-' 0 ';  } min=8;    for (int i=0;i< (1<<5); ++i) {int cur=0;      for (int j=0;j<5;++j) if (I & (1<<j)) {a[cur++]=j;    } int temp=process (cur);  min=temp<min?temp:min; } printf ("%s\n", Besthd[min]);}  int process (int cur) {int num[5];    int ts=1;//with color for (int i=0;i<cur;++i) {num[i]=value[a[i]];  if (cards[a[i]][1]!=cards[a[0]][1]) ts=0;  } for (int i=cur;i<5;++i) {num[i]=value[5+i-cur];//is 5, not 6 if (cards[5+i-cur][1]!=cards[a[0]][1]) ts=0;    } qsort (Num,5,sizeof (num[0]), cmp_int); int sz=1;//But 1,10,11,12,13 is also cis for (int i=0;i<4;++i) {if (i==0 && num[i]==1 && num[4]==13) cont    Inue;  if (num[i]-num[i+1]!=-1) {sz=0; break;}  } if (Sz&&ts) return 0;  if ((Num[0]==num[1] | | num[3]==num[4]) && num[1]==num[2] && num[2]==num[3]) return 1;  if (Num[0]==num[1] && num[1]==num[2] && num[3]==num[4]) return 2; if (Num[0]==num[1] &Amp;& num[2]==num[3] && num[3]==num[4]) return 2;  if (TS) return 3;  if (SZ) return 4;  if (Num[0]==num[1] && num[1]==num[2]) return 5;  if (num[1]==num[2] && num[2]==num[3]) return 5;  if (Num[3]==num[4] && num[4]==num[5]) return 5; if (Num[0]==num[1] && (num[2]==num[3]| |  NUM[3]==NUM[4])) return 6;  if (num[1]==num[2] && num[3]==num[4]) return 6;  if (Num[0]==num[1] | | num[1]==num[2] | | num[2]==num[3] | | num[3]==num[4]) return 7; else return 8;}  int cmp_int (const void *_a, const void *_b) {return * (int*) _a-* (int*) _b; }



UVa 131 Solitaire player with super power

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.