2016亚洲区域赛现场赛北京赛区e题

6 篇文章 0 订阅
4 篇文章 0 订阅

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
题意:给你一个长度为5的字符串,每个都是数字,然后给你下面三种操作
操作一:交换相连的两个数字
操作二:将一个数字加一,如果大于9,与10取模。此操作最多3次
操作三:将一个数字乘以二,如果大于9,与10取模。此操作最多2次
问你能不能把“12345”变成给定的字符,只能通过上面三种操作,且每一种操作次数不能超过规定次数。

解题思路:通过bfs处理每一种状态的后续状态,然后把所有的状态记录下来,直接查询就行,也就是把“12345”能够达到的所有状态都处理出来。ans[i][j][k]表示从字符串“12345”到达字符串“xxxxx”(把xxxxx当成整数得到的值为i)操作2的剩余次数为j,操作3剩余的次数为k的最小步骤数。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
using namespace std;
const int maxn = 1e5 + 10;
const int inf = 0x3f3f3f3f;
char s[10];
int ten[10];
struct node{
    string num;
    int step;
    int inc;
    int dou;
};
int ans[maxn][5][5];
bool visit[maxn][5][5];
queue<node> Q;
node nx;
int transfer(string p)
{
    int sum = 0;
    int j = 0;
    for(int i = 4; i >= 0; i--)
    {
        sum += (p[i] - '0')*ten[j++];
    }
    return sum;
}
void bfs(node n0)
{
    Q.push(n0);
    while(!Q.empty())
    {
        node n = Q.front();
        Q.pop();

        int value = transfer(n.num);
        ans[value][n.inc][n.dou] = min(ans[value][n.inc][n.dou],n.step);
        visit[value][n.inc][n.dou] = true;

        node x;
        for(int i = 0; i <= 3; i++)
        {
            x.num = n.num;
            x.num[i] = n.num[i + 1];
            x.num[i + 1] = n.num[i];
            value = transfer(x.num);
            if(!visit[value][n.inc][n.dou])
            {
                x.step = n.step + 1;
                x.inc = n.inc;
                x.dou = n.dou;
                Q.push(x);
                visit[value][n.inc][n.dou] = true;
            }
        }
        if(n.inc > 0)
        {
            for(int i = 0; i <= 4; i++)
            {
               x.num = n.num;
               int term = n.num[i] - '0';
               term++;
               term %= 10;
               x.num[i] = term + '0';
               value = transfer(x.num);
               if(!visit[value][n.inc - 1][n.dou])
               {
                  x.step = n.step + 1;
                  x.inc = n.inc - 1;
                  x.dou = n.dou;
                  Q.push(x);
                  visit[value][n.inc - 1][n.dou] = true;
               }
            }
        }
        if(n.dou > 0)
        {
              for(int i = 0; i <= 4; i++)
              {
                 x.num = n.num;
                 int term = n.num[i] - '0';
                 term *= 2;
                 term %= 10;
                 x.num[i] = term + '0';
                 value = transfer(x.num);
                 if(!visit[value][n.inc][n.dou - 1])
                 {
                   x.step = n.step + 1;
                   x.inc = n.inc;
                   x.dou = n.dou - 1;
                   Q.push(x);
                   visit[value][n.inc][n.dou - 1] = true;
                 }
              }
        }
    }
}
void init()
{
    while(!Q.empty()) Q.pop();
    nx.step = 0;
    nx.inc = 3;
    nx.dou = 2;
    nx.num = "12345";
    ten[0] = 1;
    for(int i = 1; i <= 6; i++)
    {
        ten[i] = ten[i - 1]*10;
    }
    memset(visit,false,sizeof(visit));
    memset(ans,inf,sizeof(ans));

}
int main()
{
    init();
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    bfs(nx);

    while(~scanf("%s",s))
    {
        node sx;
        sx.num = nx.num;
        for(int i = 0; i <= 4; i++)
        {
            sx.num[i] = s[i];
        }
        int res = transfer(sx.num);
        //cout<<res<<endl;
        int Min = inf;
        for(int i = 0; i <= 3; i++)
        {
            for(int j = 0; j <= 2; j++)
            {
                //cout<<ans[res][i][j]<<endl;
                Min = min(Min,ans[res][i][j]);
            }
        }
        if(Min == inf) printf("-1\n");
        else printf("%d\n",Min);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值