【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 ...
随机推荐
- Microsoft Visual Studio 2010下log4cplus的安装,集成,测试
原文:http://blog.csdn.net/eclipser1987/article/details/6904301 log4cplus是C++编写的开源的日志系统,功能非常全面,用到自己开发的工 ...
- C语言中关键词static的用法与作用域
一.面向过程设计中的static 转载:http://blog.csdn.net/celerylxq/article/details/6160499 1.静态全局变量 在全局变量前,加上关键字stat ...
- vs2015新建web应用程序空模板和添加webapi的模板生成文件的比较
文件名为全红色的,是使用webapi模板生成的新文件夹或文件
- Linux Swap扩容
最近在做rac,在环境检查的时候发现swap空间检查不通过,所以我们第一想到的是对swap进行扩容,那么swap扩容有哪些方法呢?这里主要介绍两种方法,一通过添加额外磁盘,扩展swap分区,二是通过本 ...
- iOS设计模式 - 装饰
iOS设计模式 - 装饰 原理图 说明 1. cocoa框架本身实现了装饰模式(category的方式实现了装饰模式) 2. 装饰模式指的是动态的给一个对象添加一些额外的职责,相对于继承子类来说,装饰 ...
- Linux入门-4 Linux下获取帮助
help MAN INFO doc help <command> -h或<command> --help whatis <cmd> MAN man <comm ...
- 搭建spring boot+elasticsearch+activemq服务
目前时间是:2017-01-24 本文不涉及activemq的安装 需求 activemq实时传递数据至服务 elasticsearch做索引 对外开放查询接口 完成全文检索 环境 jdk:1.8 s ...
- 沉淀,再出发:Java基础知识汇总
沉淀,再出发:Java基础知识汇总 一.前言 不管走得多远,基础知识是最重要的,这些知识就是建造一座座高楼大厦的基石和钢筋水泥.对于Java这门包含了编程方方面面的语言,有着太多的基础知识了,从最初的 ...
- HTTP协议图--HTTP 工作过程
HTTP请求响应模型 HTTP通信机制是在一次完整的 HTTP 通信过程中,客户端与服务器之间将完成下列7个步骤: 建立 TCP 连接 在HTTP工作开始之前,客户端首先要 ...
- tcp通讯中socket套接字accept和listen的关系
今天看到一个文章,客户端的connect在服务端调用accept之前,突然想到这可以建立正常的连接么?以前从没细细的思考过listen accept connect之前的关系,带着疑问学习了一下,记录 ...