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

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

时间: 2024-11-08 23:36:46

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

【2017 ICPC亚洲区域赛北京站 J】Pangu and Stones(区间dp)

In Chinese mythology, Pangu is the first living being and the creator of the sky and the earth. He woke up from an egg and split the egg into two parts: the sky and the earth. At the beginning, there was no mountain on the earth, only stones all over

2015 ACM / ICPC 亚洲区域赛总结(长春站&amp;&amp;北京站)

队名:Unlimited Code Works(无尽编码)  队员:Wu.Wang.Zhou 先说一下队伍阵容:Wu是大三学长:Wang高中noip省一:我最渣,去年来大学开始学的a+b,参加今年区域赛之前只学了大部分图论内容,以及一些数据结构.动态规划等等,但是还不能熟练运用... ... 先从长春站说起吧... ... 长春站是我加入ACM以来参加的第一场ICPC,因此无比的激动!从杭州出发,乘了整整24小时的火车,终于到达了长春:第一次到大东北,沿途的风景与南方有很大差异,体会到了东北的寒

2014ACM/ICPC亚洲区域赛牡丹江站总结

我在集训队里面也就一般水平,这次学校史无前例的拿到了8个名额,由于大三的只有两个队伍,所以我们13及能分到名额,由于13及人数很多,组长就按照谁在oj上面a的题多就让谁去,我和tyh,sxk,doubleq幸运的在大二就有机会参加亚洲现场赛,非常激动.牡丹江赛区是我和sxk和doubleq组成rainbow战队,我们对这次区域赛其实就是去张张见识,增加大赛经验(外加公费旅游2333),可是当我们真正来到赛场的时候不知道为上面我非常渴望拿一块牌子.第一天热身赛,double迅速切下水题,我一直再弄

2014ACM/ICPC亚洲区域赛牡丹江站汇总

球队内线我也总水平,这所学校得到了前所未有的8地方,因为只有两个少年队.因此,我们13并且可以被分配到的地方,因为13和非常大的数目.据领队谁oj在之上a谁去让更多的冠军.我和tyh,sxk,doubleq运的在大二就有机会參加亚洲现场赛,非常激动.牡丹江赛区是我和sxk和doubleq组成rainbow战队,我们对这次区域赛事实上就是去张张见识,添加大赛经验(外加公费旅游2333),但是当我们真正来到赛场的时候不知道为上面我非常渴望拿一块牌子. 第一天热身赛,double迅速切下水题.我一直再

2014ACM/ICPC亚洲区域赛牡丹江现场赛总结

不知道如何说起-- 感觉还没那个比赛的感觉呢?现在就结束了. 9号.10号的时候学校还评比国奖.励志奖啥的,因为要来比赛,所以那些事情队友的国奖不能答辩,自己的励志奖班里乱搞要投票,自己又不在,真是无语了--烦得要死,然后在这些事情还没处理好之前我们就这样10号中午从地大去北京站上火车了--那时真感觉这场带着这种心情来现场赛感觉要打铁了-- 然后10号晚上队友的国奖让琦神帮答辩完了,得国奖无疑了,然后自己的励志奖也定下来一定得了,在火车上的我们也松了一口气,不能因为来比赛国奖励志奖都不得是不--

2014ACM/ICPC亚洲区域赛牡丹江站现场赛-A ( ZOJ 3819 ) Average Score

Average Score Time Limit: 2 Seconds      Memory Limit: 65536 KB Bob is a freshman in Marjar University. He is clever and diligent. However, he is not good at math, especially in Mathematical Analysis. After a mid-term exam, Bob was anxious about his

2014ACM/ICPC亚洲区域赛牡丹江站现场赛-I ( ZOJ 3827 ) Information Entropy

Information Entropy Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge Information Theory is one of the most popular courses in Marjar University. In this course, there is an important chapter about information entropy. Entropy is t

2014ACM/ICPC亚洲区域赛牡丹江站D和K题

Known Notation Time Limit: 2 Seconds      Memory Limit: 131072 KB Do you know reverse Polish notation (RPN)? It is a known notation in the area of mathematics and computer science. It is also known as postfix notation since every operator in an expre

2014ACM/ICPC亚洲区域赛牡丹江站现场赛-K ( ZOJ 3829 ) Known Notation

Known Notation Time Limit: 2 Seconds      Memory Limit: 65536 KB Do you know reverse Polish notation (RPN)? It is a known notation in the area of mathematics and computer science. It is also known as postfix notation since every operator in an expres