In country Light Tower, a presidential election is going on. There are two candidates, Mr. X1 andMr. X2, and both of them are not like good persons. One is called a liar and the other is called amaniac. 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 supportX1 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 towin. According to the law of the country, the Great Judge must decide who will be the president. Butthe judge doesn’t want to o end half population of the country, so he randomly chooses a 6 years oldkid Tom and authorize him to pick the president. Sounds weird? But the democracy in Light Tower isjust like that.
The poor or lucky little kid Tom doesn’t understand what is happening to his country. But he hashis 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 waywill be president. The ao shu teacher’s puzzle is like this:
Given a string which consists of ve digits(‘0’..‘9’), like “02943”, you should change “12345” into itby 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 givenstring. If there is no solution, print ‘-1’.
Sample Input
12435
99999
12374
Sample Output
1
-1
3
题意:
操作1,交换相邻的数字,2:任意数字加1,3:任意数字*2,都对10mod。 给你一个5位数n,问你12345能经过几次变化成为n。注意操作1只能用3次,2只能用2次。
POINT:
暴力打表做。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define LL long long
const int inf = 0x3f3f3f3f;
int flag[100100][5][5];
void dfs(int a[],int inc,int dou,int now)
{
int base=10000;
int p=0;
for(int i=0;i<=4;i++){
p+=base*a[i];
base=base/10;
}
if(flag[p][inc][dou]<=now){
return;
}
flag[p][inc][dou]=now;
int b[6];
if(inc<3){
for(int i=0;i<=4;i++){
for(int j=0;j<=4;j++) b[j]=a[j];
b[i]=b[i]+1;
b[i]%=10;
dfs(b,inc+1,dou,now+1);
}
}
if(dou<2){
for(int i=0;i<=4;i++){
for(int j=0;j<=4;j++) b[j]=a[j];
b[i]=b[i]*2;
b[i]%=10;
dfs(b,inc,dou+1,now+1);
}
}
for(int i=0;i<4;i++){
for(int j=0;j<=4;j++) b[j]=a[j];
swap(b[i], b[i+1]);
dfs(b,inc,dou,now+1);
}
}
int main()
{
int a[6];
a[0]=1,a[1]=2,a[2]=3,a[3]=4,a[4]=5;
memset(flag,inf,sizeof flag);
dfs(a,0,0,0);
int n;
while(~scanf("%d",&n)){
int ans=inf;
for(int i=0;i<=3;i++){
for(int j=0;j<=2;j++){
ans=min(ans,flag[n][i][j]);
}
}
if(ans==inf){
printf("-1\n");
}else printf("%d\n",ans);
}
return 0;
}