SCU Right turn

Right turn

frog is trapped in a maze. The maze is infinitely large and divided into grids. It also consists of n

obstacles, where the i-th obstacle lies in grid (xi,yi)

.

frog is initially in grid (0,0)

, heading grid (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

integer n (0n103). Each of the following n lines contains 2 integers xi,yi. (|xi|,|yi|109,(xi,yi)(0,0), all (xi,yi)

are distinct)

Output

For each test, write 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
分析:用STL模拟比较好写,另外注意判断-1的转向次数;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <bitset>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define sys system("pause")
const int maxn=1e5+10;
const int N=1e3+10;
using namespace std;
inline int id(int l,int r){return l+r|l!=r;}
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
int n,m,k,t,cnt;
map<int,set<int> >pq1,pq2;
set<int>::iterator it;
bool flag;
void turn_r(int x,int y);
void turn_d(int x,int y);
void turn_l(int x,int y);
void turn_u(int x,int y);
void turn_r(int x,int y)
{
    it=pq2[y].lower_bound(x);
    if(it!=pq2[y].end())
    {
        if(++cnt>2*n)
        {
            flag=false;
            return;
        }
        x=*it-1;
        turn_d(x,y);
    }
    else return;
}
void turn_d(int x,int y)
{
    it=pq1[x].lower_bound(y);
    if(it!=pq1[x].begin())
    {
        --it;
        if(++cnt>2*n)
        {
            flag=false;
            return;
        }
        y=*it+1;
        turn_l(x,y);
    }
    else return;
}
void turn_l(int x,int y)
{
    it=pq2[y].lower_bound(x);
    if(it!=pq2[y].begin())
    {
        --it;
        if(++cnt>2*n)
        {
            flag=false;
            return;
        }
        x=*it+1;
        turn_u(x,y);
    }
    else return;
}
void turn_u(int x,int y)
{
    it=pq1[x].lower_bound(y);
    if(it!=pq1[x].end())
    {
        if(++cnt>2*n)
        {
            flag=false;
            return;
        }
        y=*it-1;
        turn_r(x,y);
    }
    else return;
}
int main()
{
    int i,j;
    while(~scanf("%d",&n))
    {
        pq1.clear();
        pq2.clear();
        flag=true;
        cnt=0;
        rep(i,1,n)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            pq1[x].insert(y);
            pq2[y].insert(x);
        }
        turn_r(0,0);
        if(!flag)puts("-1");
        else printf("%d\n",cnt);
    }
    return 0;
}

转载于:https://www.cnblogs.com/dyzll/p/6790810.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值