What a Ridiculous Election UVALive

题目链接:

E - What a Ridiculous Election

 UVALive - 7672 

题目大意:

12345 可以经过若干次操作转换为其它五位数。

操作分三种,分别为:

操作1:交换相邻两数
操作2:选择一位 +1,若大于 9 ,则对 10 取模。
操作3:选择一位 *2 ,若大于 9,则对 10 取模。
其中操作 2 最大进行 3 次,操作 3 最多进行 2 次。

对于给定的五位数,求 12345 在满足限制条件情况下,最少通过几步操作可以转换为目标五位数。若不可能,则输出 -1 。

具体思路:bfs,需要从12345作为起点向其他点跑,把所有情况都算出来、不能输入一个数作为起点。

a[i][j][k]代表12345变成i需要操作二j次,操作三k次,每一次输出遍历j和k就可以了。

AC代码:

  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 # define ll long long
  4 # define inf 0x3f3f3f3f
  5 const int maxn = 1e5+100;
  6 int a[maxn][4][4];
  7 int sto[10];
  8 struct node
  9 {
 10     int num;
 11     int add;
 12     int dou;
 13     int step;
 14     node() {}
 15     node(int xx,int yy,int zz,int kk)
 16     {
 17         num=xx;
 18         add=yy;
 19         dou=zz;
 20         step=kk;
 21     }
 22 };
 23 int cal()
 24 {
 25     int ans=0;
 26     for(int i=1; i<=5; i++)
 27     {
 28         ans=ans*10+sto[i];
 29     }
 30     return ans;
 31 }
 32 void chuan(int n)
 33 {
 34     sto[1]=n%10;
 35     n/=10;
 36     sto[2]=n%10;
 37     n/=10;
 38     sto[3]=n%10;
 39     n/=10;
 40     sto[4]=n%10;
 41     n/=10;
 42     sto[5]=n%10;
 43     n/=10;
 44     for(int i=1; i<=2; i++)
 45     {
 46         swap(sto[i],sto[5-i+1]);
 47     }
 48 }
 49 void bfs()
 50 {
 51     queue<node>q;
 52     q.push(node(12345,0,0,0));
 53     a[12345][0][0]=0;
 54     int tmp;
 55     while(!q.empty())
 56     {
 57         node top=q.front();
 58         q.pop();
 59         chuan(top.num);
 60         if(top.add+1<=3)
 61         {
 62             for(int j=1; j<=5; j++)
 63             {
 64                 tmp=sto[j];
 65                 sto[j]++;
 66                 sto[j]%=10;
 67                 int tt=cal();
 68                 if(a[tt][top.add+1][top.dou]==inf)
 69                     q.push(node(tt,top.add+1,top.dou,top.step+1)),a[tt][top.add+1][top.dou]=top.step+1;
 70                 sto[j]=tmp;
 71             }
 72         }
 73         if(top.dou+1<=2)
 74         {
 75             for(int j=1; j<=5; j++)
 76             {
 77                 tmp=sto[j];
 78                 sto[j]<<=1;
 79                 sto[j]%=10;
 80                 int tt=cal();
 81                 if(a[tt][top.add][top.dou+1]==inf)
 82                 {
 83                     q.push(node(tt,top.add,top.dou+1,top.step+1)),a[tt][top.add][top.dou+1]=top.step+1;
 84                 }
 85                 sto[j]=tmp;
 86             }
 87         }
 88         for(int j=1; j<5; j++)
 89         {
 90             swap(sto[j],sto[j+1]);
 91             int tt=cal();
 92             if(a[tt][top.add][top.dou]==inf)
 93             {
 94                 q.push(node(tt,top.add,top.dou,top.step+1)),a[tt][top.add][top.dou]=top.step+1;
 95             }
 96             swap(sto[j],sto[j+1]);
 97         }
 98     }
 99 }
100 int main()
101 {
102     memset(a,inf,sizeof(a));
103     bfs();
104   //  chuan(12345);
105     int n;
106     int ttt ;
107     while(~scanf("%d",&n))
108     {
109         int minn = inf;
110         for(int i=0; i<=3; i++)
111         {
112             for(int j=0; j<=2; j++)
113             {
114                 minn = min( minn, a[n][i][j] );
115             }
116         }
117         printf("%d
",minn==inf ? -1 : minn);
118     }
119     return 0;
120 }

 

原文地址:https://www.cnblogs.com/letlifestop/p/10734983.html