What a Ridiculous Election UVALive - 7672(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.
    思路:一开始就是暴力bfs,猛tle。因为都是由“12345”这个序列转化的,所以我们可以预处理出来所有的可能情况,三维标记。然后就离线查询,因为第二种操作,第三种操作都是有次数限制的,所以就直接遍历12种可能。在bfs的时候用优先队列,这样的话,我们就能保证每个数出现的时候次数是最小的。
    代码如下:
#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;

const int maxx=1e5+100;
int vis[maxx][4][4];
struct node{
	string s;
	int cz2;
	int cz3;
	int cnt;
	node(string a,int b,int c,int d)
	{
		s=a,cz2=b,cz3=c,cnt=d;
	}
	bool operator<(const node &a)const{
		return cnt>a.cnt;
	}
};
string s;

inline int fcs(string a)
{
	int ans=0;
	for(int i=0;i<a.length();i++)
	{
		ans=ans*10+(a[i]-'0');
	}
	return ans;
}
inline void bfs()
{
	map<int,int> mp; 
	int ans=inf;
	priority_queue<node> p;
	string a="12345",t,f;
	int x=fcs(a);
	vis[x][3][2]=0;
	p.push(node(a,3,2,0));
	while(p.size())
	{
		node a=p.top();
		p.pop();
		t=a.s;
		for(int i=0;i<4;i++)
		{
				f=t;
				swap(f[i],f[i+1]);
				int oo=fcs(f);
				if(vis[oo][a.cz2][a.cz3]==inf)
				{
					vis[oo][a.cz2][a.cz3]=a.cnt+1;
					p.push(node(f,a.cz2,a.cz3,a.cnt+1));
				}
		}
		if(a.cz2>0)
		{
			for(int i=0;i<5;i++)
			{
					f=t;
					x=f[i]-'0';
					x=(x+1)%10;
					f[i]=x+'0';
					int oo=fcs(f);
					if(vis[oo][a.cz2-1][a.cz3]==inf)
					{
						vis[oo][a.cz2-1][a.cz3]=a.cnt+1;
						p.push(node(f,a.cz2-1,a.cz3,a.cnt+1));
					}
			}
		}
		if(a.cz3>0)
		{
			for(int i=0;i<5;i++)
			{
					f=t;
					x=f[i]-'0';
					x=x*2%10;
					f[i]=x+'0';
					int oo=fcs(f);
					if(vis[oo][a.cz2][a.cz3-1]==inf)
					{
						vis[oo][a.cz2][a.cz3-1]=a.cnt+1;
						p.push(node(f,a.cz2,a.cz3-1,a.cnt+1));
					}
			}
		}
	}
}
int main()
{
	memset(vis,inf,sizeof(vis));
	bfs();
	while(cin>>s)
	{
		int ans=inf;
		for(int i=0;i<=3;i++)
		{
			for(int j=0;j<=2;j++)
			{
				ans=min(ans,vis[fcs(s)][i][j]);
			}
		}
		if(ans==inf) puts("-1");
		else printf("%d\n",ans);
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值