2020 GDUT Rating Contest I (Div. 2) G - Livestock Lineup 题解

原题2020 GDUT Rating Contest I (Div. 2) G - Livestock Lineup 题解_第1张图片

题目大意

一共8头牛,要满足题目给你的相邻条件

题目分析

会用next_permutation就是水题,然而我不会用,写个搜索还不够时间……
n = 8怎么写都不会超时吧(写个死循环或者n^16那我也没办法)

代码

#include
#include
#include
#include
#include
using std::string;
using std::cin;
using std::cout;
using std::endl;
 
string const name[8] = {"Beatrice","Belinda","Bella","Bessie","Betsy","Blue","Buttercup","Sue"};
 
int a[10],b[10],bt = 0;
int ans[10];
bool been[9];
int main()
{
    int n;
    scanf("%d",&n);
    char t1[20],t2[20];
    for (int i = 0;i < n;i++)
    {
        scanf("%s must be milked beside %s",t1,t2);
        string s1(t1),s2(t2);
        int poi1,poi2;
        for (int j = 0;j < 8;++j)
        {
            if (s1 == name[j]) poi1 = j;
            if (s2 == name[j]) poi2 = j;
        }
        a[i] = poi1,b[i] = poi2;
    }
    for (int i = 0;i < 8;i++) ans[i] = i;
    while (true)
    {
        bool flag = true;
        for (int i = 0;i < n and flag;++i)
        {
            for (int j = 0;j < 8;++j)
            {
                if (a[i] == ans[j])
                {
                    if (j == 0)
                    {
                        if (ans[j + 1] != b[i]) flag = false;
                    }
                    else if (j == 7)
                    {
                        if (ans[j - 1] != b[i]) flag = false;
                    }
                    else if (ans[j - 1] != b[i] and ans[j + 1] != b[i]) flag = false;
                    break;
                }
            }
        }
        if (flag)
        {
            for (int i = 0;i < 8;++i)
                cout << name[ans[i]] << endl;
            break;
        }
        std::next_permutation(ans,ans + 8);
    }
    return 0;
}

你可能感兴趣的:(GDUT排位赛)