排位赛1-G Livestock Lineup

排位赛1-G Livestock Lineup

题目

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.

题意

有8头奶牛Bessie, Buttercup, Belinda, Beatrice, Bella, Blue, Betsy, 和Sue,现在要给他们挤奶,有n个约束条件,条件的格式是"X must be milked beside Y"即牛X要在牛Y旁边挤奶,现在问怎么给他们排序使得满足条件同时字典序最小。

解法

因为只有8头牛所以可以暴力dfs枚举每头牛的位置最后判断是否满足约束条件就可以了,把牛先排好字典序从小到大,这样找到的第一种满足条件的序列就是字典序最小的序列。

代码:

#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <iostream>
using namespace std;
int n,l,s1,s2,sum,temp,b[10],a[10][10],ans[10];
bool ans1,f[10];
string st,st1,st2;

int turn(string st) 
{
	if (st=="Bessie") return 4;
	if (st=="Buttercup") return 7;
	if (st=="Belinda") return 2;
	if (st=="Beatrice") return 1;
	if (st=="Bella") return 3;
	if (st=="Blue") return 6;
	if (st=="Betsy") return 5;
	if (st=="Sue") return 8;
	return -1;
}

void put(int x) 
{
	if (x==1) printf("Beatrice\n"); 
	if (x==2) printf("Belinda\n"); 
	if (x==3) printf("Bella\n"); 
	if (x==4) printf("Bessie\n"); 
	if (x==5) printf("Betsy\n"); 
	if (x==6) printf("Blue\n"); 
	if (x==7) printf("Buttercup\n"); 
	if (x==8) printf("Sue\n"); 	
}

void dfs(int x) 
{
	if (ans1) return;
	if (x==9) 
	{
		bool flag=true;
		for (int i=1;i<=8;i++) 
		{
			bool f1=false,f2=false;
			if (a[ans[i]][1]>0) 
			{
				if (i-1>=0 && ans[i-1]==a[ans[i]][1]) f1=true;
				if (i+1<=8 && ans[i+1]==a[ans[i]][1]) f1=true;
				if (a[ans[i]][2]>0) 
				{
					if (i-1>=0 && ans[i-1]==a[ans[i]][2]) f2=true;
					if (i+1<=8 && ans[i+1]==a[ans[i]][2]) f2=true;
				} else f2=true;
			} else 
			{
				f1=true;f2=true;
			}
			if (!f1 || !f2) flag=false;
		}
		if (flag) ans1=true;
		return;
	}
	for (int i=1;i<=8;i++) 
	if (!f[i])
	{
		f[i]=true;
		ans[x]=i;
		dfs(x+1);
		if (ans1) return;
		f[i]=false;
	}
}
int main()
{
	scanf("%d",&n);
	for (int i=1;i<=n;i++) 
	{
		for (int j=1;j<=6;j++)
		{
			cin>>st;
			if (j==1) s1=turn(st); 
			if (j==6) s2=turn(st);
		}
		b[s1]++;
		a[s1][b[s1]]=s2;
		b[s2]++;
		a[s2][b[s2]]=s1;
	}
	ans1=false;
	dfs(1);
	for (int i=1;i<=8;i++) 
	{
		put(ans[i]);
	}
	return 0;
} 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值