Test Instructions
The frog went straight until he ran into an obstacle, and at this point he had to turn right and go straight. How many times do you want to turn right without hitting an obstacle, infinite loop output-1. Solving
There are only 1000 obstacles, which are discretized with map, record an obstacle on X or Y with set, then simulate, record the state with an obstacle, repeat state output-1, otherwise output the answer. Code
/****************************************\ * AUTHOR:ZTX * title:j-Right Turn * ALG: * cmt:g++ i64d * Time : \****************************************/#include <cstdio> #define REP (i,l,r) for (i= (l); i<= (R); i++) #def Ine Rep (i,l,r) for (i= (L);i< (R), i++) #define REV (I,r,l) for (i= (R); i>= (l); i--) #define Rev. (I,r,l) for (i= (R);i> (
l); i--) typedef long Long LL;
typedef double LF;
int CH, NEG;
Template <typename tp>inline void Read (tp& ret) {ret = NEG = 0; while (Ch=getchar (), ch< '! ');
if (ch = = '-') NEG = true, ch = getchar ();
while (ret = ret*10+ch-' 0 ', Ch=getchar (), ch> '! ');
if (NEG) ret =-ret;
} template <typename tp>inline void readc (tp& ret) {while (Ret=getchar (), ret< '! ');
while (Ch=getchar (), ch> '! ');
} template <typename Tp>inline void reads (TP *ret) {ret[0]=0;while (Ch=getchar (), ch< '! ');
while (Ret[++ret[0]]=ch,ch=getchar (),ch> '! '); Ret[ret[0]+1]=0; } #include <map> #include <set> #include <cstring> #define MAXN 1010LL struct Node {int pos,
ID;
BOOL operator < (const node& B) Const {return pos < B.pos;
}
} ;
int n, ans;
int X[MAXN], Y[MAXN];
int XX[MAXN], YY[MAXN];
int idx, Idy;
STD::MAP<INT,INT>MAPX, Mapy;
STD::SET<NODE>ROW[MAXN], COL[MAXN];
Std::set<node>:: iterator it;
BOOL REC[5][MAXN]; Xiang Zhuang int main () {int I, TMP, R, now, dir;//#define READ #ifdef read Freopen (". In", "R
", stdin);
Freopen (". Out", "w", stdout);
#endif while (scanf ("%d", &n)! = EOF) {idx = Idy = 0;
Mapx.clear (), mapy.clear ();
Rep (I,1,n) row[i].clear (), col[i].clear ();
Rep (i,1,n) {read (X[i]), read (Y[i]);
if (tmp=mapx[x[i]],tmp) xx[i] = tmp;
else Mapx[x[i]] = + + idx, xx[i] = idx;
if (tmp=mapy[y[i]],tmp) yy[i] = tmp; else Mapy[y[i]] = + + Idy, yy[i] = Idy;
Row[yy[i]].insert (node) {x[i],i}), Col[xx[i]].insert ((node) {y[i],i});
} #define RIGHT 1 #define DOWN 2 #define LEFT 3 #define up 4 ans = 0;
memset (Rec,0,sizeof rec);
r = mapy[0];
if (!r) {puts ("0"); goto END;
} it = Row[r].upper_bound ((node) {0,0});
if (it = = Row[r].end ()) {puts ("0"); Goto end;
} now = It->id;
Rec[right][now] = true;
dir = right;
while (ans + +, dir= (dir%4) +1,true) {//printf ("ans =%d,now%d%d\n", Ans,x[now],y[now]);
if (dir = = right) {r = mapy[y[now]-1];
if (!r) goto ANS;
it = Row[r].upper_bound ((node) {x[now],0});
if (it = = Row[r].end ()) goto ANS;
now = It->id;
if (Rec[right][now]) {puts ("-1"); goto END; } Rec[right][now] = true;
} if (dir = = down) {r = mapx[x[now]-1];
if (!r) goto ANS;
it = Col[r].upper_bound ((node) {y[now],0});
if (it = = Col[r].begin ()) goto ANS;
It--;
now = It->id;
if (Rec[down][now]) {puts ("-1"); goto END;
} Rec[down][now] = true;
} if (dir = = left) {r = mapy[y[now]+1];
if (!r) goto ANS;
it = Row[r].upper_bound ((node) {x[now],0});
if (it = = Row[r].begin ()) goto ANS;
It--;
now = it-> ID;
if (Rec[left][now]) {puts ("-1"); goto END;
} Rec[left][now] = true;
} if (dir = = up) {r = mapx[x[now]+1]; if (!r) goto ANS;
it = Col[r].upper_bound ((node) {y[now],0});
if (it = = Col[r].end ()) goto ANS;
now = It->id;
if (Rec[up][now]) {puts ("-1"); goto END;
} Rec[up][now] = true;
}} ANS:;
printf ("%d\n", ans);
END:; } #ifdef READ fclose (stdin);
Fclose (stdout); #else GetChar ();
GetChar ();
#endif return 0;
}