HihoCoder - 1426 What a Ridiculous Election (BFS预处理)

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

分析:该问题求 有限制的最小步数,题目中的测试数据很多,故需要进行预处理. 预处理的时候要用到BFS

BFS的性质是先出现的符合要求的步数一定最小,所以用ans[a.val][a.cnt2][a.cnt3]表示数a.val  在操作2数目为a.cnt2

操作3数目为a.cnt3的状态的最小步数.

代码如下:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std;
const int MAXN=1e5+100;
int ans[MAXN][5][5];
int tmp;
int cnt2[MAXN];
int cnt3[MAXN];
struct Node
{
    int x[10];
    int step;
    int cnt2;
    int cnt3;
    int val;
};
queue<Node>Q;
void bfs()
{

memset(ans,-1,sizeof(ans));
memset(cnt2,-1,sizeof(cnt2));
memset(cnt3,-1,sizeof(cnt3));
  Node a,next;
   ans[12345][0][0]=0;
  for(int i=1;i<=5;i++)
  a.x[i]=i;
  a.step=0;
  a.cnt2=0;
  a.cnt3=0;
  a.val=12345;
  cnt2[12345]=0;
  cnt3[12345]=0;
  Q.push(a);
  while(!Q.empty())
  {
     a=Q.front();
     Q.pop();
  //  printf("%d\n",a.step);
     for(int i=1;i<=5;i++)
     {
         for(int j=1;j<=5;j++)
            next.x[j]=a.x[j];
         if(a.cnt2<3){
          next.x[i]=(a.x[i]+1)%10;
           next.val=next.x[1]*10000+next.x[2]*1000+next.x[3]*100+next.x[4]*10+next.x[5];
           next.step=a.step+1;
           next.cnt2=a.cnt2+1;
           next.cnt3=a.cnt3;
            if(ans[next.val][next.cnt2][next.cnt3]==-1)
          {
           ans[next.val][next.cnt2][next.cnt3]=next.step;
           Q.push(next);
         }
         }
         for(int j=1;j<=5;j++)
            next.x[j]=a.x[j];
         if(a.cnt3<2){
             next.x[i]=(a.x[i]*2)%10;
              next.val=next.x[1]*10000+next.x[2]*1000+next.x[3]*100+next.x[4]*10+next.x[5];
           next.step=a.step+1;
           next.cnt2=a.cnt2;
           next.cnt3=a.cnt3+1;
            if(ans[next.val][next.cnt2][next.cnt3]==-1)
          {
           ans[next.val][next.cnt2][next.cnt3]=next.step;
           Q.push(next);
         }
         }
         //  }
    }


      for(int j=1;j<=5;j++)
   next.x[j]=a.x[j];
     for(int i=1;i<=4;i++)
      {

        swap(next.x[i],next.x[i+1]);
         next.val=next.x[1]*10000+next.x[2]*1000+next.x[3]*100+next.x[4]*10+next.x[5];
          next.step=a.step+1;
           next.cnt2=a.cnt2;
           next.cnt3=a.cnt3;
            if(ans[next.val][next.cnt2][next.cnt3]==-1)
          {
           ans[next.val][next.cnt2][next.cnt3]=next.step;
           Q.push(next);
         }

         swap(next.x[i],next.x[i+1]);
      }
  }
}
int main()
{
   bfs();
 /*  freopen("1.out","w",stdout);
  int n;
     for(int i=0;i<=99999;i++)
    printf("%d %d\n",i,ans[i]);*/
    int n,res;
    while(scanf("%d",&n)!=EOF)
    {
        res=1000000;
        for(int i=0;i<=3;i++)
            for(int j=0;j<=2;j++){
                if(ans[n][i][j]!=-1)
            res=min(ans[n][i][j],res);
            }
            if(res==1000000)
                puts("-1");
            else
        printf("%d\n",res);
    }

    return 0;
}

 

posted @ 2017-08-29 09:54  hinata_hajime  阅读(283)  评论(0编辑  收藏  举报