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+][][];
struct node{
int num[];
int op2,op3;
int step;
};
int calsum(node a)
{
int sum=,t=;
for(int i=;i<=;i++)
{
sum+=(a.num[i]*t);
t/=;
}
return sum;
}
void bfs()
{
int i;
queue<node>qu;
memset(ans,INF,sizeof(ans));
node a;
a.op2=;//+1
a.op3=;//*2
a.step=;
for(i=;i<=;i++)
a.num[i]=i;
qu.push(a);
ans[][][]=;
while(!qu.empty())
{
node t=qu.front();
qu.pop();
for(i=;i<=;i++)//swap
{
node tt=t;
swap(tt.num[i],tt.num[i-]);
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>)//+1
{
for(i=;i<=;i++)
{
node tt=t;
tt.num[i]=(tt.num[i]+)%;
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>)//*2
{
for(i=;i<=;i++)
{
node tt=t;
tt.num[i]=(tt.num[i]*)%;
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=;i<=;i++) //op2
for(j=;j<=;j++) //op3
minn=min(minn,ans[n][i][j]);
if(minn==INF)printf("-1\n");
else printf("%d\n",minn);
}
return ;
}

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

  1. 【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 w ...

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

    队名:Unlimited Code Works(无尽编码)  队员:Wu.Wang.Zhou 先说一下队伍:Wu是大三学长:Wang高中noip省一:我最渣,去年来大学开始学的a+b,参加今年区域赛之 ...

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

    球队内线我也总水平,这所学校得到了前所未有的8地方,因为只有两个少年队.因此,我们13并且可以被分配到的地方,因为13和非常大的数目.据领队谁oj在之上a谁去让更多的冠军.我和tyh,sxk,doub ...

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

    不知道怎样说起-- 感觉还没那个比赛的感觉呢?如今就结束了. 9号.10号的时候学校还评比国奖.励志奖啥的,由于要来比赛,所以那些事情队友的国奖不能答辩.自己的励志奖班里乱搞要投票,自己又不在,真是无 ...

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

    Known Notation Time Limit: 2 Seconds      Memory Limit: 65536 KB Do you know reverse Polish notation ...

  6. 【2015 ICPC亚洲区域赛长春站 G】Dancing Stars on Me(几何+暴力)

    Problem Description The sky was brushed clean by the wind and the stars were cold in a black sky. Wh ...

  7. 【2013 ICPC亚洲区域赛成都站 F】Fibonacci Tree(最小生成树+思维)

    Problem Description Coach Pang is interested in Fibonacci numbers while Uncle Yang wants him to do s ...

  8. 【2018 ICPC亚洲区域赛南京站 A】Adrien and Austin(博弈)

    题意: 有一排n个石子(注意n可以为0),每次可以取1~K个连续的石子,Adrien先手,Austin后手,若谁不能取则谁输. 思路: (1) n为0时的情况进行特判,后手必胜. (2) 当k=1时, ...

  9. 【2018 ICPC亚洲区域赛徐州站 A】Rikka with Minimum Spanning Trees(求最小生成树个数与总权值的乘积)

    Hello everyone! I am your old friend Rikka. Welcome to Xuzhou. This is the first problem, which is a ...

随机推荐

  1. C++学习笔记(2)----类模板和友元

    当一个类包含一个友元声明时,类与友元各自是否是模板是相互无关的.如果一个类模板包含一个非模板友元,则友元被授权可以访问所有模板实例.如果友元自身是模板,类可以授权给所有友元模板实例,也可以只授权给特定 ...

  2. Forword与sendRedirect的区别

    二.本质区别 解释一 一句话,转发是服务器行为,重定向是客户端行为.为什么这样说呢,这就要看两个动作的工作流程: 转发过程:客户浏览器发送http请求——>web服务器接受此请求——>调用 ...

  3. elasticsearch集群搭建报错: not enough master nodes discovered during pinging

    自己用一台 阿里云 服务器 搭建ES集群的时候,总是报上面的问题. 而且两个ES服务都是报同样的问题.自己的配置文件如下: es服务1配置文件 cluster.name: elasticsearch ...

  4. Hypver-V中的快照

    应用快照选项的区别: 获取并应用快照:对虚拟机当前的状态进行快照,然后恢复到所选的快照状态 应用:不保存当前虚拟机的状态,直接将其恢复到所选的快照状态

  5. python 实现插入排序、冒泡排序、归并排序

    def InsertSort(A): '''插入排序算法:传入一个list,对list中的数字进行排序''' print('插入排序前list元素顺序:',A) length=len(A) for i ...

  6. August 06th 2017 Week 32nd Sunday

    No words are necessary between two loving hearts. 两颗相爱的心之间不需要言语. No, I don't think so. Words may be ...

  7. December 19th 2016 Week 52nd Sunday

    Truth and roses have thorns about them. 真理和玫瑰,身边都有刺. Either truth or roses, they all have thorns aro ...

  8. linux下常用命令:

    常用指令 ls        显示文件或目录 -l           列出文件详细信息l(list) -a          列出当前目录下所有文件及目录,包括隐藏的a(all) mkdir     ...

  9. 使用JAVA进行排序

    利用JAVA完成排序 当我们在进行数据库进行查询的时候,当需要按某个字段来进行排序的时候,可以使用SQL语句来完成排序,可以升序,也可以降序.JAVA中的Collections类也可以完成这种操作,S ...

  10. JavaScript事件的委派与事件的绑定

    事件的委派 在很多需求中,通常元素是动态创建添加到一个父元素中的,这时候我们点击新增的元素是没有反应的 <script type="text/javascript"> ...