2016 ICPC北京 problem E.What a Ridiculous Election(预处理bfs)

What a Ridiculous Election

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;

转换规则:

1.交换相邻的数;

2.其中一个数+1(限制次数3),若加完一大于9,则%10;

3.其中一个数*2(限制次数2),若加完一大于9,则%10;

解析:

可以这么想,“12345”模拟变化,怎么到达目标字符串。但是怎么求最小。

不如我们直接预处理,求出12345可以变化到的所有字符串。应用bfs+队列。

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

#define e exp(1)
#define pi acos(-1)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define ll long long
#define ull unsigned long long
#define mem(a,b) memset(a,b,sizeof(a))
int gcd(int a,int b){return b?gcd(b,a%b):a;}

string ss;
int ans[100000][4][3];
struct qnode{
    string s;
    int op2,op3,step;
    qnode(){}
    qnode(string _s,int _op2,int _op3,int _step){s=_s,op2=_op2,op3=_op3,step=_step;}
    bool operator<(const qnode &r)const
    {
        return step<r.step;
    }
};

int Strint(string ss)
{
    int sum=0;
    for(int i=0; i<5; i++)
    {
        sum=sum*10+ss[i]-'0';
    }
    return sum;
}
void dfs()
{
    ans[12345][0][0]=0;
    queue<qnode> que;
    que.push(qnode("12345",0,0,0));
    qnode tmp,q;
    int num;
    while(!que.empty())
    {
        tmp=que.front();que.pop();
        q.step=tmp.step+1;

        for(int i=0; i<4; i++)
        {
            q.s=tmp.s;
            swap(q.s[i],q.s[i+1]);
            q.op2=tmp.op2;q.op3=tmp.op3;
            num=Strint(q.s);
            if(ans[num][q.op2][q.op3]!=inf)continue;
            ans[num][q.op2][q.op3]=q.step;

            que.push(q);
        }

        if(tmp.op2<3)
        {
            for(int i=0; i<5; i++)
            {
                q.s=tmp.s;
                q.s[i]=char(((q.s[i]-'0')+1)%10+'0');
                q.op2=tmp.op2+1;q.op3=tmp.op3;

                num=Strint(q.s);
                if(ans[num][q.op2][q.op3]!=inf)continue;
                ans[num][q.op2][q.op3]=q.step;


                que.push(q);
            }
        }
        if(tmp.op3<2)
        {
            for(int i=0; i<5; i++)
            {
                q.s=tmp.s;
                q.s[i]=char(((q.s[i]-'0')*2)%10+'0');
                q.op2=tmp.op2;q.op3=tmp.op3+1;

                num=Strint(q.s);
                if(ans[num][q.op2][q.op3]!=inf)continue;
                ans[num][q.op2][q.op3]=q.step;

                que.push(q);
            }

        }
    }
}
int main()
{
    mem(ans,inf);
    dfs();
    while(cin>>ss)
    {
        int num=Strint(ss),mx=inf;
        for(int i=0; i<=3; i++)
        {
            for(int j=0; j<=2; j++)
            {
                mx=min(ans[num][i][j],mx);
            }
        }
        if(mx==inf)puts("-1");
        else printf("%d\n",mx);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值