E - What a Ridiculous Election UVALive - 7672 【 BFS ,预处理各种状态 】

2023-03-09 15:10:29 浏览数 (1)

E - What a Ridiculous Election

UVALive - 7672 

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 能够到达其他数的最小步数,在进行 O(1) 查询就可以了。这里的预先处理用 BFS 来进行。

代码语言:javascript复制
#include<bits/stdc  .h>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 1e5   10;
bool vis[maxn][5][5]; // 标记此状态是否到达过
struct node
{
    string now; // 当前的数字
    int step,a,b; // step记录的是步数,a是指的加操作次数,b指的乘操作次数
};
int ans[maxn]; // 存答案
int get(string x) // 化成数字
{
    int res = 0;
    for(int i = 0; i < 5; i   )
    {
        res *= 10;
        res  = x[i] - '0';
    }
    return res;
}
void bfs(node a)  
{
    memset(vis,0,sizeof(vis));
    int x;
    node w,l;
    queue<node>q; 
    q.push(a);
    x = get(a.now);
    vis[x][a.a][a.b] = 1; 
    while(!q.empty())
    {
        l = q.front();
        q.pop();
        x = get(l.now);
        ans[x] = min(ans[x],l.step); // x为当前数,去最小的步数
        string to;
        for(int i = 0; i < 5; i   ) // 五位数
        {
            to = l.now;
            if(l.a < 3)
            {
                if(to[i] < '9') // 加操作
                {
                    to[i]   ;
                }
                else to[i] = '0';
                x = get(to);
                w = {to,l.step 1,l.a 1,l.b}; 
                if(!vis[x][w.a][w.b]) // 如果没访问过,且是合法的
                {
                    q.push(w);
                    vis[x][w.a][w.b] = 1;
                }
            }
            to = l.now; // 别忘记是对队首的操作,也就是原串
            if(l.b < 2) // 两次乘操作
            {
                int temp = to[i] - '0';
                temp = temp * 2;
                temp %= 10;
                to[i] = temp   '0';
                x = get(to);
                w = {to, l.step 1,l.a,l.b 1};
                if(!vis[x][w.a][w.b])
                {
                    q.push(w);
                    vis[x][w.a][w.b] = 1;
                }
            }
        }
        to = l.now; // 这里也是,对于取出的q.front()操作
        w = {to,l.step 1,l.a,l.b};
        for(int i = 0; i < 4; i   )  // 交换,只能相邻交换
        {
            swap(w.now[i],w.now[i 1]);
            x = get(w.now);
            if(!vis[x][w.a][w.b])
            {
                q.push(w);
                vis[x][w.a][w.b] = 1;
            }
            swap(w.now[i],w.now[i 1]);
        }
    }
}
int main()
{
    ios::sync_with_stdio(0);
    node a;
    memset(ans,inf,sizeof(ans));
    a = {"12345",0,0,0};  // 预处理12345能够到达的状态
    bfs(a);
    string s;
    while(cin >> s)
    {
        if(s=="12345")
        {
            cout << 0<<endl;
        }
        else
        {
            int x = get(s);
            int res = ans[x];
            if(res == inf)
            {
                res = -1;
            }
            cout << res <<endl;
        }
    }
    return 0;
}

0 人点赞