看别人代码看了很久的二分图最大匹配问题,今天终于算是自己AC了一道题,写完原创博客去看大牛们的代码!
Card Game Cheater |
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) |
Total Submission(s): 117 Accepted Submission(s): 84 |
Problem Description
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 |
Source
Northwestern Europe 2004
|
Recommend
8600
|
分析:典型的二分图最大匹配,用夏娃的牌去匹配(在这里匹配代表夏娃比亚当的牌大)亚当的牌,最大匹配数就是夏娃能获得的最大分。另外,输入处理稍微麻烦一点。
下面给出AC代码:
#include <iostream>
#include <cstdio>
#include <stdio.h>
#include <string.h>
#include <cstring>
#include <stdlib.h>
#include <algorithm>
#include <cstdlib>
#include <math.h>
#define N 27
using namespace std;
struct node{//牌
int num;//牌的数字
int f;//牌的花色
}adam[N],eve[N];//分别定义亚当和夏娃的手牌,即为二分图的两个集合
int match[N],visit[N];
int k;
bool lessthan(node a,node b){//比较两张牌的大小
if(a.num == b.num){
return a.f < b.f;
}else{
return a.num < b.num;
}
}
int find(int i){
int j;
for(j = 1;j <= k;j++){
if(!visit[j]&&lessthan(adam[j],eve[i])){
visit[j] = 1;
if(!match[j]||find(match[j])){
match[j] = i;
return 1;
}
}
}
return 0;
}
int getnum(){//匈牙利算法获取二分图的最大匹配数
int sum = 0,i;
memset(match,0,sizeof(match));
for(i = 1;i <= k;i++){
memset(visit,0,sizeof(visit));
if(find(i))sum++;
}
return sum;
}
void input(node *a,int k){//输入处理
char card[3];
for(int i = 1;i <= k;i++){
scanf("%s",card);
if(card[0] == 'T'){
a[i].num = 10;
}else if(card[0] == 'J'){
a[i].num = 11;
}else if(card[0] == 'Q'){
a[i].num = 12;
}else if(card[0] == 'K'){
a[i].num = 13;
}else if(card[0] == 'A'){
a[i].num = 14;
}else{
a[i].num = card[0]-'0';
}
if(card[1] == 'H'){
a[i].f = 4;
}else if(card[1] == 'S'){
a[i].f = 3;
}else if(card[1] == 'D'){
a[i].f = 2;
}else if(card[1] == 'C'){
a[i].f = 1;
}
}
}
int main(){
int n;
scanf("%d",&n);
while(n--){
scanf("%d",&k);
input(adam,k);
input(eve,k);
int ans = getnum();
printf("%d\n",ans);
}
return 0;
}