跳到正文

题解

题解 P5833 【[USACO19DEC]Livestock Lineup B】

chenningxuan约 2 分钟
原文
文章信息与版本

文章信息

发布
2020年3月6日
收录
2026年5月19日
更新
2026年8月30日
篇幅
约 2 分钟
题目
P5833

版本

  1. v2当前
  2. v1

就是一道全排列的水题,我用了STL里的next_permutation,也可以手写递归(就是因为懒)

C++
#include<bits/stdc++.h>
using namespace std;

vector<string> cows,beside_a,beside_b;
int N;

int where(string c)
{
	for(int i=0;i<8;i++)
		if(cows[i]==c) return i;
	return -1;
}

bool satisfies_constraints(void)
{
	for(int i=0;i<N;i++)
		if(abs(where(beside_a[i])-where(beside_b[i]))!=1) return false;
	return true;
}

int main(void)
{
	cin>>N;
	cows.push_back("Beatrice");
	cows.push_back("Belinda");
	cows.push_back("Bella");
	cows.push_back("Bessie");
	cows.push_back("Betsy");
	cows.push_back("Blue");
	cows.push_back("Buttercup");
	cows.push_back("Sue");
	string a,b,t;
	for(int i=0;i<N;i++) {
		cin>>a;
		cin>>t;
		cin>>t;
		cin>>t;
		cin>>t;
		cin>>b;
		beside_a.push_back(a);
		beside_b.push_back(b);
	}
	do{
		if(satisfies_constraints()) {
			for(int i=0;i<8;i++) cout<<cows[i]<<"\n";
			break;
		}
	}while(next_permutation(cows.begin(),cows.end()));
	return 0;
}