【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+][][];
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预处理)的更多相关文章
- 【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 ...
- 2015 ACM / ICPC 亚洲区域赛总结(长春站&北京站)
队名:Unlimited Code Works(无尽编码) 队员:Wu.Wang.Zhou 先说一下队伍:Wu是大三学长:Wang高中noip省一:我最渣,去年来大学开始学的a+b,参加今年区域赛之 ...
- 2014ACM/ICPC亚洲区域赛牡丹江站汇总
球队内线我也总水平,这所学校得到了前所未有的8地方,因为只有两个少年队.因此,我们13并且可以被分配到的地方,因为13和非常大的数目.据领队谁oj在之上a谁去让更多的冠军.我和tyh,sxk,doub ...
- 2014ACM/ICPC亚洲区域赛牡丹江现场赛总结
不知道怎样说起-- 感觉还没那个比赛的感觉呢?如今就结束了. 9号.10号的时候学校还评比国奖.励志奖啥的,由于要来比赛,所以那些事情队友的国奖不能答辩.自己的励志奖班里乱搞要投票,自己又不在,真是无 ...
- 2014ACM/ICPC亚洲区域赛牡丹江站现场赛-K ( ZOJ 3829 ) Known Notation
Known Notation Time Limit: 2 Seconds Memory Limit: 65536 KB Do you know reverse Polish notation ...
- 【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 ...
- 【2013 ICPC亚洲区域赛成都站 F】Fibonacci Tree(最小生成树+思维)
Problem Description Coach Pang is interested in Fibonacci numbers while Uncle Yang wants him to do s ...
- 【2018 ICPC亚洲区域赛南京站 A】Adrien and Austin(博弈)
题意: 有一排n个石子(注意n可以为0),每次可以取1~K个连续的石子,Adrien先手,Austin后手,若谁不能取则谁输. 思路: (1) n为0时的情况进行特判,后手必胜. (2) 当k=1时, ...
- 【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 ...
随机推荐
- PID控制算法
PID控制算法 四轴如何起飞的原理 四轴飞行器的螺旋桨与空气发生相对运动,产生了向上的升力,当升力大于四轴的重力时四轴就可以起飞了. 四轴飞行器飞行过程中如何保持水平: 我们先假设一种理想状况:四个电 ...
- Grunt入门学习之(3) -- Gruntfile具体示例
经过前面的学习,将测试的Gruntfile整合在一起! /** * Created by Administrator on 2017/6/22. */ module.exports = functio ...
- (Stanford CS224d) Deep Learning and NLP课程笔记(一):Deep NLP
Stanford大学在2015年开设了一门Deep Learning for Natural Language Processing的课程,广受好评.并在2016年春季再次开课.我将开始这门课程的学习 ...
- [翻译] VLDContextSheet
VLDContextSheet 效果: A clone of the Pinterest iOS app context menu. 复制了Pinterest应用的菜单效果. Example Usag ...
- 倒计时特效的CountAnimationLabel
倒计时特效的CountAnimationLabel 效果: 源码: CountAnimationLabel.h 与 CountAnimationLabel.m // // CountAnimation ...
- 【MyBatis】 MyBatis入门
1.MyBatis简介 MyBatis是这个框架现在的名字,而此框架最早的名字是IBatis,其名字的含义是“internet”.“abatis”两个单词的组合,是在2002年的时候开始的一个开源项目 ...
- 沉淀再出发:redis的安装和使用
沉淀再出发:redis的安装和使用 一.前言 下面让我们看看Redis这个键值对内存数据库的使用方法和相关注意事项. 二.Redis的安装和使用 2.1.Redis的定义 REmote DIction ...
- matlab 黑白格子
有一个生成黑白格子的函数 40 这个参数是改变大小的 img=checkerboard(40)<0.5; figure; imshow(img,[])
- 关于 Can't connect to MySQL server on 'localhost' (10061) 的一个解决方案
问题描述: 使用Navicat for mysql 无法远程连接到本地数据库,提示Can't connect to MySQL server on 'localhost' (10038) . 本地服务 ...
- 怎么知道是哪个div被点击了
怎么知道是哪个div被点击了 不在div中加onclick等事件调用函数 ,用事件监听函数,但是如果div中的div被点击了,addEventListener得到了两个监听事件,我想点击div里的di ...