HDU4930 Fighting the Landlords 模拟

时间:2021-06-10 20:26:46

Fighting the Landlords

Fighting the Landlords

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 175    Accepted Submission(s): 61

Problem Description
Fighting the Landlords is a card game which has been a heat for years
in China. The game goes with the 54 poker cards for 3 players, where
the “Landlord” has 20 cards and the other two (the “Farmers”) have 17.
The Landlord wins if he/she has no cards left, and the farmer team wins
if either of the Farmer have no cards left. The game uses the concept of
hands, and some fundamental rules are used to compare the cards. For
convenience, here we only consider the following categories of cards:

1.Solo: a single card. The priority is: Y (i.e. colored Joker) > X
(i.e. Black & White Joker) > 2 > A (Ace) > K (King) > Q
(Queen) > J (Jack) > T (10) > 9 > 8 > 7 > 6 > 5
> 4 > 3. It’s the basic rank of cards.

2.Pair : two
matching cards of equal rank (e.g. 3-3, 4-4, 2-2 etc.). Note that the
two Jokers cannot form a Pair (it’s another category of cards). The
comparison is based on the rank of Solo, where 2-2 is the highest, A-A
comes second, and 3-3 is the lowest.

3.Trio: three cards of
the same rank (e.g. 3-3-3, J-J-J etc.). The priority is similar to the
two categories above: 2-2-2 > A-A-A > K-K-K > . . . > 3-3-3.

4.Trio-Solo: three cards of the same rank with a Solo as the kicker.
Note that the Solo and the Trio should be different rank of cards (e.g.
3-3-3-A, 4-4-4-X etc.). Here, the Kicker’s rank is irrelevant to the comparison, and the Trio’s rank determines the priority. For example, 4-4-4-3 > 3-3-3-2.

5.Trio-Pair : three cards of the same rank with a Pair as the kicker
(e.g. 3-3- 3-2-2, J-J-J-Q-Q etc.). The comparison is as the same as
Trio-Solo, where the Trio is the only factor to be considered. For
example,4-4-4-5-5 > 3-3-3-2-2. Note again, that two jokers cannot
form a Pair.

6.Four-Dual: four cards of the same rank with two cards as the kicker. Here, it’s allowed for the two kickers to share the same rank. The four same cards dominates the comparison: 5-5-5-5-3-4 > 4-4-4-4-2-2.

In the categories above, a player can only beat the prior hand using
of the same category but not the others. For example, only a prior Solo
can beat a Solo while a Pair cannot. But there’re exceptions:

7.Nuke: X-Y (JOKER-joker). It can beat everything in the game.

8.Bomb: 4 cards of the same rank. It can beat any other category
except Nuke or another Bomb with a higher rank. The rank of Bombs
follows the rank of individual cards: 2-2-2-2 is the highest and 3-3-3-3
is the lowest.

Given the cards of both yours and the next
player’s, please judge whether you have a way to play a hand of cards
that the next player cannot beat you in this round. If you no longer have cards after playing, we consider that he cannot beat you either. You may see the sample for more details.

 
Input
The input contains several test cases. The number of test cases T (T<=20) occurs in the first line of input.

Each test case consists of two lines. Both of them contain a string
indicating your cards and the next player’s, respectively. The length of
each string doesn’t exceed 17, and each single card will occur at most 4 times totally on two players’ hands except that the two Jokers each occurs only once.

 
Output
For each test case, output Yes if you can reach your goal, otherwise output No.
 
Sample Input
4
33A
2
33A
22
33
22
5559T
9993
 
Sample Output
Yes
No
Yes
Yes
 
Source
 
Recommend
hujie

题意:斗地主。给你两个人手上的牌,现在轮到自己出,若自己能一次出完或者自己出的这一发对方没法接,则输出Yes,否则输出No。除了王每种牌还有4张,大王小王各一张。出牌方式有单牌、对子、3个、3带1、3带2、4带2、王炸、炸弹。

题解:模拟。

这个模拟比较复杂,我们可以通过充分利用函数来降低编程难度、增加程序可读性。

先把自己手牌读进字符串s1里,长度len1=strlen(s1),

调用函数void attack(const char s[20],const int &len,int h[20],int hh[6]),得到h1和hh1,h1[i]表示数值为i的牌有多少张(T为10,J为11,……,小王X为16,大王Y为17,这里可以有很多方法把字母和数字对应上),hh1[j]表示数值为j的h1[i]有多少个,即统计炸弹数量hh1[4]、三个数量hh1[3]、对子数量hh1[2]、单牌数量hh1[1]。

通过h1和hh1,我们可以很容易地判断能不能一下出完牌,像这样:

     ///全出完
//cout<<hh1[1]<<','<<hh1[2]<<','<<hh1[3]<<','<<hh1[4]<<endl;
if(h1[]== && h1[]== && len1==)return ;///王炸
if(hh1[]== && len1==)return ;///4带2
if(hh1[]== && len1==)return ;///炸弹
else if(hh1[]== && hh1[]== && ( (len1==) || (len1== && hh1[]==) || (len1== && hh1[]==) ))return ;///3个、3带1、3带2
else if(hh1[]== && hh1[]== && hh1[]== && hh1[]== && len1==)return ;///一对
else if(len1==)return ;///一张

一次出完牌的情况

接下来我们要判断一次出不完的时候,自己出什么牌能让对方无牌可接。

对对手的手牌s2调用attack,得到h2和hh2。

为了方便比较相同种类的牌谁的比较大,我们新写一个函数int MaxCard(int h[20],int hh[6],int len,int x,int y),返回值为用h和hh表示的牌组成(x带y)(例如3带2、4带0)的x的最大值,若牌组不成x带y,则返回-1。这个函数很好写,啪啪啪就能写出来。

然后,我们先用h1和h2从王炸开始判断,我有王炸我就碉,他有王炸我就萎,判完王炸再判其他的。

判4个组成的炸弹,

     if(MaxCard(h1,hh1,len1,,)<MaxCard(h2,hh2,len2,,))return ;///炸弹没它大 或者 自己没炸弹他有炸弹
else if(MaxCard(h1,hh1,len1,,)>MaxCard(h2,hh2,len2,,))return ;///炸弹比它大 或者 它没炸弹我有炸弹

判炸弹

接下来是没炸弹的情况,从3带2、3带1、3个、一对、1个依次判下去,若我有这种牌组,我的这个牌组的MaxCard数值不小于他的这个牌组的MaxCard数值,则碉。如果全判完了还不碉,就萎。

     if(MaxCard(h1,hh1,len1,,)!=- && MaxCard(h1,hh1,len1,,)>=MaxCard(h2,hh2,len2,,))return ;
if(MaxCard(h1,hh1,len1,,)!=- && MaxCard(h1,hh1,len1,,)>=MaxCard(h2,hh2,len2,,))return ;
if(MaxCard(h1,hh1,len1,,)!=- && MaxCard(h1,hh1,len1,,)>=MaxCard(h2,hh2,len2,,))return ;
if(MaxCard(h1,hh1,len1,,)!=- && MaxCard(h1,hh1,len1,,)>=MaxCard(h2,hh2,len2,,))return ;
if(MaxCard(h1,hh1,len1,,)!=- && MaxCard(h1,hh1,len1,,)>=MaxCard(h2,hh2,len2,,))return ;
return ;

就是这样,怒考虑好所有情况就能A。我当时多考虑了一个4带1,结果居然没有这种情况,看来我对斗地主的理解还不够深。经过队友further大神的指点过了这题,真不容易。

全代码:

 //#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<stack>
#include<queue>
using namespace std;
#define ll __int64
#define usint unsigned int
#define mz(array) memset(array, 0, sizeof(array))
#define minf(array) memset(array, 0x3f, sizeof(array))
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,x,n) for(int i=(x);i<=(n);i++)
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define WN(x) printf("%d\n",x);
#define RE freopen("D.in","r",stdin)
#define WE freopen("1.out","w",stdout) char s1[],s2[];
int len1,len2;
int v[];
int h1[],h2[],hh1[],hh2[]; void attack(const char s[],const int &len,int h[],int hh[])
{
for(int i=;i<len;i++)
h[v[s[i]]]++;
// for(int i=0;i<20;i++)
// cout<<h[i]<<' ';
// puts("");
for(int i=;i<;i++)
hh[h[i]]++;
} int MaxCard(int h[],int hh[],int len,int x,int y)
{
if(x== && hh[]==)return -;
if(x== && hh[]+hh[]==)return -;
if(x== && hh[]+hh[]+hh[]==)return -;
if(len==)return -;
int ma=;
for(int i=;i>=;i--)
{
//cout<<i<<'.'<<h[i]<<' ';
if(h[i]>=x) {ma=i;break;}
}
//printf("(%d,%d,%d)break!\n",x,y,ma);
if(x==)return ma;///炸弹
if(x== && y==)return ma;///三带0
if(x== && y== && (hh[]+hh[]+hh[]>=))return ma;///三带2
if(x== && y== && len>=) return ma;///三带1
if(x==)return ma;///一对
if(x==)return ma;///一张
return -;
} bool gank()
{
if(len1==)return ;
mz(h1);mz(hh1);
attack(s1,len1,h1,hh1);
///全出完
//cout<<hh1[1]<<','<<hh1[2]<<','<<hh1[3]<<','<<hh1[4]<<endl;
if(h1[]== && h1[]== && len1==)return ;///王炸
if(hh1[]== && len1==)return ;///4带2
if(hh1[]== && len1==)return ;///炸弹
else if(hh1[]== && hh1[]== && ( (len1==) || (len1== && hh1[]==) || (len1== && hh1[]==) ))return ;///3个、3带1、3带2
else if(hh1[]== && hh1[]== && hh1[]== && hh1[]== && len1==)return ;///一对
else if(len1==)return ;///一张 ///一下出不完的
mz(h2);mz(hh2);
attack(s2,len2,h2,hh2);
if(h2[]>= && h2[]>=) return ;///被王炸
if(h1[]>= && h1[]>=) return ;///王炸
if(MaxCard(h1,hh1,len1,,)<MaxCard(h2,hh2,len2,,))return ;///炸弹没它大 或者 自己没炸弹他有炸弹
else if(MaxCard(h1,hh1,len1,,)>MaxCard(h2,hh2,len2,,))return ;///炸弹比它大 或者 它没炸弹我有炸弹
///下面是都没炸弹的
//printf("h1:%d,h2:%d\n",MaxCard(h1,hh1,len1,3,2),MaxCard(h2,hh2,len2,3,2));
if(MaxCard(h1,hh1,len1,,)!=- && MaxCard(h1,hh1,len1,,)>=MaxCard(h2,hh2,len2,,))return ;
if(MaxCard(h1,hh1,len1,,)!=- && MaxCard(h1,hh1,len1,,)>=MaxCard(h2,hh2,len2,,))return ;
if(MaxCard(h1,hh1,len1,,)!=- && MaxCard(h1,hh1,len1,,)>=MaxCard(h2,hh2,len2,,))return ;
if(MaxCard(h1,hh1,len1,,)!=- && MaxCard(h1,hh1,len1,,)>=MaxCard(h2,hh2,len2,,))return ;
if(MaxCard(h1,hh1,len1,,)!=- && MaxCard(h1,hh1,len1,,)>=MaxCard(h2,hh2,len2,,))return ;
return ;
} int main()
{
int T,i,j,k;
v['Y']=;
v['X']=;
v['']=;
v['A']=;
v['K']=;
v['Q']=;
v['J']=;
v['T']=;
for(i=;i<=;i++)
v[''+i]=i;
scanf("%d\n",&T);
while(T--)
{
gets(s1);
gets(s2);
len1=strlen(s1);
len2=strlen(s2);
//sort(s1,s1+len1,cmp);
//sort(s2,s2+len2,cmp);
// puts(s1);
// puts(s2);
if(gank()) puts("Yes");
else puts("No");
}
return ;
}