1. 程式人生 > >Hihocoder 1426 E. What a Ridiculous Election

Hihocoder 1426 E. What a Ridiculous Election

Description

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、交換相鄰的兩個字串,不限次數。

  2、某一字元+1,>=10則%10.此操作最多三次。

  3、某一字元*2,>=10則%10.此操作最多兩次。

  問最少通過多少次轉化為目標字串,如果不能,輸出-1.

  思路:

  BFS。

  優先佇列優先把步數少的放在隊頭。

#include<bits/stdc++.h>
using namespace std;

int minStp[100000][4][3];
string str;

struct Node
{
    string s;
    int op2,op3,stp;
    Node(){};
    Node(string _s,int _op2,int _op3,int _stp)
    {
        s=_s,op2=_op2,op3=_op3,stp=_stp;
    }
}now,nex;

bool operator<(Node a,Node b)
{
    return a.stp>b.stp;
}//按步數從小到大排

int str2int(string s)
{
    int ret=0;
    for(int i=0;i<5;i++)
        ret*=10,ret+=s[i]-'0';
    return ret;
}

void bfs()
{
    priority_queue<Node>q;
    q.push(Node("12345", 0, 0, 0));
    minStp[12345][0][0]=0;
    int num;
    while(!q.empty())
    {
        now=q.top();
        q.pop();
        nex.stp=now.stp+1;
        for(int i=0;i<4;i++)//一步交換操作,任意兩個相鄰的字元
        {
            nex.s=now.s;
            swap(nex.s[i],nex.s[i+1]);
            nex.op2=now.op2,nex.op3=now.op3;
            num=str2int(nex.s);
            if(minStp[num][nex.op2][nex.op3]!=0x3f3f3f3f) continue;//之前的轉化已經變化出當前的num;
            minStp[num][nex.op2][nex.op3]=nex.stp;
            q.push(nex);
        }
        if(now.op2<3)//操作2最多進行3次
        {
            for(int i=0;i<5;i++)//可能對每一個字元進行操作
            {
                nex.s=now.s;
                nex.s[i]=char(((nex.s[i]-'0')+1)%10+'0');
                nex.op2=now.op2+1,nex.op3=now.op3;
                num=str2int(nex.s);
                if(minStp[num][nex.op2][nex.op3]!=0x3f3f3f3f) continue;
                minStp[num][nex.op2][nex.op3]=nex.stp;
                q.push(nex);
            }
        }
        if(now.op3<2)//操作3最多進行2次
        {
            for(int i=0;i<5;i++)
            {
                nex.s=now.s;
                nex.s[i]=char(((nex.s[i]-'0')*2)%10+'0');
                nex.op2=now.op2,nex.op3=now.op3+1;
                num=str2int(nex.s);
                if(minStp[num][nex.op2][nex.op3]!=0x3f3f3f3f) continue;
                minStp[num][nex.op2][nex.op3]=nex.stp;
                q.push(nex);
            }
        }
    }
}

int main()
{
    memset(minStp,0x3f3f3f3f,sizeof(minStp));
    bfs();
    while(cin>>str)
    {
        int mn=0x3f3f3f3f;
        for(int i=0;i<=3;i++)
            for(int j=0;j<=2;j++)
                mn=min(mn, minStp[str2int(str)][i][j]);
        printf("%d\n",mn==0x3f3f3f3f?-1:mn);
    }
    return 0;
}