The 2016 ACM-ICPC Asia Beijing Regional Contest E - What a Ridiculous Election

 

https://vjudge.net/contest/259447#problem/E

 

bfs,k个限制条件以数组的额外k维呈现。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 #define ll long long
 4 #define minv 1e-6
 5 #define inf 1e9
 6 #define pi 3.1415926536
 7 #define nl 2.7182818284
 8 const ll mod=1e9+7;//998244353
 9 const int maxn=1e5+10;
10 
11 int qx[maxn*100],qy[maxn*100],num[maxn*100],step[maxn*100],ci[maxn],c[5]={1,10,100,1000,10000},d[5];
12 bool vis[maxn][4][3];
13 
14 int main()
15 {
16     int head,tail,i,j,s,x,y,ss,bu;
17     head=0,tail=1;
18     for (i=0;i<1e5;i++)
19         ci[i]=inf;
20     qx[1]=3,qy[1]=2,num[1]=12345,step[1]=0,ci[12345]=0,vis[12345][3][2]=1;
21     while (head<tail)
22     {
23         head++;
24         s=num[head];
25         x=qx[head];
26         y=qy[head];
27         bu=step[head]+1;
28         ss=s;
29         for (i=0;i<5;i++)
30             d[i]=ss%10,ss/=10;
31         for (i=0;i<4;i++)
32         {
33             j=i+1;
34             ss=s+(c[i]-c[j])*(d[j]-d[i]);
35             if (!vis[ss][x][y])
36             {
37                 tail++;
38                 qx[tail]=x;
39                 qy[tail]=y;
40                 num[tail]=ss;
41                 step[tail]=bu;
42                 ci[ss]=min(ci[ss],bu);
43                 vis[ss][x][y]=1;
44             }
45         }
46         if (x!=0)
47             for (i=0;i<5;i++)
48             {
49                 ss=s+c[i]*(d[i]==9?-9:1);
50                 if (!vis[ss][x-1][y])
51                 {
52                     tail++;
53                     qx[tail]=x-1;
54                     qy[tail]=y;
55                     num[tail]=ss;
56                     step[tail]=bu;
57                     ci[ss]=min(ci[ss],bu);
58                     vis[ss][x-1][y]=1;
59                 }
60             }
61         if (y!=0)
62             for (i=0;i<5;i++)
63             {
64                 ss=s+c[i]*(d[i]*2%10-d[i]);
65                 if (!vis[ss][x][y-1])
66                 {
67                     tail++;
68                     qx[tail]=x;
69                     qy[tail]=y-1;
70                     num[tail]=ss;
71                     step[tail]=bu;
72                     ci[ss]=min(ci[ss],bu);
73                     vis[ss][x][y-1]=1;
74                 }
75             }
76     }
77     while (~scanf("%d",&s))
78         printf("%d\n",ci[s]==inf?-1:ci[s]);
79     return 0;
80 }

 

posted @ 2018-10-22 13:22  congmingyige  阅读(202)  评论(0编辑  收藏  举报