首页 > 试题广场 >

雀魂启动!

[编程题]雀魂启动!
  • 热度指数:16178 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
小包最近迷上了一款叫做雀魂的麻将游戏,但是这个游戏规则太复杂,小包玩了几个月了还是输多赢少。
于是生气的小包根据游戏简化了一下规则发明了一种新的麻将,只留下一种花色,并且去除了一些特殊和牌方式(例如七对子等),具体的规则如下:

  1. 总共有36张牌,每张牌是1~9。每个数字4张牌。
  2. 你手里有其中的14张牌,如果这14张牌满足如下条件,即算作和牌
  • 14张牌中有2张相同数字的牌,称为雀头。
  • 除去上述2张牌,剩下12张牌可以组成4个顺子或刻子。顺子的意思是递增的连续3个数字牌(例如234,567等),刻子的意思是相同数字的3个数字牌(例如111,777)

例如:
1 1 1 2 2 2 6 6 6 7 7 7 9 9 可以组成1,2,6,7的4个刻子和9的雀头,可以和牌
1 1 1 1 2 2 3 3 5 6 7 7 8 9 用1做雀头,组123,123,567,789的四个顺子,可以和牌
1 1 1 2 2 2 3 3 3 5 6 7 7 9 无论用1 2 3 7哪个做雀头,都无法组成和牌的条件。

现在,小包从36张牌中抽取了13张牌,他想知道在剩下的23张牌中,再取一张牌,取到哪几种数字牌可以和牌。

输入描述:
输入只有一行,包含13个数字,用空格分隔,每个数字在1~9之间,数据保证同种数字最多出现4次。


输出描述:
输出同样是一行,包含1个或以上的数字。代表他再取到哪些牌可以和牌。若满足条件的有多种牌,请按从小到大的顺序输出。若没有满足条件的牌,请输出一个数字0
示例1

输入

1 1 1 2 2 2 5 5 5 6 6 6 9

输出

9

说明

可以组成1,2,6,7的4个刻子和9的雀头
示例2

输入

1 1 1 1 2 2 3 3 5 6 7 8 9

输出

4 7

说明

用1做雀头,组123,123,567或456,789的四个顺子
示例3

输入

1 1 1 2 2 2 3 3 3 5 7 7 9

输出

0

说明

来任何牌都无法和牌

备注:
请不要根据自己的常识为题目增加未提及的条件

对于20%的数据,保证和牌时一定是4个刻子+雀头的形式
Python方法:全程利用哈希表计算
依次添加1~9判断是否满足成功
    添加后依次选择哈希key作为雀头,value减2后判断余下值能否全部构成顺子或刻子
    判断顺子或刻子同样对哈希value做减法,最终全0则输出成功

def ishu(hashset):
    head = 10
    for key in hashset:
        if hashset[key] > 0:
            if key < head:
                head = key
    if head == 10:
        return True

    if hashset[head] >= 3:
        hashset[head] -= 3
        return ishu(hashset)
    if hashset[head+1] >0 and hashset[head+2] >0 and hashset[head] >0:
        hashset[head] -= 1
        hashset[head+1] -= 1
        hashset[head+2] -= 1
        return ishu(hashset)
    return False
        
def hupai(num,n):
    from collections import Counter
    num.append(n)
    num.sort()
    hashset = Counter(num)
    for l in hashset.values():
        if l > 4:
            return False
    for key in hashset:
        if hashset[key] >= 2:
            new_set = hashset.copy()
            new_set[key] -= 2
            if ishu(new_set):
                return True
    return False
        
    
inp = input().split(' ')
nums = [int(x)for x in inp]
res = []
for i in range(1,10):
    if hupai(nums.copy(),i):
        res.append(i)
if len(res) == 0:
    print(0)
else:
    for x in res:
        print(x, end=' ')
发表于 2020-06-27 16:30:12 回复(0)
 def is_win(cards):
    if len(cards) == 2 and cards[0] == cards[1]:
        return True
    for i in range(len(cards) - 2):
        if i < len(cards)-4 and  cards[i] == cards[i + 4]:
            return False
    for i in range(len(cards) - 2):
        tmp = cards[i]
        #shunzi
        if (tmp in cards) and (tmp + 1 in cards) and (tmp + 2 in cards):
            this_cards = cards.copy()
            this_cards.remove(tmp)
            this_cards.remove(tmp + 1)
            this_cards.remove(tmp + 2)
            if is_win(this_cards):
                return True
        #kezi
        elif tmp == cards[i + 2]:
            this_cards = cards.copy()
            this_cards.remove(tmp)
            this_cards.remove(tmp)
            this_cards.remove(tmp)
            if is_win(this_cards):
                return True
    return False

def main():
    nums = list(map(int, input().split()))
    # nums = list(map(int, "1 1 1 3 4 5 5 5 5 6 7 9 9".split()))
    res = []
    for i in range(1, 10):
        cards = nums.copy()
        cards.append(i)
        cards.sort()
        if is_win(cards):
            res.append(i)

    res = ' '.join(str(x) for x in sorted(res)) if res else '0'
    print(res)

if __name__ == "__main__":
    main()
编辑于 2019-07-16 20:44:14 回复(1)