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 (0≤n≤103). 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;
}
时间: 2024-10-19 02:22:38

SCU Right turn的相关文章

SCU 4445 Right turn(dfs)题解

思路:离散化之后,直接模拟就行,标记vis开三维 代码: #include<iostream> #include<algorithm> #include<cstdio> #include<stdio.h> #include<string.h> #include<queue> #include<cmath> #include<map> #include<set> #include<vector&

SCU 4445 Right turn

模拟. 每次找一下即将要遇到的那个点,这个数据范围可以暴力找,自己的写的时候二分了一下.如果步数大于$4*n$一定是$-1$. #include<bits/stdc++.h> using namespace std; const int INF = 0x7FFFFFFF; const int mod = 1e9 + 7; const int N = 5e6 + 10; const int M = 1e4 + 1; const double eps = 1e-10; int T,n,m; str

scu oj 4445 Right turn 2015年四川省赛J题(模拟题)

一般的模拟题.对于出现过的每一个x建立一个set 集合,对于y也如此.然后查找要走到哪个点即可,主要要对状态记录,判断是否无线循环,否者出现无线循环的情况,就tle了. #include<stdio.h> #include<string.h> #include<iostream> #include<string> #include<queue> #include<cmath> #include<map> #include&

HDU 4869 Turn the pokers(推理)

HDU 4869 Turn the pokers 题目链接 题意:给定n个翻转扑克方式,每次方式相应能够选择当中xi张进行翻转.一共同拥有m张牌.问最后翻转之后的情况数 思路:对于每一些翻转,假设能确定终于正面向上张数的情况,那么全部的情况就是全部情况的C(m, 张数)之和.那么这个张数进行推理会发现,事实上会有一个上下界,每隔2个位置的数字就是能够的方案,由于在翻牌的时候,相应的肯定会有牌被翻转,而假设向上牌少翻一张,向下牌就要多翻一张.奇偶性是不变的,因此仅仅要每次输入张数,维护上下界,最后

HDU 2438 Turn the corner (计算几何 + 三分)

Turn the corner Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2059    Accepted Submission(s): 785 Problem Description Mr. West bought a new car! So he is travelling around the city. One day h

The Free Lunch Is Over: A Fundamental Turn Toward Concurrency in Software

The Free Lunch Is Over A Fundamental Turn Toward Concurrency in Software By Herb Sutter The biggest sea change in software development since the OO revolution is knocking at the door, and its name is Concurrency. This article appeared in Dr. Dobb's J

hdu 2483 Turn the corner(三分)

Turn the corner Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1899    Accepted Submission(s): 719 Problem Description Mr. West bought a new car! So he is travelling around the city. One day h

hdu 4869 Turn the pokers(递推&amp;组合数学&amp;逆元)

Turn the pokers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1279    Accepted Submission(s): 466 Problem Description During summer vacation,Alice stay at home for a long time, with nothing t

HDOJ 4869 Turn the pokers

最后的结果中正面向上的奇偶性是一定的,计算出正面向上的范围low,up 结果即为 C(m,low)+ C(m,low+2) +.... + C(m,up) ,用逆元取模 Turn the pokers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 828    Accepted Submission(s): 302 Problem D