题解:

好像不用dp

暴力+位运算即可

代码:

#include<bits/stdc++.h> 
using namespace std;
typedef vector<int> ve;
map<ve,int>hash;
queue<ve>Q;
string s[35];
int n;
int main()
{
       scanf("%d",&n);
       for (int i=1;i<=n;i++) cin>>s[i];
    ve u,v,t;
    for (int i=1;i<=n;i++)
    if (s[i]=="") {puts("0");return 0;}
    else u.push_back(i<<6);
    hash[u]=0;
    Q.push(u);
    while (!Q.empty())
     {
        u=Q.front();
        Q.pop();
        int x=hash[u],cnt;
        for (int ch='0';ch<='1';ch++)
         {
            cnt=0;
            v.clear();
            for (int i=0;i<(int)u.size();i++)
             {
                int which=u[i]>>6,where=u[i]&63;
                if (s[which][where]^ch) continue;
                if (++where==s[which].size())
                 {
                    ++cnt;
                    for(int j=1;j<=n;++j) v.push_back(j<<6);
                 }
                else v.push_back(which<<6|where);
             }
            if (cnt>=3) {printf("%d\n",x+1);return 0;}
            sort(v.begin(),v.end());
            t.clear();
            for (int i=0;i<v.size();i++)
             if (i<2||v[i]^v[i-2]) t.push_back(v[i]);   
            int &th=hash[t];
            if (t.size()&&!th) th=x+1,Q.push(t);
         }
     }
    puts("-1");
}

 

posted on 2018-02-20 15:00  宣毅鸣  阅读(71)  评论(0编辑  收藏  举报