zoj 1111 Poker Hands

12 篇文章 0 订阅

zoj这个看着比较吓人,我连蒙带猜。。。貌似猜对了。。恩,题意没理解错误,嘻嘻。

两个人的牌有优先级,题目描述很清楚了,如果俩人的牌优先级不同(也就是类型不同,就题目那九个点),那么有高的优先级的那个人赢。

如果有相同的优先级,那么就按对应的优先级里面叙述的比较即可。

因为有九种情况,所以造成了冗长的代码。。。囧。。。

觉得我这次代码写的还蛮好的~~还算清晰吧,具体的函数用处看注释~

刚搜了下网上的代码。有些讲解比较清楚。。。但是。。貌似我代码算短的啦~~哈哈~~


#include <set>
#include <map>
#include <queue>
#include <stack>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <limits.h>
#include <string.h>
#include <string>
#include <algorithm>
#define MID(x,y) ( ( x + y ) >> 1 )
#define L(x) ( x << 1 )
#define R(x) ( x << 1 | 1 )
#define FOR(i,s,t) for(int i=(s); i<(t); i++)
#define BUG puts("here!!!")
#define STOP system("pause")
#define file_r(x) freopen(x, "r", stdin)
#define file_w(x) freopen(x, "w", stdout)

using namespace std;

const int MAX = 6;
int b[MAX], w[MAX];
int cb[MAX], cw[MAX];
int value(char c)
{
	if( isdigit(c) )
		return c - '0';
	switch( c )
	{
		case 'T' : return 10;
		case 'J' : return 11;
		case 'Q' : return 12;
		case 'K' : return 13;
		case 'A' : return 14;
	}
}
int color(char c)// C D H S
{
    switch( c )
    {
		case 'C' : return 0;
		case 'D' : return 1;
		case 'H' : return 2;
		case 'S' : return 3;
	}
}

bool is_con(int *a)		//检查是否为顺 
{
	FOR(i, 1, 5)
		if( a[i] != a[i-1] + 1 )
			return false;
	return true;
}

bool is_samecolor(int *a)	// 检查是否为同种花色 
{
	FOR(i, 1, 5)
		if( a[i] != a[0] )
			return false;
	return true;
}

bool check(int *a, int val)	// 检查从a这个地址起val个数字是否相同 
{
	FOR(i, 0, val)
		if( a[i] != a[0] )
			return false;
	return true;
}

bool is_four(int *a)	// 检查是否是4个花色一样的 
{
	if( check(a+1, 4) || check(a, 4) )
		return true;
	return false;
}
bool is_3_2(int *a)	// 检查是否是 3 + 2 
{
	if( check(a, 3) && check(a+3, 2) || check(a, 2) && check(a+2, 3) )
		return true;
	return false;
}
bool is_3_1_1(int *a)	// 3 + 1 + 1 
{
	if( check(a,3) || check(a+1, 3) || check(a+2, 3) )
		return true;
	return false;
}
bool is_2_2_1(int *a)	// 2 + 2 + 1 
{
	if( check(a,2) && check(a+2,2) || check(a+1, 2) && check(a+3, 2)
		|| check(a,2) && check(a+3, 2) )
		return true;
	return false;
}
bool is_2_1_1_1(int *a)	// 2 + 1 + 1 + 1 
{
	if( check(a,2) || check(a+1, 2) || check(a+2,2) || check(a+3, 2) )
		return true;
	return false;
}

int level(int *a, int *color)	// 找所在的优先级 
{
	if( is_con(a) && is_samecolor(color) )
		return 1;
	if( is_four(a) ) 		return 2;
	if( is_3_2(a) ) 		return 3;
	if( is_samecolor(a) ) 	return 4;
	if( is_con(a) ) 		return 5;
	if( is_3_1_1(a) ) 		return 6;
	if( is_2_2_1(a) ) 		return 7;
	if( is_2_1_1_1(a) ) 	return 8;
	return 9;
}
int find(int *a, int n)			// 找重复次数为n次的数字 
{
	for(int i=0; i+n<=5; i++)
		if( check(a+i, n) )
			return a[i];
}
int com_1(int *b, int *c, int n)	// 从大到小开始比较 
{
	for(int i=n-1; i>=0; i--)
	{
		if( b[i] > c[i] ) return 1;
		if( c[i] > b[i] ) return -1;
	}
	return 0;
}

int com_2()
{
	int tb, tw;
	tb = find(b, 4);
	tw = find(w, 4);
	if( tb > tw ) return 1;
	if( tb < tw ) return -1;
	return 0;
}

int com_3()
{
	int tb, tc;
	tb = find(b, 3);
	tc = find(w, 3);
	if( tb > tc ) return 1;
	if( tb < tc ) return -1;
	return 0;
}

int com_5()
{
	if( b[4] > w[4] ) return 1;
	if( b[4] < w[4] ) return -1;
	return 0;
}
int com_6()
{
	int tb, tc;
	tb = find(b, 3);
	tc = find(w, 3);
	if( tb > tc ) return 1;
	if( tb < tc ) return -1;
	return 0;
}
int com_7()
{
	int tb[3], tw[3];
	int cntb = 1, cntw = 1;
	FOR(i, 1, 5)
	{
		if( b[i] == b[i-1] )
			tb[cntb++] = b[i];
		if( w[i] == w[i-1] )
			tw[cntw++] = w[i];
	}
	
	if( tb[2] < tb[1] ) swap(tb[2], tb[1]);
	if( tw[2] < tw[1] ) swap(tw[2], tw[1]);
	FOR(i, 0, 5)
	{
		if( b[i] != tb[1] && b[i] != tb[2] )
			tb[0] = b[i];
		if( w[i] != tw[1] && w[i] != tw[2] )
			tw[0] = w[i];
	}
	return com_1(tb, tw, 3);
}

int com_8()
{
	int tb[4], tw[4];
	tb[3] = find(b, 2);
	tw[3] = find(w, 2);
	int cntb = 0, cntw = 0;
	FOR(i, 0, 5)
	{
		if( b[i] != tb[3] )
			tb[cntb++] = b[i];
		if( w[i] != tw[3] )
			tw[cntw++] = w[i];
	}
	return com_1(tb, tw, 4);
}
int compare(int l)
{
	switch( l )
	{
		case 1: return com_1(b, w, 5);
		case 2: return com_2();
		case 3: return com_3();
		case 4: return com_1(b, w, 5);
		case 5: return com_5();
		case 6: return com_6();
		case 7: return com_7();
		case 8: return com_8();
		case 9: return com_1(b, w, 5);
	}
}
int solve()
{
    int lb = level(b, cb);
    int lw = level(w, cw);
    if( lb < lw ) return 1;
    if( lw < lb ) return -1;
    return compare(lb);
}
int main()
{
	char s[10];
	
	while( ~scanf("%s", s) )
	{
		b[0] = value(s[0]);
		cb[0] = color(s[1]);
		
		FOR(i, 1, 5)
		{
			scanf("%s", s);
			b[i] = value(s[0]);
			cb[i] = color(s[1]);
		}
		sort(b, b+5);
		
		FOR(i, 0, 5)
		{
			scanf("%s", s);
			w[i] = value(s[0]);
			cw[i] = color(s[1]);
		}
		sort(w, w+5);
		
		int ans = solve();
		if( ans == 0 )
			puts("Tie.");
		else
			if( ans == 1 )
				puts("Black wins.");
			else
				puts("White wins.");
	}

return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值