题目链接:https://vjudge.net/problem/SCU-4445
1 #include<cstdio> 2 #include<cstring> 3 #include<cmath> 4 #include<algorithm> 5 using namespace std; 6 7 const int maxn=1010; 8 const int inf=0x3f3f3f3f; 9 10 struct node 11 { 12 int x,y; 13 }po[maxn<<2]; 14 15 int main() 16 { 17 int n; 18 while(scanf("%d",&n)!=EOF) 19 { 20 for(int i=1;i<=n;i++) 21 scanf("%d%d",&po[i].x,&po[i].y); 22 int step=0; 23 int nowx=0,nowy=0; 24 int f=2; 25 while(step<=4*n+1) 26 { 27 int tx=-inf,ty=-inf; 28 if(f==1) 29 { 30 for(int i=1;i<=n;i++) 31 if(po[i].x==nowx&&po[i].y>nowy) 32 { 33 tx=po[i].x; 34 if(ty!=-inf) ty=min(ty,po[i].y); 35 else ty=po[i].y; 36 } 37 38 if(tx==-inf||ty==-inf) break; 39 step++; 40 nowx=tx; 41 nowy=ty-1; 42 f=2; 43 } 44 else if(f==2) 45 { 46 for(int i=1;i<=n;i++) 47 if(po[i].y==nowy&&po[i].x>nowx) 48 { 49 ty=po[i].y; 50 if(tx!=-inf) tx=min(tx,po[i].x); 51 else tx=po[i].x; 52 } 53 if(tx==-inf||ty==-inf) break; 54 step++; 55 nowx=tx-1; 56 nowy=ty; 57 f=3; 58 59 } 60 else if(f==3) 61 { 62 for(int i=1;i<=n;i++) 63 if(po[i].x==nowx&&po[i].y<nowy) 64 { 65 tx=po[i].x; 66 if(ty!=-inf) ty=max(ty,po[i].y); 67 else ty=po[i].y; 68 } 69 if(tx==-inf||ty==-inf) break; 70 step++; 71 nowx=tx; 72 nowy=ty+1; 73 f=4; 74 75 } 76 else 77 { 78 for(int i=1;i<=n;i++) 79 if(po[i].y==nowy&&po[i].x<nowx) 80 { 81 ty=po[i].y; 82 if(tx!=-inf) tx=max(tx,po[i].x); 83 else tx=po[i].x; 84 } 85 if(tx==-inf||ty==-inf) break; 86 step++; 87 nowx=tx+1; 88 nowy=ty; 89 f=1; 90 91 } 92 } 93 if(step>4*n+1) puts("-1"); 94 else printf("%d ",step); 95 } 96 return 0; 97 98 }