Https://vjudge.net/contest/259447#problem/E
Bfs,k constraints are presented in an extra K-dimension of the array.
1#include <bits/stdc++.h>2 using namespacestd;3 #definell Long Long4 #defineMINV 1e-65 #defineINF 1e96 #definePi 3.14159265367 #defineNL 2.71828182848 Constll mod=1e9+7;//9982443539 Const intmaxn=1e5+Ten;Ten One intqx[maxn* -],qy[maxn* -],num[maxn* -],step[maxn* -],ci[maxn],c[5]={1,Ten, -, +,10000},d[5]; A BOOLvis[maxn][4][3]; - - intMain () the { - intHead,tail,i,j,s,x,y,ss,bu; -Head=0, tail=1; - for(i=0; i<1e5;i++) +ci[i]=inf; -qx[1]=3, qy[1]=2, num[1]=12345, step[1]=0, ci[12345]=0, vis[12345][3][2]=1; + while(head<tail) A { athead++; -s=Num[head]; -x=Qx[head]; -y=Qy[head]; -bu=step[head]+1; -ss=s; in for(i=0;i<5; i++) -d[i]=ss%Ten, ss/=Ten; to for(i=0;i<4; i++) + { -j=i+1; theSs=s+ (C[i]-c[j]) * (d[j]-d[i]); * if(!Vis[ss][x][y]) $ {Panax Notoginsengtail++; -qx[tail]=x; theqy[tail]=y; +num[tail]=SS; Astep[tail]=bu; theci[ss]=min (ci[ss],bu); +vis[ss][x][y]=1; - } $ } $ if(x!=0) - for(i=0;i<5; i++) - { thess=s+c[i]* (d[i]==9?-9:1); - if(!vis[ss][x-1][y])Wuyi { thetail++; -qx[tail]=x-1; Wuqy[tail]=y; -num[tail]=SS; Aboutstep[tail]=bu; $ci[ss]=min (ci[ss],bu); -vis[ss][x-1][y]=1; - } - } A if(y!=0) + for(i=0;i<5; i++) the { -ss=s+c[i]* (d[i]*2%Ten-d[i]); $ if(!vis[ss][x][y-1]) the { thetail++; theqx[tail]=x; theqy[tail]=y-1; -num[tail]=SS; instep[tail]=bu; theci[ss]=min (ci[ss],bu); thevis[ss][x][y-1]=1; About } the } the } the while(~SCANF ("%d",&s)) +printf"%d\n", ci[s]==inf?-1: Ci[s]); - return 0; the}
The ACM-ICPC Asia Beijing regional Contest e-what a ridiculous election