SCU-4445 Right turn(模拟)

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 ( 0n10 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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值