2015北京C(搜索技巧预处理)

链接:https://cn.vjudge.net/contest/250782#problem/C

题意:给定2个字符串和2种操作,操作1是把一个数字变成另一个数字,操作2是把一种数字全部变成另一种数字,求把一个字符串比变成另一个字符串的最少操作次数(字符串均在1-6之间)

这个技巧真的有些特殊。。显然需要用操作2来减少操作次数,而且应该是先用完操作2后在用操作1。。

然后关键在看操作2。。直接搜肯定是不行的,对当前的字符串有30种变换,而且状态也不好存。。

然后需要预处理使得操作2这个变换更加简单,可以注意到,其实数字只有6个而且是整体变换,那么可以预处理每位数字变换到所需数字的操作2的次数。。具体来说就是将123456去做bfs,然后就能处理出操作2能够到达的所有情况。。

 

/**
 *        ┏┓    ┏┓
 *        ┏┛┗━━━━━━━┛┗━━━┓
 *        ┃       ┃  
 *        ┃   ━    ┃
 *        ┃ >   < ┃
 *        ┃       ┃
 *        ┃... ⌒ ...  ┃
 *        ┃       ┃
 *        ┗━┓   ┏━┛
 *          ┃   ┃ Code is far away from bug with the animal protecting          
 *          ┃   ┃   神兽保佑,代码无bug
 *          ┃   ┃           
 *          ┃   ┃        
 *          ┃   ┃
 *          ┃   ┃           
 *          ┃   ┗━━━┓
 *          ┃       ┣┓
 *          ┃       ┏┛
 *          ┗┓┓┏━┳┓┏┛
 *           ┃┫┫ ┃┫┫
 *           ┗┻┛ ┗┻┛
 */
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<bitset>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-8
#define succ(x) (1LL<<(x))
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define NM 700005
#define nm 2000005
#define pi 3.1415926535897931
using namespace std;
const ll inf=1e9+7;
ll read(){
    ll 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 f*x;
}





int p[10],d[NM],b[NM],a[NM],ans,c[NM],tot,n,f[10];
char _s[NM];
queue<int>q;

void bfs(){
    p[0]=1;inc(i,1,5)p[i]=p[i-1]*10;
    q.push(123456);d[123456]=1;
    while(!q.empty()){
	int t=q.front();q.pop();c[++tot]=t;
	inc(i,1,6)inc(j,1,6)if(i!=j){
	    int cnt=t;
	    inc(k,0,5)if(cnt/p[k]%10==i)cnt+=(j-i)*p[k];
	    if(!d[cnt])d[cnt]=d[t]+1,q.push(cnt);
	}
    }
}

int main(){
    //freopen("data.in","r",stdin);
    bfs();
    while(~scanf("%s",_s+1)){
	n=strlen(_s+1);ans=inf;
	inc(i,1,n)b[i]=_s[i]-'0';
	inc(i,1,n)scanf("%1d",a+i);
	inc(k,1,tot){
	    int s=d[c[k]]-1;
	    for(int x=c[k],i=1;x;i++,x/=10)f[i]=x%10;
	    reverse(f+1,f+7);
	    inc(i,1,n)if(f[a[i]]!=b[i])s++;
	    ans=min(ans,s);
	}
	printf("%d\n",ans);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值