HDU1528_Card Game Cheater(二分图/最大匹配)

解题报告

题目传送门

题意:

两个人拿着两副牌,其中一人知道另一个人的手牌,问要怎么配对才能使他获得更多的点数。

游戏规则:

两张牌的第一个数大的牌的人加点。

第一个数相同就比较第二个数。

H>S>D>C

思路:

很容易建图,二分图最大匹配over

#include <iostream>
#include <cstring>
#include <cstdio>

using namespace std;
int mmap[100][100],vis[100],pre[100],k;
char str[100][10];
int one(char c) {
    if(c>='2'&&c<='9')
        return c-'0';
    else {
        if(c=='T')return 10;
        else if(c=='J')return 11;
        else if(c=='Q')return 12;
        else if(c=='K')return 13;
        else if(c=='A')return 14;
        else if(c=='H')return 15;
        else if(c=='S')return 16;
        else if(c=='D')return 17;
        else if(c=='C')return 18;
    }
}
int dfs(int x) {
    for(int i=1; i<=k; i++) {
        if(!vis[i]&&mmap[x][i]) {
            vis[i]=1;
            if(pre[i]==-1||dfs(pre[i])) {
                pre[i]=x;
                return 1;
            }
        }
    }
    return 0;
}
int main() {
    int t,i,j;
    char ch[10];
    while(~scanf("%d",&t)) {
        while(t--) {
            memset(pre,-1,sizeof(pre));
            memset(mmap,0,sizeof(mmap));
            scanf("%d",&k);
            for(i=1; i<=k; i++) {
                scanf("%s",str[i]);
            }
            for(i=1; i<=k; i++) {
                scanf("%s",ch);
                for(j=1; j<=k; j++) {
                    if(one(ch[0])>one(str[j][0])) {
                        mmap[i][j]=1;
                    } else if(one(ch[0])==one(str[j][0])) {
                        if(one(ch[1])<one(str[j][1]))
                            mmap[i][j]=1;
                    }
                }
            }
            int ans=0;
            for(i=1; i<=k; i++) {
                memset(vis,0,sizeof(vis));
                ans+=dfs(i);
            }
            printf("%d\n",ans);
        }
    }
    return 0;
}

Card Game Cheater

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 1251    Accepted Submission(s): 667

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

HDU1528_Card Game Cheater(二分图/最大匹配),布布扣,bubuko.com

时间: 2024-11-05 02:19:42

HDU1528_Card Game Cheater(二分图/最大匹配)的相关文章

二分图最大匹配 (hdu1281、1528)

(hdu1281)棋盘游戏 题意:棋盘中一些格子可以放棋子,但要求棋子不能同行也不能同列.有一些格子若不放棋子,则最大匹配数目减少,则这个格子就是"重要点". 二分图的匹配:给定一个二分图G,M为G边集的一个子集,如果M满足当中的任意两条边都不依附于同一个顶点,则称M是一个匹配. #include"stdio.h" #include"string.h" #include"stdlib.h" #include"algo

匈牙利算法dfs模板 [二分图][二分图最大匹配]

最近学了二分图最大匹配,bfs模板却死活打不出来?我可能学了假的bfs 于是用到了dfs模板 寻找二分图最大匹配的算法是匈牙利算法 匈牙利算法的主要程序是寻找增广路 寻找增光路是过程是:从一个未经配对的点出发,历经未配边.匹配边.未配边.匹配边.未配边....最终到达一个未配点的过程,只要把路径中的未配边和匹配边的“身份”对调,匹配就加一了.这就是一个寻找增广路的过程,通过不断寻找增广路,可以找到最大的匹配. 1 #include<cstdio> 2 #include<cstring&g

图论——LCA、强联通分量、桥、割顶、二分图最大匹配、网络流

A: 交通运输线 时间限制: 5 Sec  内存限制: 128 MB 题目描述 战后有很多城市被严重破坏,我们需要重建城市.然而,有些建设材料只能在某些地方产生.因此,我们必须通过城市交通,来运送这些材料的城市.由于大部分道路已经在战争期间完全遭到破坏,可能有两个城市之间没有道路.当然在运输线中,更不可能存在圈. 现在,你的任务来了.给你战后的道路情况,我们想知道,两个城市之间是否存在道路,如果存在,输出这两个城市之间的最短路径长度. 输入 第一行一个整数Case(Case<=10)表示测试数据

POJ 2226二分图最大匹配

匈牙利算法是由匈牙利数学家Edmonds于1965年提出,因而得名.匈牙利算法是基于Hall定理中充分性证明的思想,它是二部图匹配最常见的算法,该算法的核心就是寻找增广路径,它是一种用增广路径求二分图最大匹配的算法. #include<stdio.h> #include<string.h> #include<stdlib.h> int n1,n2; char map[1005][1005]; //数组开大点 int mapx[1005][1005],mapy[1005]

【Codevs1922】骑士共存问题(最小割,二分图最大匹配)

题意: 在一个n*n个方格的国际象棋棋盘上,马(骑士)可以攻击的棋盘方格如图所示.棋盘上某些方格设置了障碍,骑士不得进入. 对于给定的n*n个方格的国际象棋棋盘和障碍标志,计算棋盘上最多可以放置多少个骑士,使得它们彼此互不攻击. n<=200,m<=n^2 思路:经典的二分图最大匹配问题,采用黑白点染色的思想. 如果按照相邻点黑白不同染色,可以发现每次跳到的点必定与现在所在点不同色,二分图最大匹配即可. 这里用最小割来解决,因为不能允许任何黑白点之间的任何一条边有流量,符合最小割的思想. 1

POJ2239 Selecting Courses(二分图最大匹配)

题目链接 N节课,每节课在一个星期中的某一节,求最多能选几节课 好吧,想了半天没想出来,最后看了题解是二分图最大匹配,好弱 建图: 每节课 与 时间有一条边 1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <cstring> 5 #include <algorithm> 6 #include <vector> 7 using namespa

二分图最大匹配总结

hdoj1528 二分匹配模版: 代码: 1 #include<stdio.h> 2 #include<iostream> 3 #include<string.h> 4 #include<algorithm> 5 #include<math.h> 6 using namespace std; 7 #define N 220 8 9 int n, maps[N][N], vis[N], used[N]; 10 11 struct node 12 {

POJ - 1422 Air Raid 二分图最大匹配

题目大意:有n个点,m条单向线段.现在问要从几个点出发才能遍历到所有的点 解题思路:二分图最大匹配,只要一条匹配,就表示两个点联通,两个点联通只需要选取其中一个点即可,所以有多少条匹配,就可以减去多少个点 #include<cstdio> #include<cstring> using namespace std; const int N = 130; int g[N][N], vis[N], link[N]; int n, m; void init() { memset(g, 0

&quot;《算法导论》之‘图’&quot;:不带权二分图最大匹配(匈牙利算法)

博文“二分图的最大匹配.完美匹配和匈牙利算法”对二分图相关的几个概念讲的特别形象,特别容易理解.本文介绍部分主要摘自此博文. 还有其他可参考博文: 趣写算法系列之--匈牙利算法 用于二分图匹配的匈牙利算法 1. 前言 二分图:简单来说,如果图中点可以被分为两组,并且使得所有边都跨越组的边界,则这就是一个二分图.准确地说:把一个图的顶点划分为两个不相交集 U 和V ,使得每一条边都分别连接U.V中的顶点.如果存在这样的划分,则此图为一个二分图.二分图的一个等价定义是:不含有「含奇数条边的环」的图.