Right turn
frog is trapped in a maze. The maze is infinitely large and divided into grids. It also consists of n n obstacles, where the i i-th obstacle lies in grid (x i ,y i ) (xi,yi).
frog is initially in grid (0,0) (0,0), heading grid (1,0) (1,0). She moves according to The Law of Right Turn: she keeps moving forward, and turns right encountering a obstacle.
The maze is so large that frog has no chance to escape. Help her find out the number of turns she will make.
Input
The input consists of multiple tests. For each test:
The first line contains 1 1 integer n n ( 0≤n≤10 3 0≤n≤103). Each of the following n n lines contains 2 2 integers x i ,y i xi,yi. ( |x i |,|y i |≤10 9 ,(x i ,y i )≠(0,0) |xi|,|yi|≤109,(xi,yi)≠(0,0), all (x i ,y i ) (xi,yi) are distinct)
Output
For each test, write
1
1 integer which denotes the number of turns, or ``-1
'' if she makes infinite turns.
Sample Input
2
1 0
0 -1
1
0 1
4
1 0
0 1
0 -1
-1 0
Sample Output
2
0
-1
意:一个迷宫有n块石头,一个人从(0,0)出发,一开始是面向右边,如果他遇到石头就要向右转,问这个人要转几次弯,如果会无限次转弯就输出-1
题解:离散+模拟,如果碰到石头就右转,直到再次回到原路线时,表示会无限循环输出-1,否则一定会走出迷宫输出转弯数.
#include<cstdio>
#include<algorithm>
#include<string.h>
#include<queue>
#include<set>
#include<functional>
#include<iostream>
#include<math.h>
#include<vector>
#include<stdlib.h>
using namespace std;
const int maxn = 1e3+ 5;
const int inf = 0x3fffffff;
struct node{
int x,y;
}p[maxn];
bool cmp1(node p1,node p2){
if(p1.x!=p2.x) return p1.x<p2.x;
return p1.y<p2.y;
}
bool cmp2(node p1,node p2){
if(p1.y!=p2.y) return p1.y<p2.y;
return p1.x<p2.x;
}
int xx[maxn],yy[maxn],xxx[maxn],yyy[maxn];
bool vis1[maxn][4],vis2[maxn][4];
int main(){
int n;
// freopen("in.txt","r",stdin);
while(~scanf("%d",&n)){
memset(vis1,0,sizeof(vis1));
memset(vis2,0,sizeof(vis2));
for(int i=0;i<n;i++)
scanf("%d%d",&p[i].x,&p[i].y);
sort(p,p+n,cmp1);
for(int i=0;i<n;i++){
xx[i]=p[i].x;
yy[i]=p[i].y;
}
sort(p,p+n,cmp2);
for(int i=0;i<n;i++){
xxx[i]=p[i].x;
yyy[i]=p[i].y;
}
bool mark=true;
int flag=0,x=0,y=0,cnt=0;
while(1){
if(flag==3){
int st=lower_bound(xx,xx+n,x)-xx;
int ed=upper_bound(xx,xx+n,x)-xx;
int pos=inf,index=-1;
for(int i=st;i<ed;i++) if(yy[i]>y&&yy[i]<pos) {pos=yy[i];index=i;}
if(pos==inf) {mark=false;break;}
y=pos-1;
if(vis1[index][flag]==1) break;
vis1[index][flag]=1;
flag=0;
}
else if(flag==0){
int st=lower_bound(yyy,yyy+n,y)-yyy;
int ed=upper_bound(yyy,yyy+n,y)-yyy;
int pos=inf,index=-1;
for(int i=st;i<ed;i++) if(xxx[i]>x&&xxx[i]<pos) {pos=xxx[i];index=i;}
if(pos==inf) {mark=false;break;}
x=pos-1;
if(vis2[index][flag]==1) break;
vis2[index][flag]=1;
flag=1;
}
else if(flag==1){
int st=lower_bound(xx,xx+n,x)-xx;
int ed=upper_bound(xx,xx+n,x)-xx;
int pos=-inf,index=-1;
for(int i=st;i<ed;i++) if(yy[i]<y&&yy[i]>pos) {pos=yy[i];index=i;}
if(pos==-inf) {mark=false;break;}
y=pos+1;
if(vis1[index][flag]==1) break;
vis1[index][flag]=1;
flag=2;
}
else if(flag==2){
int st=lower_bound(yyy,yyy+n,y)-yyy;
int ed=upper_bound(yyy,yyy+n,y)-yyy;
int pos=-inf,index=-1;
for(int i=st;i<ed;i++) if(xxx[i]<x&&xxx[i]>pos) {pos=xxx[i];index=i;}
if(pos==-inf) {mark=false;break;}
x=pos+1;
if(vis2[index][flag]==1) break;
vis2[index][flag]=1;
flag=3;
}
cnt++;
}
if(mark) printf("-1\n");
else printf("%d\n",cnt);
}
return 0;
}