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. Android BitmapDrawable

    功能:显示缩略图,大小为40*40 //通过openRawResource获取一个inputStream对象 InputStream inputStream = getResources().open ...

  2. day04之VUE痛悟

    vue组件组件分为三部分

  3. 数据预处理(Python scikit-learn)

    在机器学习任务中,经常会对数据进行预处理.如尺度变换,标准化,二值化,正规化.至于采用哪种方法更有效,则与数据分布和采用算法有关.不同算法对数据的假设不同,可能需要不同的变换,而且有时无需进行变换,也 ...

  4. linux 下MySQL的安装

    一.安装MySQL   1.下载源码包     从mysql官网上下载linux下的source包mysql-5.0.51b.tar.gz,注意是下载GNU tar格式的,不是rpm包.    2.解 ...

  5. Python初学者第十四天 三元运算及文件处理2

    14day 1.三元运算: 又称三目运算,是对简单的条件语句的简写 如简单条件语句: if a > b: n = a else: n = b print(n) 三目运算语句: n = a if ...

  6. Chapter 2 Secondary Sorting:Detailed Example

    2.1 Introduction MapReduce framework sorts input to reducers by key, but values of reducers are arbi ...

  7. mysql 插入汉字异常: Incorrect string value: '\xE8\xB0\xA2\xE9\x9D\x99' for column 'uname' at row 1

    该字段编码问题,不支持中文,设置支持中文即可

  8. Alpha Scrum5

    Alpha Scrum5 牛肉面不要牛肉不要面 Alpha项目冲刺(团队作业5) 各个成员在 Alpha 阶段认领的任务 林志松:督促和监督团队进度,前端页面编写 林书浩.陈远军:界面设计.美化 吴沂 ...

  9. Python安装Windows的pip包

    1.到https://www.python.org/downloads/ 下载python包安装python 2.到https://pypi.python.org/pypi/pip#downloads ...

  10. python新建以时间命名的目录

    1.新建三级目录,第一级是去年的年份,第二级是去年的月,第三级为去年的日,在日的文件中写入今年的时分秒 import os import time import shutil def create_f ...