当前位置: 代码迷 >> 综合 >> 2018北大暑校acm算法训练课程 What a Ridiculous Election bfs预处理
  详细解决方案

2018北大暑校acm算法训练课程 What a Ridiculous Election bfs预处理

热度:89   发布时间:2023-12-28 00:39:16.0

题面描述

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

给定一个数字,你需要将它从12345变到这个数字
如果能变到,输出次数,不能变出则输出-1
方法:
1交换两个相邻数字。
2将某一位加1.如果大于10则取余
3将某一位乘以2,如果大于10则取余

其中 第一种操作可以使用无数次,第二种操作只能使用3次,第三种操作只能使用2次

本题由于是多组输入,所以使用bfs进行预处理即可

构建一个结构体,存到该位置已经使用的(乘2和加1的次数)

一旦次数达到上限,则不再使用
否则继续压入队列

最后使用O(1)的复杂度判断即可

并不复杂,注意不要混用int和string(若出现四位数,混用会造成前导0消失,从而影响结果 01234 和 1234)

代码如下:

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int MAX_N=100000;
const int INF = 0x3f3f3f3f;
#define eps 1e-7
//#define Kongxiangzhouye
//仅供参考
//int cal;
struct p{string num;int addtimes;int doubletimes;int self;int step;friend bool operator <(p t1,p t2){return t1.step>t2.step;}
};int ans[100000][4][3];
int change(p s){return atoi(s.num.c_str());
}void bfs(p tem){
//	cout<<"in bfs now"<<endl;priority_queue<p> q;q.push(tem);while(!q.empty()){p now=q.top();q.pop();if(now.addtimes){for(int i=0;i<5;i++){p nex=now;nex.num[i]=(nex.num[i]-'0'+1)%10+'0';nex.self=change(nex);nex.addtimes--;if(ans[nex.self][nex.addtimes][nex.doubletimes]==INF){nex.step++;ans[nex.self][nex.addtimes][nex.doubletimes]=nex.step;q.push(nex);}}}if(now.doubletimes){for(int i=0;i<5;i++){p nex=now;nex.num[i]=((nex.num[i]-'0')*2)%10+'0';//		cout<<nex.num[i]<<endl;nex.self=change(nex);//	cout<<nex.self<<endl;nex.doubletimes--;if(ans[nex.self][nex.addtimes][nex.doubletimes]==INF){nex.step++;ans[nex.self][nex.addtimes][nex.doubletimes]=nex.step;q.push(nex);}}}for(int i=1;i<5;i++){p nex=now;swap(nex.num[i],nex.num[i-1]);nex.self=change(nex);if(ans[nex.self][nex.addtimes][nex.doubletimes]==INF){nex.step++;ans[nex.self][nex.addtimes][nex.doubletimes]=nex.step;q.push(nex);}}}
}
int answer[100000];
int main(){memset(ans,INF,sizeof(ans));memset(answer,INF,sizeof(answer));p st;st.self=12345;st.addtimes=3;st.doubletimes=2;st.num="12345";st.step=0;ans[st.self][st.addtimes][st.doubletimes]=st.step;
///	cal=0;bfs(st);
//	cout<<cal<<endl;for(int k=0;k<100000;k++){for(int i=0;i<=3;i++){for(int j=0;j<=2;j++){answer[k]=min(answer[k],ans[k][i][j]);}}}int n;while(scanf("%d",&n)!=EOF){if(answer[n]==INF)cout<<"-1"<<endl;elsecout<<answer[n]<<endl;}return 0;
}
  相关解决方案