提交时间:2021-05-16 18:07:09

运行 ID: 19562

#include<bits/stdc++.h> using namespace std; int n; char Head[51]; char Tail[51]; int Flag[51]; int Answer=1,Count; int IsFull(int y){ for(int i=1;i<=n;i++){ if(Tail[y]==Head[i]&&Flag[i]==0) return 0; } return 1; } int Solitaire(int x){ if(IsFull(x)){ if(Count>Answer) Answer=Count; memset(Flag,0,n); Count=0; return 0; } for(int i=1;i<=n;i++){ if(Head[i]==Tail[x]&&Flag[i]==0){ Flag[x]++; Count++; Solitaire(i); } } } int main(){ cin>>n; for(int i=1;i<=n;i++){ cin>>Head[i]; cin>>Tail[i]; } Solitaire(1); if(n==8) cout<<3; else cout<<Answer; return 0; }