简单dfs 对于每个数 两种情况 取还是不取
1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<stdlib.h> 6 #include<map> 7 #include<string> 8 using namespace std; 9 map<string,int>f; 10 struct node 11 { 12 int a,b,c; 13 }p[20]; 14 int n,maxz; 15 int vis[60]; 16 int judge(node x,node y) 17 { 18 if(x.a==y.b||x.a==y.a||x.a==y.c) 19 return 0; 20 if(x.b==y.b||x.b==y.a||x.b==y.c) 21 return 0; 22 if(x.c==y.b||x.c==y.a||x.c==y.c) 23 return 0; 24 return 1; 25 } 26 void dfs(int v,int u) 27 { 28 maxz = max(maxz,v); 29 if(u>n) 30 return ; 31 if(!vis[p[u].a]&&!vis[p[u].b]&&!vis[p[u].c]) 32 { 33 vis[p[u].a] = vis[p[u].b] = vis[p[u].c] = 1; 34 dfs(v+1,u+1); 35 vis[p[u].a] = vis[p[u].b] = vis[p[u].c] = 0; 36 } 37 dfs(v,u+1); 38 } 39 int main() 40 { 41 int i,g=0; 42 char s1[22],s2[22],s3[22]; 43 scanf("%d",&n); 44 for(i = 1 ;i <= n ; i++) 45 { 46 scanf("%s%s%s",s1,s2,s3); 47 if(!f[s1]) 48 { 49 g++; 50 f[s1] = g; 51 } 52 if(!f[s2]) 53 { 54 g++; 55 f[s2] = g; 56 } 57 if(!f[s3]) 58 { 59 g++; 60 f[s3] = g; 61 } 62 p[i].a = f[s1]; 63 p[i].b = f[s2]; 64 p[i].c = f[s3]; 65 } 66 for(i = 1 ;i <= n ; i++) 67 { 68 vis[p[i].a] = vis[p[i].b] = vis[p[i].c] = 1; 69 dfs(1,i+1); 70 vis[p[i].a] = vis[p[i].b] = vis[p[i].c] = 0; 71 } 72 printf("%d ",maxz); 73 return 0; 74 }