AtCoder Beginner Contest 132 E - Hopscotch Addict
bfs
位置+状态
just need to calculate min value(only it is useful), so O(1*x)
挺有趣的一道题。。。
1 #include <cstdio> 2 #include <cstdlib> 3 #include <cmath> 4 #include <cstring> 5 #include <string> 6 #include <algorithm> 7 #include <iostream> 8 using namespace std; 9 #define ll long long 10 11 const int maxn=1e6+10; 12 13 struct node 14 { 15 int d; 16 node *to; 17 }*e[maxn]; 18 19 int qx[maxn*3],qy[maxn*3],qz[maxn*3]; 20 21 int f[maxn][3]; 22 23 int main() 24 { 25 node *p; 26 int n,m,x,y,z,xx,yy,zz,head,tail,s,t; 27 scanf("%d%d",&n,&m); 28 while (m--) 29 { 30 scanf("%d%d",&x,&y); 31 p=new node(); 32 p->d=y; 33 p->to=e[x]; 34 e[x]=p; 35 } 36 37 scanf("%d%d",&s,&t); 38 qx[1]=s,qy[1]=0,qz[1]=3; 39 head=0,tail=1; 40 while (head<tail) 41 { 42 head++; 43 x=qx[head]; 44 y=qy[head]; 45 z=qz[head]; 46 47 p=e[x]; 48 while (p) 49 { 50 xx=p->d; 51 yy=(y+1)%3; 52 zz=z+1; 53 if (!f[xx][yy]) 54 { 55 if (xx==t && yy==0) 56 { 57 printf("%d",(zz-3)/3); 58 return 0; 59 } 60 tail++; 61 qx[tail]=xx; 62 qy[tail]=yy; 63 qz[tail]=zz; 64 f[xx][yy]=zz; 65 } 66 p=p->to; 67 } 68 } 69 printf("-1"); 70 return 0; 71 }