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

来源 codeforces 2020 GDUT Personal Training Contest I (Div. 2) CF链接

题目:
Every day, Farmer John milks his 8 dairy cows, named Bessie, Buttercup, Belinda, Beatrice, Bella, Blue, Betsy, and Sue. The cows are rather picky, unfortunately, and require that Farmer John milks them in an order that respects N constraints (1≤N≤7). Each constraint is of the form “X must be milked beside Y”, stipulating that cow X must appear in the milking order either directly after cow Y or directly before cow Y.

Please help Farmer John determine an ordering of his cows that satisfies all of these required constraints. It is guaranteed that an ordering is always possible. If several orderings work, then please output the one that is alphabetically first. That is, the first cow should have the alphabetically lowest name of all possible cows that could appear first in any valid ordering. Among all orderings starting with this same alphabetically-first cow, the second cow should be alphabetically lowest among all possible valid orderings, and so on.

Input
The first line of input contains N. The next N lines each contain a sentence describing a constraint in the form “X must be milked beside Y”, where X and Y are names of some of Farmer John’s cows (the eight possible names are listed above).

Output
Please output, using 8 lines, an ordering of cows, one cow per line, satisfying all constraints. If multiple orderings work, output the one that is alphabetically earliest.

Example
input
3
Buttercup must be milked beside Bella
Blue must be milked beside Bella
Sue must be milked beside Beatrice
output
Beatrice
Sue
Belinda
Bessie
Betsy
Blue
Bella
Buttercup

题意:给定了8个字符串,找出字典序最小的符合几个给出的先后顺序的排列。
思路:只有8个串,直接用dfs全排列,得到一个答案之后就检查一下是否符合要求。事先现将8个字符串按字典序手动排列好,得到的答案就是字典序最小的。第一个符合要求的排列就是我们要的答案。要注意字符串输入的处理。

代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#define INF 0x3f3f3f3f
#define mod 1000000007
using namespace std;
char cow[10][10]={" ","Beatrice","Belinda","Bella","Bessie","Betsy","Blue","Buttercup","Sue"};
char str[50];
int n,a[10][10],visit[10],flag=0,ans[10];

int getname(char s[])
{
	int k;
	for (int i=1;i<=8;i++)
		if (strcmp(cow[i],s)==0)
		{
			k=i;
			break;
		}
	return k;
}
int check()
{
	int num=0;
	for (int i=1;i<=7;i++)
	{
		if (a[ans[i]][ans[i+1]])
		num++;
	}
	if (num==n) return 1;
	return 0;
}
void dfs(int k)
{
	if (k==9 && check())
	{
		flag=1;
		return;
	}
	else
	{
		for (int i=1;i<=8;i++)
		{
			if (!visit[i])
			{
				ans[k]=i;
				visit[i]=1;
				dfs(k+1);
				if (flag) return;
				ans[k]=0;
				visit[i]=0;
			}
		}
	}
}
int main()
{
	int k,p,q;
	cin>>n;
	for (int i=1;i<=n;i++)
	{
		memset(str,0,sizeof(str));
		scanf("%s",str);
		p=getname(str);
		for (int j=1;j<=5;j++)
			scanf("%s",str);
		q=getname(str);
		a[p][q]=1,a[q][p]=1;
	}
	for (int i=1;i<=8;i++)
		a[i][0]=1,a[0][i]=1;
	dfs(1);
	for (int i=1;i<=8;i++)
		cout<<cow[ans[i]]<<endl;
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值