前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >E - What a Ridiculous Election UVALive - 7672 【 BFS ,预处理各种状态 】

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

作者头像
Lokinli
发布2023-03-09 15:10:29
1340
发布2023-03-09 15:10:29
举报
文章被收录于专栏:以终为始以终为始

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;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-04-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • E - What a Ridiculous Election
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档