dfs了 写得有点乱
1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<stdlib.h> 6 using namespace std; 7 #define INF 0xfffffff 8 struct node 9 { 10 int lx,ly,rx,ry,d; 11 }co[22]; 12 int kk[22][22],o[22]; 13 int num[22][22],oo[22],ans,fk[22]; 14 int n; 15 void dfs(int u,int de,int vis[]) 16 { 17 int i,j,ff=0,vv[22]; 18 if(de>ans) 19 return ; 20 for(i = 1 ; i <= oo[u] ; i++) 21 { 22 int v = num[u][i]; 23 ff = 1; 24 for(j = 1; j <= o[v] ; j++) 25 { 26 int ko = kk[v][j]; 27 if(!vis[ko]) 28 { 29 ff=0; 30 break; 31 } 32 } 33 if(ff) 34 vis[v]=1; 35 } 36 for(i = 1; i <= n ; i++) 37 if(!vis[i]) 38 break; 39 if(i==n+1) 40 { 41 ans = de; 42 return ; 43 } 44 for(i = 1; i <= n ; i++) 45 vv[i] = vis[i]; 46 for(i = 1; i <= n ; i++) 47 { 48 if(!vis[i]) 49 { 50 for(j = 1; j <= o[i] ; j++) 51 { 52 int x = kk[i][j]; 53 if(!vis[x]) 54 break; 55 } 56 if(j>o[i]) 57 dfs(co[i].d,de+1,vis); 58 } 59 for(j = 1; j <= n ; j++) 60 vis[j] = vv[j]; 61 } 62 } 63 int main() 64 { 65 int i,j,t,vis[22]; 66 scanf("%d",&t); 67 while(t--) 68 { 69 scanf("%d",&n); 70 memset(vis,0,sizeof(vis)); 71 memset(kk,0,sizeof(kk)); 72 memset(o,0,sizeof(o)); 73 memset(oo,0,sizeof(oo)); 74 memset(fk,0,sizeof(fk)); 75 ans = INF; 76 for(i = 1; i <= n ; i++) 77 { 78 scanf("%d%d%d%d%d",&co[i].ly,&co[i].lx,&co[i].ry,&co[i].rx,&co[i].d); 79 oo[co[i].d]++; 80 num[co[i].d][oo[co[i].d]] = i; 81 } 82 for(i = 1; i <= n ; i++) 83 { 84 for(j = 1 ; j <= n ; j++) 85 { 86 if(i==j) 87 continue; 88 if(((co[j].lx>=co[i].lx&&co[j].lx<co[i].rx)||(co[j].rx<=co[i].rx&&co[j].rx>co[i].lx))&&(co[j].ry<=co[i].ly)) 89 { 90 o[i]++; 91 kk[i][o[i]] = j; 92 } 93 } 94 } 95 for(i = 1; i <= n ; i++) 96 { 97 if(!o[i]&&!fk[co[i].d]) 98 { 99 fk[co[i].d] = 1; 100 dfs(co[i].d,1,vis); 101 } 102 memset(vis,0,sizeof(vis)); 103 } 104 printf("%d ",ans); 105 } 106 return 0; 107 }