2015Beijing区域赛(Today Is a Rainy Day-暴搜)

给一个不超过110位字符串(只有1~6),要求进行最小次变换变成另一个
变换是:把①特定1位,或②所有相同的数字改成另一个

显然②在①之前一定不劣
枚举②的情况6!种
然后暴搜
注意:本题要把字符串预处理不然会T
考场上不想写状压结果T了

#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p]) 
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define pb push_back
#define mp make_pair 
#define fi first
#define se second
typedef long long ll;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+llabs(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
    int x=0,f=1; char ch=getchar();
    while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
    while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
    return x*f;
} 
int p6[7]={1,6,36,216,1296,7776,46656};
struct V{
    int c;
    V(int i,int j,int k,int l,int m,int n){c=(i-1)*p6[0]+(j-1)*p6[1]+(k-1)*p6[2]+(l-1)*p6[3]+(m-1)*p6[4]+(n-1)*p6[5];}
    V(int c):c(c){}
    V(){}
    friend bool operator<(V a,V b) {
        return a.c<b.c;
    } 
    int get(int i) {
        return c/p6[i-1]%6+1;   
    } 
    void pri()
    {
        For(i,6) cout<<get(i)<<' '; cout<<endl;
    } 
};

int h[10000000];
queue<pair<int,int> > q;
vector<int> vec;
void bfs()
{
    memset(h,-1,sizeof(h));
    h[V(1,2,3,4,5,6).c]=0;
    q.push(mp(V(1,2,3,4,5,6).c,0));

    while (!q.empty()) {
        pair<int,int> now=q.front(),v;
        vec.pb(now.fi);
        v.se=now.se+1;
// now.fi.pri();
        For(i,6) 
            For(j,6) {
                if (i^j)
                {
                    v.fi=0;
                    For(k,6)
                        v.fi+=p6[k-1] *( ( (V(now.fi).get(k) == i) ? j :  V(now.fi).get(k)) -1 );

                    if (h[v.fi]!=-1) continue;
                    h[v.fi]=v.se;
                    q.push(v);
                }
            }       


        q.pop();
    }   
}
#define MAXN 120
char s[MAXN],s2[MAXN];
int f[7][7];
int main()
{
// freopen("hiho1251.in","r",stdin);
// freopen("hiho1251.out","w",stdout);

    bfs();
    int m=vec.size();

    while (scanf("%s%s",s2,s)!=EOF) {

        int n=strlen(s2),ans=n;

        memset(f,0,sizeof(f));
        Rep(i,n) f[s[i]-'0'][s2[i]-'0']++ ;

        for(int k=0;k<m;k++)
        {
            int j=vec[k];
            if (h[j]==-1) continue;
            V now(j);
            int res=h[j];
            For(i,6) 
            {
                For(i2,6) 
                    res+=f[i][i2]* (now.get(i)!=i2);
                if (res>=ans) break;
            }
            ans=min(ans,res);

        }       
        cout<<ans<<endl;
    }


    return 0;
}

你可能感兴趣的:(2015Beijing区域赛(Today Is a Rainy Day-暴搜))