UVA 1252-twenty Questions (state compression dp+ subset enumeration)

Source: Internet
Author: User

Main topic: There are n items, each item has m characteristics, each item may have or not each feature, now assume that an item, by asking certain characteristics to determine the item, ask the maximum number of times you can determine the item.


Different strategies for further questioning may be taken after each inquiry, depending on the answer.

With D[S][S0], the answer is S0 (that is, the item has S0 in s), at least the number of times it needs to be asked. Enumerates the feature completion recursion for the next query. The final d[0][0] is the answer. S0 is obviously a subset of S. The next question is not characteristic of S. If there is only one item for a D[S][S0], then the value is 0 and no more queries are required.


State transition equation:

d[s][s0]=min {max (d[s|u][s0],d[s|u][s0|u])} (u== (1<<k), s&u==0)


#include <stdio.h> #include <stdlib.h>int a[150];int d[2100][2100];char e[1010];int Main (void) {int i,j,u,p , q,n,m,sump,minp,ok,s,s0;scanf ("%d%d", &m,&n); while ((m!=0) | | (n!=0)) {for (i=1;i<=n;i++) {scanf ("%s", e+1), Sump=0;for (j=1;j<=m;j++) {sump=2*sump+e[j]-' 0 ';} A[i]=sump;} p= (1<<m) -1;for (s=p; s>=0; s--) {S0=s;while (1) {sump=0;ok=1;for (i=1;i<=n;i++) {if ((a[i]&s) ==s0) {sump++;if (sump>1) {OK=0;break;}}} if (ok==1) {d[s][s0]=0;} Else{minp= (1<<10); for (i=1;i<=m;i++) {u=1<< (i-1); if ((U&s) ==0) {q=d[s|u][s0]>d[s|u][s0|u]?d[ S|U][S0]:d [S|u][s0|u];minp=q<minp?q:minp;}} d[s][s0]=minp+1;} if (s0==0) {break;} s0= (s0-1) &s;}} printf ("%d\n", D[0][0]); scanf ("%d%d", &m,&n);} return 0;}


UVA 1252-twenty Questions (state compression dp+ subset enumeration)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.