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

题面描述

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;
		else
			cout<<answer[n]<<endl;
	}
		
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值