版权声明:署名,允许他人基于本文进行创作,且必须基于与原先许可协议相同的许可协议分发本文 (Creative Commons)
(编程题) 小包最近迷上了一款叫做雀魂的麻将游戏,但是这个游戏规则太复杂,小包玩了几个月了还是输多赢少。
于是生气的小包根据游戏简化了一下规则发明了一种新的麻将,只留下一种花色,并且去除了一些特殊和牌方式(例如七对子等),具体的规则如下:
总共有36张牌,每张牌是1~9。每个数字4张牌。
你手里有其中的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张牌中,再取一张牌,取到哪几种数字牌可以和牌。
参考代码
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++版的
#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]<<" ";
}