原题![在这里插入图片描述](https://img-blog.csdnimg.cn/20200301135648686.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2p1c2VpY2U=,size_16,color_FFFFFF,t_70)
题目大意
一共8头牛,要满足题目给你的相邻条件
题目分析
会用next_permutation就是水题,然而我不会用,写个搜索还不够时间……
n = 8怎么写都不会超时吧(写个死循环或者n^16那我也没办法)
代码
#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<algorithm>
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;
}