UVA1252TwentyQuestions

//UVA1252TwentyQuestions
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN = 128 + 5;
const int MAXM = 11;
char obj[MAXN][1 << MAXM];
int d[1 << MAXM][1 << MAXM], vis[1 << MAXM][1 << MAXM], cnt[1 << MAXM][1 << MAXM];
int m, n;
void Initial() {
	memset(d, 0, sizeof(d));
	memset(vis, 0, sizeof(vis));
	memset(cnt, 0, sizeof(cnt));
	for(int i = 0; i < n; i++) {
	    int feature = 0;
		for(int j = 0; j < m; j++) 
		    if(obj[i][j] == '1') feature |= (1 << j);
	    for(int s = 0; s < (1 << m); s++) 
		    cnt[s][s & feature]++;
	}
}
int dp(int s, int a) {
	if(cnt[s][a] <= 1) return 0;
	if(cnt[s][a] == 2) return 1;
	int& ans = d[s][a];
	
	if(vis[s][a]) return ans;
	vis[s][a] = 1;
	ans = m;
	
	for(int k = 0; k < m; k++) {
		if(!(s & (1 << k))) {//若k操作未被访问过 
			int s1 = s | (1 << k), a1 = a | (1 << k);
			
			if(cnt[s1][a] >= 1 && cnt[s1][a1] >= 1) {
			   	int need = max(dp(s1, a1), dp(s1, a)) + 1;
			    ans = min(ans, need);
		    }
		}
	}
	return ans;
}

int main() {
	
	while(scanf("%d%d", &m, &n) == 2 && n && m) {
		
		for(int i = 0; i < n; i++) scanf("%s", obj[i]);
		Initial();
	    printf("%d\n", dp(0, 0));
	}
	return 0;
}
/*
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值