hihocoder1426 What a Ridiculous Election(bfs预处理)

5 篇文章 0 订阅
5 篇文章 0 订阅

The 2016 ACM-ICPC Asia Beijing Regional Contest

2016 ACM-ICPC 亚洲区域赛北京站E题

 

Problem 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"变成一个长度为5并且只由'0'~'9'字符组成的字符串

每次操作有3种选择

1.交换相邻的两个数字

2.将一个数字加1,如果大于9就模10

3.将一个数字乘2,如果大于9就模10

2操作最多能用3次,3操作最多能用2次。

思路:用bfs来预处理,得出“12345”通过以上操作能转换成的字符串的最少操作次数。

#include <bits/stdc++.h>
using namespace std;
#define maxn 100000
#define inf 2139062143
char s[10];
bool flag, vis[10];
int res[maxn][5][5];
struct node{
    int a[10], sec, th, step;
    bool operator < (const node &a) const{
        return step>a.step;
    }
};
node target;
int check(node now){
    int num = 0;
    for(int i = 1;i <= 5;i++)
        if(now.a[i]!=target.a[i]) return 0;
    return 1;
}
int turn(node now){
    int sum = 0;
    for(int i = 1;i <= 5;i++){
        sum += now.a[i];
        sum *= 10;
    }
    return sum / 10;
}
void bfs(node now){
    priority_queue<node> q;
    q.push(now);
    int tmp = turn(now), i;
    //printf("%d\n", tmp);
    res[tmp][now.sec][now.th] = 0;

    node u, next;
    bool flag;
    while(!q.empty()){
        u = q.top();
        q.pop();
        for(i = 2;i <= 5;i++) {
            next = u;
            next.step++;
            swap(next.a[i], next.a[i-1]);
            int p = turn(next);
            if(next.step>=res[p][next.sec][next.th]) continue;//printf("%d\n", p);
            q.push(next);
            res[p][next.sec][next.th] = next.step;
        }
        if(u.sec>0) {
            for(i = 1;i <= 5;i++){
                next = u;
                next.sec--;
                next.step++;
                next.a[i] = (next.a[i]+1)%10;
                int p = turn(next);
                if(next.step>=res[p][next.sec][next.th]) continue;
                q.push(next);
                res[p][next.sec][next.th] = next.step;
            }
        }
        if(u.th>0) {
            for(i = 1;i <= 5;i++){
                next = u;
                next.th--;
                next.step++;
                next.a[i] = (next.a[i]*2)%10;
                int p = turn(next);
                if(next.step>=res[p][next.sec][next.th]) continue;
                q.push(next);
                res[p][next.sec][next.th] = next.step;
            }
        }
    }
}
int main()
{
    int i, j, ans;
    node now;
    now.sec = 3, now.th = 2, now.step = 0;
    for(i = 1;i <= 5;i++) now.a[i] = i;
    memset(res, 127, sizeof res);
    bfs(now);

    while(~scanf("%s", s)){
        for(i = 1;i <= 5;i++) target.a[i] = s[i-1]-'0';
        int p = turn(target);
        ans = inf;
        for(i = 0;i <= 3;i++){
            for(j = 0;j <= 2;j++)
                if(res[p][i][j]<ans){
                    ans = res[p][i][j];
                }

        }
        if(ans==inf) printf("-1\n");
        else printf("%d\n", ans);
    }
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值