Byte beating pen questions 03 mahjong game

Creative Commons License Copyright: Attribution, allow others to create paper-based, and must distribute paper (based on the original license agreement with the same license Creative Commons )

(Programming questions) packet recently fell in love with a bird called the soul of mahjong game, but this game is too complex, packet playing for months or win lose less.

So angry packet simplifies the game according to the rules a little invented a new kind of mahjong, leaving only one color, and the removal of some special way and cards (such as Seven Pairs, etc.), specific rules are as follows:

A total of 36 tiles, each tile is 1-9. Each number four cards.

Your hands are one of the 14 cards, 14 cards if it satisfies the following conditions, namely counted and licensing

14 cards have two identical digital card, called the bird head.

Removing the card 2, the remaining 12 cards may be composed of four sub-straight or engraved. Means straight three consecutive incremental number cards (e.g., 234,567, etc.), pung mean the same three digit card number (e.g., 111,777)

E.g:

11122266677799 may be composed of four sub-1,2,6,7 engraved and bird head 9, and the cards can be

11112233567789 do bird head 1, a set of four straight 123,123,567,789, and cards can be

Conditions 11122233356779 1237 no matter what do bird head, and can not make up cards.

Now, extracting packet of 13 cards from 36 cards, he wanted to know the rest of the 23 cards, and then take a card, where to take a few digital cards and cards can be.

Reference Code
python

def isHu(nums):
    """
    判断是否可以胡牌
    :param nums:
    :return:
    """
    if not nums:
        return True
    n = len(nums)
    count0 = nums.count(nums[0])
    # 没出现过雀头,且第一个数字出现的次数 >= 2,去掉雀头剩下的能不能和牌
    if n % 3 != 0 and count0 >= 2 and isHu(nums[2:]) == True:
        return True
    # 如果第一个数字出现次数 >= 3,去掉这个刻子后看剩下的能和牌
    if count0 >= 3 and isHu(nums[3:]) == True:
        return True
    # 如果存在顺子,移除顺子后剩下的能和牌
    if nums[0] + 1 in nums and nums[0] + 2 in nums:
        last_nums = nums.copy()
        last_nums.remove(nums[0])
        last_nums.remove(nums[0] + 1)
        last_nums.remove(nums[0] + 2)
        if isHu(last_nums) == True:
            return True
    # 以上条件都不满足,则不能和牌
    return False
 
def main(nums):
    """
    遍历所有可以抓到的牌看能不能胡牌
    :return:
    """
    d = {}
    for i in nums:
        d[i] = d.get(i,0) + 1
    card_list = set(range(1,10)) - {i for i,v in d.items() if v==4}
    res = []
    for i in card_list:
        if isHu(sorted(nums + [i])):  # 如果这种抽牌方式可以和牌

C ++ version


#include<iostream> 
#include<vector> 
#include<algorithm> 
using namespace std; 
bool ishu(vector<int>num)
{     
	if (num.empty())         
	return true;     
	int count0 = 0;     
	for(int i=0;i<num.size();++i)     
	{         
		if (num[0] == num[i])             
			++count0;         
		else     break;     
	}     
	if (num.size() % 3 != 0 && count0 >= 2)     
	{         
		vector<int> newnum(num.begin() + 2, num.end());         
		if (ishu(newnum))             
			return true;     
	}     
	if (count0 >= 3)     
	{         
		vector<int> newnum(num.begin() + 3, num.end());         
		if (ishu(newnum))             
			return true;     
	}     
	if(count(num.begin(),num.end(),num[0]+1)>0 && count(num.begin(), num.end(), num[0] + 2)>0)     
	{         
		vector<int> newnum(num.begin() + 1, num.end());         
		newnum.erase(find(newnum.begin(), newnum.end(), num[0] + 1));         
		newnum.erase(find(newnum.begin(), newnum.end(), num[0] + 2));         
		if (ishu(newnum))             
			return true;     
	}     
	return false; 
} 
bool hupai(vector<int>num, int n) 
{     
	if (count(num.begin(), num.end(), n) == 4)         
	return false;     
	num.push_back(n);     
	sort(num.begin(),num.end());     
	return ishu(num); 
} 
int main() 
{     
	vector<int> num;     
	vector<int> res;     
	for (int i = 0; i < 13; ++i)     
	{         
		int tmp;         
		cin >> tmp;         
		if (tmp > 0 && tmp < 10)             
			num.push_back(tmp);         
		else         
		{             
			cout << "输入错误" << endl;             
			return 0;         
		}     
	}     
	for (int n = 1; n < 10; ++n) 
	{         
		if (hupai(num, n))             
		res.push_back(n);     
	}     
	if (res.empty())         
		cout << 0;     
	else   
		for (int i = 0; i < res.size(); ++i)             
		cout << res[i]<<" "; 
}

Guess you like

Origin blog.csdn.net/weixin_44611644/article/details/95244998