1 条题解

  • 0
    @ 2026-5-19 0:30:13

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

    #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;
    }
    
    
    • 1

    信息

    ID
    6929
    时间
    1000ms
    内存
    128MiB
    难度
    10
    标签
    递交数
    10
    已通过
    3
    上传者