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:
- Swap two adjacent digits.
- Increase a digit by one. If the result exceed 9, change it to it modulo 10.
- 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,总共有三个操作:
1.交换两个相邻的字符
2.任意一个字符加1mod10,只有3次这种操作。
3.任意一个字符变成两倍mod10,只有两次这种操作。
题解:
我们先不考虑第一个操作,第二第三个操作数只有5个,情况非常少,那么我们可以dfs求出所有的情况,再用next_permutation(,)函数求出每次所给的串的全排列,在找最小的答案就好了。这里就有一个非常好用的网站:https://www.udebug.com/LA/7672
先点击select input 选择输入样例,那么右边就会出现样例,在点下面这个按钮,那么就会出现
这样子一个东西,之后再把你的答案复制到右边的框里,再比较一下就知道你的答案是否正确了。
#include<bits/stdc++.h>
using namespace std;
int vis[100005];
struct node
{
int num,inc,dbl;
node(){}
node(int num,int inc,int dbl):num(num),inc(inc),dbl(dbl){}
};
void dfs(int num,int inc,int dbl,int step)
{
vis[num]=min(vis[num],step);
if(!inc&&!dbl)
return ;
if(inc)
{
int pow=1;
for(int i=1;i<=5;i++)
{
int x=num;
if((x/pow)%pow==9)
x=x-(x/pow)%pow*pow;
else
x+=pow;
pow*=10;
dfs(x,inc-1,dbl,step+1);
}
}
if(dbl)
{
int pow=1;
for(int i=1;i<=5;i++)
{if(num==12350)
int flag=1;
int x=num;
int add=((num/pow)%10)*pow;
if(add*2>=10*pow)
x=x-add+(add*2)%(10*pow);
else
x+=add;
dfs(x,inc,dbl-1,step+1);
pow*=10;
}
}
}
int main()
{
int n;
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
for(int i=0;i<100005;i++)
vis[i]=100;
dfs(12345,3,2,0);
while(~scanf("%d",&n))
{
//cout<<n<<endl;
int a[6],b[6],x=n,cnt=5;
for(int i=1;i<=5;i++)
{
a[cnt--]=x%10,b[cnt+1]=a[cnt+1];
x/=10;
}
sort(b+1,b+6);
int ans=100;
do{
x=0;
for(int i=1;i<=5;i++)
x=x*10+b[i];
if(vis[x]!=100)
{
int c[6],num=0;
for(int i=1;i<=5;i++)
c[i]=b[i];
for(int i=1;i<=4;i++)
{
if(a[i]!=c[i])
{
for(int j=i+1;j<=5;j++)
{
if(c[j]==a[i])
{
for(int k=j;k>=i+1;k--)
swap(c[k],c[k-1]),num++;
break;
}
}
}
}
ans=min(ans,vis[x]+num);
}
}while(next_permutation(b+1,b+6));
if(ans!=100)
printf("%d\n",ans);
else
printf("-1\n");
}
return 0;
}