Uva--11205 (暴力搜索,DFS)
2014-07-10 01:15:20
题意&思路:问表示n个symbols需要多少个LED。直接DFS。
1 #include <cstdio> 2 #include <iostream> 3 using namespace std; 4 5 int Case,n,p,tmin,g[105][20]; 6 void Dfs(int *s,int id){ 7 if(id == p){ 8 int flag,tag = 1; 9 for(int i = 1; i <= n; ++i){ 10 for(int j = 1; j <= n; ++j){ 11 if(i == j) continue; 12 flag = 0; 13 for(int k = 1; k <= p; ++k){ 14 if(g[i][k] != g[j][k] && s[k]){ 15 flag = 1; 16 break; 17 } 18 } 19 if(!flag){ 20 tag = 0; 21 break; 22 } 23 } 24 if(!tag) break; 25 } 26 if(tag){ 27 int cnt = 0; 28 for(int i = 1; i <= p; ++i) 29 if(s[i]) ++cnt; 30 tmin = min(tmin,cnt); 31 } 32 return; 33 } 34 s[id + 1] = 0; 35 Dfs(s,id + 1); 36 s[id + 1] = 1; 37 Dfs(s,id + 1); 38 } 39 40 int main(){ 41 scanf("%d",&Case); 42 while(Case--){ 43 scanf("%d%d",&p,&n); 44 for(int i = 1; i <= n; ++i) 45 for(int j = 1; j <= p; ++j) 46 scanf("%d",&g[i][j]); 47 int mark[20] = {0}; 48 tmin = 100; 49 Dfs(mark,1); 50 mark[1] = 1; 51 Dfs(mark,1); 52 printf("%d\n",tmin); 53 } 54 return 0; 55 }