1. 程式人生 > >(狀壓dp)UVA - 1252 Twenty Questions

(狀壓dp)UVA - 1252 Twenty Questions

ace code return uva typedef clu http 狀壓 .net

題目地址

讀入二進制數及轉換的方法。

e.g. bitset<16> x;

  cin>>x;

  cout<<x.to_ulong()<<endl;

 1 #include <bits/stdc++.h>
 2 typedef long long ll;
 3 using namespace std;
 4 const int MAX=2e2;
 5 const int INF=1e9;
 6 int m,n;
 7 int a[MAX];
 8 int total;
 9 int cnt[1<<11][1<<11
];//cnt[x][y]問過x狀態,得到y狀態的確定答復的可能情況數 10 int dp[1<<11][1<<11];//dp[x][y]問過x狀態,得到y狀態的答復至少還需問多少次能保證確定 11 bitset<16> x; 12 int d_p(int st_ask,int st_re) 13 { 14 if(dp[st_ask][st_re]>=0&&dp[st_ask][st_re]!=INF) 15 return dp[st_ask][st_re]; 16 if(cnt[st_ask][st_re]<=1
) 17 return dp[st_ask][st_re]=0; 18 for(int i=0;i<m;i++) 19 { 20 if(!(st_ask&(1<<i))) //沒有問過 21 { 22 int now_max=max(d_p((st_ask|(1<<i)),(st_re|(1<<i))),d_p((st_ask|(1<<i)),(st_re)))+1; 23 dp[st_ask][st_re]=min(dp[st_ask][st_re],now_max);
24 } 25 } 26 return dp[st_ask][st_re]; 27 } 28 int main() 29 { 30 while(scanf("%d%d",&m,&n)&&m) 31 { 32 memset(cnt,0,sizeof(cnt)); 33 for(int i=0;i<n;i++) 34 { 35 cin>>x; 36 a[i]=x.to_ulong(); 37 } 38 total=1<<m; 39 for(int i=0;i<total;i++) 40 for(int j=0;j<total;j++) 41 dp[i][j]=INF; 42 for(int i=0;i<total;i++) 43 for(int j=0;j<n;j++) 44 ++cnt[i][i&a[j]]; 45 printf("%d\n",d_p(0,0)); 46 } 47 return 0; 48 }

(狀壓dp)UVA - 1252 Twenty Questions