ZOJ - 2223 Card Game Cheater

来源:互联网 发布:华为数据库工程师待遇 编辑:程序博客网 时间:2024/05/23 21:43

题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1223 


Card Game Cheater

Time Limit: 10 Seconds      Memory Limit: 32768 KB

Adam and Eve play a card game using a regular deck of 52 cards. The rules are simple. The players sit on opposite sides of a table, facing each other. Each player gets k cards from the deck and, after looking at them, places the cards face down in a row on the table. Adam��s cards are numbered from 1 to k from his left, and Eve��s cards are numbered 1 to k from her right (so Eve��s i:th card is opposite Adam��s i:th card). The cards are turned face up, and points are awarded as follows (for each i �� {1, . . . , k}):

  • If Adam��s i:th card beats Eve��s i:th card, then Adam gets one point.

  • If Eve��s i:th card beats Adam��s i:th card, then Eve gets one point.

  • A card with higher value always beats a card with a lower value: a three beats a two, a four beats a three and a two, etc. An ace beats every card except (possibly) another ace.

  • If the two i:th cards have the same value, then the suit determines who wins: hearts beats all other suits, spades beats all suits except hearts, diamond beats only clubs, and clubs does not beat any suit.

    For example, the ten of spades beats the ten of diamonds but not the Jack of clubs.

    This ought to be a game of chance, but lately Eve is winning most of the time, and the reason is that she has started to use marked cards. In other words, she knows which cards Adam has on the table before he turns them face up. Using this information she orders her own cards so that she gets as many points as possible.

    Your task is to, given Adam��s and Eve��s cards, determine how many points Eve will get if she plays optimally.

    Input

    There will be several test cases. The first line of input will contain a single positive integer N giving the number of test cases. After that line follow the test cases.

    Each test case starts with a line with a single positive integer k <= 26 which is the number of cards each player gets. The next line describes the k cards Adam has placed on the table, left to right. The next line describes the k cards Eve has (but she has not yet placed them on the table). A card is described by two characters, the first one being its value (2, 3, 4, 5, 6, 7, 8 ,9, T, J, Q, K, or A), and the second one being its suit (C, D, S, or H). Cards are separated by white spaces. So if Adam��s cards are the ten of clubs, the two of hearts, and the Jack of diamonds, that could be described by the line

    TC 2H JD

    Output

    For each test case output a single line with the number of points Eve gets if she picks the optimal way to arrange her cards on the table.

    Sample Input

    3
    1
    JD
    JH
    2
    5D TC
    4C 5H
    3
    2H 3H 4H
    2D 3D 4D

    Sample Output

    1
    1
    2


    代码一:

    #include<iostream>#include<stdio.h>#include<string.h>using namespace std;int map[53][53];int xm[53];int ym[53];int tag[53];int change(char ch){if(ch=='H'){return 3;}else if(ch=='S'){return 2;    }else if(ch=='D'){return 1;}else if(ch=='C'){return 0;}else if(ch=='T'){return 8*4;}else if(ch=='J'){return 9*4;}else if(ch=='Q'){return 10*4;}else if(ch=='K'){return 11*4;}else if(ch=='A'){return 12*4;}else{return (ch-'2')*4;}}int dfs(int u){int i;for(i=0;i<52;i++){if(tag[i]==0&&map[u][i]==1){tag[i]=1;if(ym[i]==-1||dfs(ym[i])==1){ym[i]=u;xm[u]=i;return 1;}}}return 0;}int find(){int count,j;count=0;memset(xm,-1,sizeof(xm));memset(ym,-1,sizeof(ym));for(j=0;j<52;j++){if(xm[j]==-1)    {memset(tag,0,sizeof(tag));if(dfs(j)==1){count++;        }    }}return count;}void print(){int i,j;for(i=0;i<52;i++){for(j=0;j<52;j++){printf(" %d ",map[i][j]);}printf("\n");}}int main(){int t,n,i,j,tem;char str[4];int a[30];while(scanf("%d",&t)!=EOF){while(t--){memset(map,0,sizeof(map));scanf("%d",&n);memset(a,0,sizeof(a));for(i=0;i<n;i++){scanf("%s",str);for(j=0;j<2;j++){a[i]+=change(str[j]);}}for(i=0;i<n;i++){scanf("%s",str);tem=0;for(j=0;j<2;j++){tem+=change(str[j]);}for(j=0;j<n;j++){if(tem>a[j]){map[tem][a[j]]=1;}}}printf("%d\n",find());}}return 0;}


    代码二:

    #include<stdio.h>#include<string.h>#define N 52 int map[N][N],v[N],link[N]; int A[N],B[N];int t,n; int dfs(int k) {     int i;     for(i=1;i<=n;i++)     {         if(map[k][i]&&!v[i])         {             v[i]=1;             if(link[i]==0||dfs(link[i]))             {                 link[i]=k;                 return 1;             }         }     }     return 0; } int main() {     char c,d,e;     int i,j,vv,flag,ans;     int ap,bp;          scanf("%d",&t);     while(t--)     {         scanf("%d",&n);         getchar();         flag=1;         ap=bp=0;         for(i=0;i<2*n;i++)         {             scanf("%c%c%c",&c,&d,&e);             if(c>='0'&&c<='9')                 vv=c-'0';             if(c=='T')vv=10;             if(c=='J')vv=11;             if(c=='Q')vv=12;             if(c=='K')vv=13;             if(c=='A')vv=14;             vv*=100;             if(d=='H')vv+=4;             if(d=='S')vv+=3;             if(d=='D')vv+=2;             if(d=='C')vv+=1;             if(flag)                 A[++ap]=vv;             else                 B[++bp]=vv;             if(e=='\n')                 flag=0;         }         memset(map,0,sizeof(map));         for(i=1;i<=n;i++)         {             for(j=1;j<=n;j++)             {                 if(A[i]<B[j])                     map[j][i]=1;             }         }        memset(link,0,sizeof(link));         ans=0;         for(i=1;i<=n;i++)         {             memset(v,0,sizeof(v));             if(dfs(i))                 ans++;         }         printf("%d\n",ans);     } } 


    代码三:

    #include <stdio.h>  #include <string.h>  #include <stdlib.h>    #define max(a, b) (((a) > (b)) ? (a) : (b))  #define min(a, b) (((a) < (b)) ? (a) : (b))    #define FALSE   0  #define TRUE    1    #define infinity    0x0f0f0f0f  #define minus_inf    0x80808080    #define MAXSIZE 27    int tcase, k ;  int cover[MAXSIZE], link[MAXSIZE], adam[MAXSIZE], eve[MAXSIZE] ;    int makeInt (char c)  {      int sign = 10, d ;      if ('0' < c && c <= '9')          d = c - '0' ;      else if (c == 'T') d = 10 ;      else if (c == 'J') d = 11 ;      else if (c == 'Q') d = 12 ;      else if (c == 'K') d = 13 ;      else if (c == 'A') d = 14 ;      else {          sign = 1 ;          if (c == 'C') d = 1 ;          else if (c == 'D') d = 2 ;          else if (c == 'S') d = 3 ;          else if (c == 'H') d = 4 ;      }      return d * sign ;  }    int find (int cur)  {      int i;      for (i = 0 ; i < k ; ++i)      {          if (cover[i] == FALSE && eve[cur] > adam[i])          {              cover[i] = TRUE ;              if (link[i] == -1 || find (link[i]))              {                  link[i] = cur ;                  return 1 ;              }          }      }      return 0 ;  }    int main ()  {      char a, b;      int i, sum ;      while (scanf ("%d", &tcase) != EOF)      {          while (tcase--)          {              scanf ("%d", &k) ;              getchar () ;              for (i = 0 ; i < k ; ++i)              {                  scanf ("%c%c", &a, &b) ;                  getchar () ;                  adam[i] = makeInt (a) + makeInt (b) ;              }              for (i = 0 ; i < k ; ++i)              {                  scanf ("%c%c", &a, &b) ;                  getchar () ;                  eve[i] = makeInt (a) + makeInt (b) ;              }                            sum = 0 ;              memset (link, -1, sizeof (link)) ;              for (i = 0 ; i < k ; ++i)              {                  memset (cover, 0, sizeof (cover)) ;                  sum += find (i) ;              }              printf ("%d\n", sum) ;          }      }      return 0 ;  }