UVALive

来源:互联网 发布:淘宝手机充值 编辑:程序博客网 时间:2024/06/05 21:16

In country Light Tower, a presidential election is going on. There are two candidates, Mr. X1andMr. 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 supportX1are almost as many as the people who support X2.

After the election day, X1and X2get 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

124359999912374

Sample Output

1

-1

3

 


题意:将“12345”变为所给串所需要的最少操作次数,若无法变成目标数则输出"-1"

操作有三种:1、交换相邻的两个数字2、某一位数字加一(模10) 3、某一位数字乘二(模10)

其中操作2最多使用三次,操作3最多使用两次


若所有状态都可以达到最多有1e5*3*4种情况,不是很多,所以可以先将所有的情况求出,利用DFS或BFS


#include<iostream>#include<stdio.h>#include<string.h>#include<queue>using namespace std;const int N = 99999 + 10;const int INF = 0x3f3f3f3f;int Time[N][5][5];void dfs(char s[],int dou,int add,int cnt){    int sum = 0;    for(int i=0;i<5;i++) sum = sum * 10 + s[i] - '0';    if(Time[sum][add][dou]>cnt){        Time[sum][add][dou] = cnt;    }else return;    char str[10];    for(int i=0;i<5;i++){                if(dou<2){            for(int j=0;j<5;j++) str[j] = s[j];            str[i] = (str[i]-'0') * 2 % 10 + '0';            dfs(str,dou+1,add,cnt+1);        }        if(add<3){            for(int j=0;j<5;j++) str[j] = s[j];            str[i] = (str[i] - '0' + 1) % 10 + '0';            dfs(str,dou,add+1,cnt+1);        }    }    for(int i=0;i<4;i++){        swap(s[i],s[i+1]);        for(int j=0;j<5;j++) str[j] = s[j];        dfs(str,dou,add,cnt+1);        swap(s[i],s[i+1]);    }}struct node{    int num,add,dou,cnt;    node(int a,int b,int c,int d){        num = a;add = b;dou = c;cnt = d;    }};void bfs(){    node st(12345,0,0,0);    queue<node>q;    q.push(st);    Time[12345][0][0] = 0;    while(!q.empty()){        st = q.front();q.pop();        int num[10],n = st.num;        for(int i=4;i>=0;i--) {num[i] = n%10;n/=10;}        for(int i=0;i<5;i++){            int temp = num[i];            if(st.add<3){                n = 0;                num[i]++; num[i]%=10;                for(int j=0;j<5;j++) n = n*10 + num[j];                if(Time[n][st.add+1][st.dou]==INF){                    Time[n][st.add+1][st.dou] = st.cnt + 1;                    q.push(node(n,st.add+1,st.dou,st.cnt+1));                }                num[i] = temp;            }            if(st.dou<2){                n = 0;                num[i] *= 2; num[i]%=10;                for(int j=0;j<5;j++) n = n*10 + num[j];                if(Time[n][st.add][st.dou+1]==INF){                    Time[n][st.add][st.dou+1] = st.cnt + 1;                    q.push(node(n,st.add,st.dou+1,st.cnt+1));                }                num[i] = temp;            }        }        for(int i=0;i<4;i++){            n = 0;            swap(num[i],num[i+1]);            for(int j=0;j<5;j++) n = n*10 + num[j];            if(Time[n][st.add][st.dou]==INF){                Time[n][st.add][st.dou] = st.cnt + 1;                q.push(node(n,st.add,st.dou,st.cnt+1));            }            swap(num[i],num[i+1]);        }    }}int main(){    memset(Time,INF,sizeof Time);    char s[10]="12345";//    dfs(s,0,0,0);    bfs();    while(scanf("%s",s)!=EOF){        int num = 0;        for(int i=0;i<5;i++) num = num * 10 + s[i] - '0';        int ans = INF;        for(int i=0;i<=3;i++){            for(int j=0;j<=2;j++){                ans = min(ans,Time[num][i][j]);            }        }        if(ans == INF) printf("-1\n");        else printf("%d\n",ans);    }    return 0;}


原创粉丝点击