Hihocoder 1426 E. What a Ridiculous Election(BFS+优先队列)

20 篇文章 0 订阅

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;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值