@vjudge
状压 The
state can be designed like this
dfs (s, a) dfs(s, a)dfs(s,a ) Represents the currently guessed set, and there is currently no definite number inssThe common feature in the s set isaaa , several
more guesses. Enumerate the next guess askk
The answer to k for the current state is
sum = max (dfs (s ∣ 2 k, a), dfs (s ∣ 2 k, a ∣ 2 k)) + 1 sum=max(dfs(s|2^k,a) ,dfs(s|2^k,a|2^k))+1sum=max(dfs(s∣2k,a),dfs(s∣2k,a∣2k))+1
For each kkk , take the minimum value of the answer
As for the boundary, if the state (s, a) (s, a) is satisfied(s,There is only one number in a ) , so you don’t have to guess again; if there are 2 more, then guess again; if there are more, thendfs dfsdfs
Code:
#include <bits/stdc++.h>
#define maxn 2110
using namespace std;
int cnt[maxn][maxn], dp[maxn][maxn], power[25], vis[maxn][maxn], n, m, kase;
char s[maxn];
int dfs(int s, int a){
if (cnt[s][a] == 1) return 0;
if (cnt[s][a] == 2) return 1;
if (vis[s][a] == kase) return dp[s][a];
vis[s][a] = kase;
int ans = m;
for (int i = 1; i <= m; ++i)
if (!(s & power[i - 1])){
int S = s | power[i - 1], A = a | power[i - 1];
ans = min(ans, 1 + max(dfs(S, A), dfs(S, a)));
}
return dp[s][a] = ans;
}
int main(){
power[0] = 1;
for (int i = 1; i <= 20; ++i) power[i] = power[i - 1] << 1;
while (1){
scanf("%d%d", &m, &n);
if (m == 0 && n == 0) break;
++kase;
for (int i = 0; i < power[m]; ++i)
for (int j = 0; j < power[m]; ++j) cnt[i][j] = 0;
for (int i = 1; i <= n; ++i){
scanf("%s", s + 1);
int x = 0;
for (int j = 1; j <= m; ++j)
x = (x << 1) | (s[j] == '1');
for (int j = 0; j < power[m]; ++j) ++cnt[j][j & x];
}
printf("%d\n", dfs(0, 0));
}
return 0;
}