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++多线程编程(教程+Demo)

    下载地址:C++多线程编程(教程+Demo) Win32 SDK函数支持进行多线程的程序设计,并提供了操作系统原理中的各种同步.互斥和临界区等操作.Visual C++ 6.0中,使用MFC类库也实现 ...

  2. 【转】使用expect实现shell自动交互

    原文地址:http://www.nginx.cn/1934.html shell脚本需要交互的地方可以使用here文档是实现,但是有些命令却需要用户手动去就交互如passwd.scp 对自动部署免去用 ...

  3. 毕向东_Java基础视频教程第19天_IO流(15~17)

    第19天-15-IO流(读取键盘录入) InputStreamReader是字节流通向字符流的桥梁,它使用指定的charset读取字节并将其解码为字符.它使用的字符集可以由名称指定或显式给定,或者可以 ...

  4. 在C#中internal、protected internal关键字是什么意思?

    internal:就是程序集. 那么什么是程序集呢?就是“项目”也就是工程里中的csproj 比如:我有个解决方案,这个方案中有2个项目,1个是控制台程序,1个是webapi项目,那么我这个解决方案中 ...

  5. Project Euler 44: Find the smallest pair of pentagonal numbers whose sum and difference is pentagonal.

    In Problem 42 we dealt with triangular problems, in Problem 44 of Project Euler we deal with pentago ...

  6. 细嚼慢咽C++primer(4)——类(1):构造函数,类型别名

    1  最简单地说,类即使定义了一个新的类型和一个新的作用域. 2  基础 1  构造函数 构造函数一般应使用一个构造函数初始化列表,来初始化对象的数据成员. Sales_item(): units_s ...

  7. December 10th 2016 Week 50th Saturday

    Storms make trees take deeper roots. 风暴使树木深深扎根. Sometimes, you may feel frustrated for failing to wi ...

  8. Python 执行命令行操作。

    os.system os.popen() commands.getstatusoutput(cmd) (status, output) = commands.getstatusoutput('cat ...

  9. 【转】多线程Core Data

    原文地址:http://www.cocoanetics.com/2012/07/multi-context-coredata/ Multi-Context CoreData When you star ...

  10. 【RabbitMQ】1、安装

    1.  下载 下载地址:http://www.rabbitmq.com/download.html 2.  windows下安装 2.1. 安装Erlang 下载:http://www.erlang. ...