2016 ICPC亚洲区域赛北京站 EWhat a Ridiculous Election(BFS预处理)
Posted kannyi
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2016 ICPC亚洲区域赛北京站 EWhat 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+5][4][3]; struct node{ int num[6]; int op2,op3; int step; }; int calsum(node a) { int sum=0,t=10000; for(int i=1;i<=5;i++) { sum+=(a.num[i]*t); t/=10; } return sum; } void bfs() { int i; queue<node>qu; memset(ans,INF,sizeof(ans)); node a; a.op2=3;//+1 a.op3=2;//*2 a.step=0; for(i=1;i<=5;i++) a.num[i]=i; qu.push(a); ans[12345][3][2]=0; while(!qu.empty()) { node t=qu.front(); qu.pop(); for(i=2;i<=5;i++)//swap { node tt=t; swap(tt.num[i],tt.num[i-1]); 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>0)//+1 { for(i=1;i<=5;i++) { node tt=t; tt.num[i]=(tt.num[i]+1)%10; 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>0)//*2 { for(i=1;i<=5;i++) { node tt=t; tt.num[i]=(tt.num[i]*2)%10; 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=0;i<=3;i++) //op2 for(j=0;j<=2;j++) //op3 minn=min(minn,ans[n][i][j]); if(minn==INF)printf("-1 "); else printf("%d ",minn); } return 0; }
以上是关于2016 ICPC亚洲区域赛北京站 EWhat a Ridiculous Election(BFS预处理)的主要内容,如果未能解决你的问题,请参考以下文章
第43届ACM-ICPC亚洲区域赛(北京)结束 课工场“全场最快解题奖”由清华大学队获得
2013 ACM-ICPC亚洲区域赛南京站C题 题解 轮廓线DP
2018ACM-ICPC亚洲区域赛南京站I题Magic Potion(网络流)