BFS预处理——UVAlive 7263

  • 题目链接:https://vjudge.net/problem/UVALive-7263

  • 题意:给出2个字符串,字符为1~6,现在给出2种操作,一种是将一种数字全部变为另外一种;一种操作是将某一个数字变成为另一个。求对第二个字符串进行最少多少次操作可以将它变为第一种

  • 分析:我们可以发现对于任意两个字符串的转换过程,第二种操作利用得当总能带来最大的价值,所以一般都是当第二种操作无法转换更多的数字时,我们再考虑第一种操作。简而言之就是,第二种操作完后再考虑第一种操作。对于第二种操作,我们可以看出是一种状态的映射,假设原始状态为6个数字123456对应123456,第二种操作结束后将变成123456对应另外6个数(范围依旧是1~6),所以我们可以用BFS预处理出初始状态到任何可能的状态(大概比 66 小一些,因为有一些状态无法抵达)所需要的花费,然后对于任何给定的字符串,我们枚举所有结束状态,然后计算在这个状态下还需要多少次第一种操作才能转换,然后取最小值即可。
    时间复杂度大致为: 66O(len(str))

  • AC代码:

/*************************************************************************
    > File Name: test.cpp
    > Author: Akira 
    > Mail: qaq.febr2.qaq@gmail.com 
 ************************************************************************/

#include<bits/stdc++.h>
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
#define MST(a,b) memset(a,b,sizeof(a))
#define CLR(a) MST(a,0)
#define Sqr(a) ((a)*(a))
using namespace std;

#define MaxN 100001
#define MaxM MaxN*10
#define INF 0x3f3f3f3f
#define PI 3.1415926535897932384626
const int mod = 1E9+7;
const double eps = 1e-6;
#define bug cout<<88888888<<endl;
#define debug(x) cout << #x" = " << x << endl;

int base[10] = {1000000, 100000, 10000, 1000, 100, 10, 1};
char tar[111], now[111];
int digit[10];
vector<int> state;
int cost[666666];

void init()
{
    MST(cost,-1);
    int pre = 123456;
    queue<int> Q;
    Q.push(pre);
    state.push_back(pre);
    cost[pre] = 0;
    while(!Q.empty())
    {
        int tmp = Q.front();
        Q.pop();
        for(int i=1;i<=6;i++)
        {
            for(int j=1;j<=6;j++)
            {
                if(i!=j)
                {
                    int next = 0;
                    for(int k=1;k<=6;k++)
                    {
                        digit[k] = tmp% base[k - 1] / base[k];
                        if( digit[k]==i) digit[k] = j;
                        next = digit[k] + next*10;
                    }
                    if(cost[next]!=-1) continue;
                    cost[next] = cost[tmp]+1;
                    Q.push(next);
                    state.push_back(next);
                }
            }
        }
    }
    //debug(state.size())
}

void solve()
{
    int len = strlen(tar);
    int ans = len;
    for(int i=0;i<state.size();i++)
    {
        int tmp = state[i];
        for(int k=1;k<=6;k++)
        {
            digit[k] = tmp% base[k - 1] / base[k];
        }
        int res = cost[tmp];
        for(int j=0;j<len;j++)
        {
            int to = tar[j]-'0';
            int cnt = now[j]-'0';
            if( digit[cnt]==to) continue;
            else res++;
        }
        //if(res<ans) debug(res);
        ans = min(ans, res);

    }
    printf("%d\n",  ans);
}

int main()
{
    init();
    while(~scanf("%s%s", tar, now))
    {
        solve();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值