# Twenty Questions UVALive - 4643

emmm, 第一次做按位的搜索，想了一会还是借鉴了大佬的题解～

https://blog.csdn.net/nealgavin/article/details/10648605

for(i = 0; i < m; i++)
{
if((s & b[i]) == ans)
num++;
}
if(num <= 1)
{
dp[s][ans] = 0;
return 0;

}

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cstdlib>

using namespace std;

#define INF 0x3f3f3f3f
#define LL long long

int mx = 2050;

int dp[2050][2050];

int b[130];

int n, m;

char a[15];

int DP(int s, int ans)
{
if(dp[s][ans] != -1)
return dp[s][ans];
int num = 0;
int i, j, k;
for(i = 0; i < m; i++)
{
if((s & b[i]) == ans)
num++;
}
if(num <= 1)
{
dp[s][ans] = 0;
return 0;
}

int to, ret = mx;
for(i = 0; i < n; i++)
{
if(s & (1 << i))
continue;
to = s | (1 << i);
int toans = ans | (1 << i);
dp[to][ans] = DP(to, ans);
dp[to][toans] = DP(to, toans);

ret = min(ret, max(dp[to][toans], dp[to][ans]) + 1);
}
dp[s][ans] = ret;
return ret;
}

int main()
{
int i, j, k;

while(scanf("%d%d", &n, &m))
{
if(!m && !n)
break;

for(i = 0; i < m; i++)
{
k = 0;
scanf("%s", a);
for(j = 0; j < n; j++)
{
k = k * 2 + a[j] - '0';
}
b[i] = k;
}

if(m == 1)
{
cout << 0 << endl;
continue;
}

memset(dp, -1, sizeof(dp));

cout << DP(0, 0) << endl;

}

return 0;
}

• 0
点赞
• 0
收藏
觉得还不错? 一键收藏
• 0
评论
03-28 624

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