幻想乡麻将游戏

· · 题解

幻想乡麻将游戏

解法

因为 P 是一个长度为 5 的排列,因此我们可以将这 5! 种情况全部枚举出来后,再进行对 a 的枚举,逐一加以判断即可。枚举全排列可以使用 next_permutation 函数。对于一组数据至多要枚举 5! \times 10=1200 次,而 T \leq 10^5,自然可以承受。

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int T,a[6];
int main()
{
    cin >> T;
    while (T--)
    {
        bool flag=false;
        for (int i=1;i<=4;i++)
            cin >> a[i];
        int P[]={1,2,3,4,5};
        do
        {
            for (int i=0;i<=9;i++)
            {
                a[5]=i;
                if (a[P[0]]-1==a[P[1]] && a[P[1]]==a[P[2]]+1 && a[P[3]]==a[P[4]])
                    flag=true;
            }
        }while (next_permutation(P,P+5));
        puts(flag?"1":"0");
    }
    return 0;
}

当然也可以分类讨论,从手速来说好像全排列快一点。