【2016 ICPC亚洲区域赛北京站 E】What a Ridiculous Election(BFS预处理)

在一个虚构国家的总统选举中,两名候选人势均力敌,最终由一名六岁儿童决定胜负。通过解决一个数学谜题来挑选总统,涉及到字符串转换算法,使用交换、加一和加倍操作将“12345”变为任意五位数串,挑战在于找到最少的操作步骤。

Description

In country Light Tower, a presidential election is going on. There are two candidates,  Mr. X1 and Mr. X2, and both of them are not like good persons. One is called a liar and the other is called a maniac. They tear(Chinese English word, means defame) each other on TV face to face, on newspaper, on internet.......on all kinds of media. The country is tore into two parts because the people who support X1 are almost as many as the people who support X2.

After the election day, X1 and X2 get almost the same number of votes. No one gets enough votes to win. According to the law of the country, the Great Judge must decide who will be the president. But the judge doesn't want to offend half population of the country, so he randomly chooses a 6 years old kid Tom and authorize him to pick the president. Sounds weird? But the democracy in Light Tower is just like that.

The poor or lucky little kid Tom doesn't understand what is happening to his country. But he has his way to do his job. Tom's ao shu(Chinese English word, means some kind of weird math for kids) teacher just left him a puzzle a few days ago, Tom decide that he who solve that puzzle in a better way will be president. The ao shu teacher's puzzle is like this:

Given a string which consists of five digits('0'-'9'), like "02943", you should change "12345" into it by as few as possible operations. There are 3 kinds of operations:

1. Swap two adjacent digits.

2. Increase a digit by one. If the result exceed 9, change it to it modulo 10.

3. Double a digit. If the result exceed 9, change it to it modulo 10.

You can use operation 2 at most three times, and use operation 3 at most twice.

As a melon eater(Chinese English again, means bystander), which candidate do you support? Please help him solve the puzzle.

Input

There are no more than 100,000 test cases.

Each test case is a string which consists of 5 digits.

Output

For each case, print the minimum number of operations must be used to change "12345" into the given string. If there is no solution, print -1.

Sample Input

12435

99999

12374

Sample Output

1

-1

3

题意:

给你一个长度为5的数字串,问最少通过几次变化可以使该串变为12345;

有下列三种变化方法:

1:交换相邻两项,次数无限制;

2:串中的某一位+1,次数最多3次,若≥10则需%10;

3:串中的某一位 *2,次数最多2次,若≥10则需%10;

题解:

用一个三维数组ans[num][op2][op3]进行预处理,代表12345变化到num时,操作2和操作3的剩余次数,以及最少操作次数,随后进行BFS预处理即可。

#include<bits/stdc++.h>
#define MAX 100000
#define INF 0x3f3f3f3f
using namespace std;
int ans[MAX+5][4][3];
struct node{
    int num[6];
    int op2,op3;
    int step;
};
int calsum(node a)
{
    int sum=0,t=10000;
    for(int i=1;i<=5;i++)
    {
        sum+=(a.num[i]*t);
        t/=10;
    } 
    return sum;
}
void bfs()
{
    int i;
    queue<node>qu;
    memset(ans,INF,sizeof(ans));
    node a;
    a.op2=3;//+1
    a.op3=2;//*2
    a.step=0;
    for(i=1;i<=5;i++)
        a.num[i]=i;
    qu.push(a);
    ans[12345][3][2]=0;
    while(!qu.empty())
    {
        node t=qu.front();
        qu.pop();
        for(i=2;i<=5;i++)//swap
        {
            node tt=t;
            swap(tt.num[i],tt.num[i-1]);
            int num=calsum(tt);
            tt.step++;
            if(tt.step<ans[num][tt.op2][tt.op3])
            {
                qu.push(tt);
                ans[num][tt.op2][tt.op3]=tt.step;
            }
        }
        if(t.op2>0)//+1
        {
            for(i=1;i<=5;i++)
            {
                node tt=t;
                tt.num[i]=(tt.num[i]+1)%10;
                int num=calsum(tt);
                tt.op2--;
                tt.step++;
                if(tt.step<ans[num][tt.op2][tt.op3])
                {
                    qu.push(tt);
                    ans[num][tt.op2][tt.op3]=tt.step;
                }
            }
        }
        if(t.op3>0)//*2
        {
            for(i=1;i<=5;i++)
            {
                node tt=t;
                tt.num[i]=(tt.num[i]*2)%10;
                int num=calsum(tt);
                tt.op3--;
                tt.step++;
                if(tt.step<ans[num][tt.op2][tt.op3])
                {
                    qu.push(tt);
                    ans[num][tt.op2][tt.op3]=tt.step;
                }
            }
        }
    }
}
int main()
{
    bfs();
    int n,i,j;
    while(scanf("%d",&n)!=EOF)
    {
        int minn=INF;
        for(i=0;i<=3;i++)     //op2
            for(j=0;j<=2;j++) //op3
                minn=min(minn,ans[n][i][j]);
        if(minn==INF)printf("-1\n");
        else printf("%d\n",minn);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/kannyi/p/9899922.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值