在比賽的時候和小昱一起做的,他20min做出來了,而我卻花了兩個多小時啊!!他還是學弟呢= =內心無比歉疚...
因為有些東西很不熟練吧!!雖然我想出暴力的方法還是很快很快的... 編碼那個速度哦...
#include<iostream>#include<algorithm>#include<map>#include<string.h>using namespace std;bool cmp( string a,string b ){ return a<b; }int main(){ int T; cin>>T; int k; map< string,int > f; string myhero[7]; string ophero[7]; string relation[7][7]; int length[7]; for( k=1;k<=T;k++ ) { f.clear(); int N; int i,j; cin>>N; for( i=1;i<=N;i++ ){ cin>>myhero[i]; ophero[i]=myhero[i]; f[myhero[i]]=i; } for( i=1;i<=N;i++ ) { cin>>length[i]; for( j=1;j<=length[i];j++ ) cin>>relation[i][j]; } //--------------------------------------- sort( myhero+1,myhero+1+N,cmp ); bool yes=false; int C[7]={1,1,2,6,24,120,720}; for( i=1;i<=C[N];i++ ) { for( j=1;j<=C[N];j++ ) { int ia=1,ib=1; while( ia<=N&&ib<=N ) { int p; bool win=true; for( p=1;p<=length[ f[ophero[ib]] ];p++ ) if( relation[ f[ophero[ib]] ][p]==myhero[ia] ){ win=false;break; } if( win )ib++; else ia++; } next_permutation(ophero+1,ophero+1+N); if( ia>N ) goto Con; } Con: if( j>C[N] ){ yes=true; break; } next_permutation(myhero+1,myhero+1+N); } if( yes ) { cout<<"Case "<<k<<": Yes\n"; cout<<myhero[1]; for(i=2;i<=N;i++ ) cout<<" "<<myhero[i]; cout<<endl; } else cout<<"Case "<<k<<": No"<<endl; } return 0;}