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+;
const int N=1e3+;
using namespace std;
inline int id(int l,int r){return l+r|l!=r;}
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}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>*n)
{
flag=false;
return;
}
x=*it-;
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>*n)
{
flag=false;
return;
}
y=*it+;
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>*n)
{
flag=false;
return;
}
x=*it+;
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>*n)
{
flag=false;
return;
}
y=*it-;
turn_r(x,y);
}
else return;
}
int main()
{
int i,j;
while(~scanf("%d",&n))
{
pq1.clear();
pq2.clear();
flag=true;
cnt=;
rep(i,,n)
{
int x,y;
scanf("%d%d",&x,&y);
pq1[x].insert(y);
pq2[y].insert(x);
}
turn_r(,);
if(!flag)puts("-1");
else printf("%d\n",cnt);
}
return ;
}

SCU Right turn的更多相关文章

  1. 模拟+贪心 SCU 4445 Right turn

    题目传送门 /* 题意:从原点出发,四个方向,碰到一个点向右转,问多少次才能走出,若不能输出-1 模拟:碰到的点横坐标相等或纵坐标相等,然而要先满足碰到点最近, 当没有转向或走到之前走过的点结束循环. ...

  2. SCU 4445 Right turn(dfs)题解

    思路:离散化之后,直接模拟就行,标记vis开三维 代码: #include<iostream> #include<algorithm> #include<cstdio&g ...

  3. SCU 4445 Right turn

    模拟. 每次找一下即将要遇到的那个点,这个数据范围可以暴力找,自己的写的时候二分了一下.如果步数大于$4*n$一定是$-1$. #include<bits/stdc++.h> using ...

  4. Lesson 11 One good turn deserves another

    Text I was having dinner at a restaurant when Tony Steele came in. Tony worked in a layer's office y ...

  5. ACM:SCU 4437 Carries - 水题

    SCU 4437  Carries Time Limit:0MS     Memory Limit:0KB     64bit IO Format:%lld & %llu  Practice  ...

  6. ACM: SCU 4438 Censor - KMP

     SCU 4438 Censor Time Limit:0MS     Memory Limit:0KB     64bit IO Format:%lld & %llu  Practice D ...

  7. ACM: SCU 4440 Rectangle - 暴力

     SCU 4440 Rectangle Time Limit:0MS     Memory Limit:0KB     64bit IO Format:%lld & %llu  Practic ...

  8. webrtc进阶-信令篇-之三:信令、stun、turn、ice

    webRTC支持点对点通讯,但是webRTC仍然需要服务端:  . 协调通讯过程中客户端之间需要交换元数据,    如一个客户端找到另一个客户端以及通知另一个客户端开始通讯.  . 需要处理NAT(网 ...

  9. HDU-4869 Turn the pokers

    原题:  Turn the pokers       思路:假设正面为0,反面为1.牌就像这样 000000....... .考虑到假如可以实现最终反面个数为m, 牌共n张, 则这n张排任取m个为反面 ...

随机推荐

  1. js获取request参数值(javascript 获取request参数值的方法)

    jsp 中的js,可以用el表达式来提取:var value = "${requestScope.XXX}"; 注:XXX为你的参数名 如:http://localhost:808 ...

  2. JSP 向 JavaScript 中传递数组

    采用隐藏标签的方式: // JSP: <%               while(rs.next())       {              %>            <in ...

  3. 12 C#中的方法

    还记得我们的第一个程序吗?忘记了?那你要努力了.我们的第一个程序是就是往dos窗口输出一些字符串.在哪个程序中只有一个方法,Main方法.Main方法是一个特殊的方法,但是它也是一个方法.为什么说Ma ...

  4. c#异步多线程

    1.asyncrel = delegate.BeginInvoke实现委托异步调用. 2.异步等待 asyncrel.IsCompleted用于判断是否执行完毕 or EndInvoke用于等待执行完 ...

  5. 简单的KKL诊断线~~~自己在家都可以制作obd诊断接口了 ~~

    简单的KKL诊断线~~~自己在家都可以制作~~ 适合bmw 07年以前的车型,因为新的车型使用D-can作为诊断接口,所以不能再使用kkl诊断接口不过SB开头的宝马3系还是可以使用的 更多内容欢迎查看 ...

  6. 浅谈Java中的hashcode方法以及equals方法

    哈希表这个数据结构想必大多数人都不陌生,而且在很多地方都会利用到hash表来提高查找效率.在Java的Object类中有一个方法: public native int hashCode(); 根据这个 ...

  7. drupal 8——图片组(list)在前台的显示顺序在登录状态和非登录状态不同

    问题描述:该页面是通过view来输出的,然而,登录状态下其页面中的图片组输出顺序是乱序的,而非登录状态下则根据id值升序输出. 原因:在原view配置页面中,没有配置默认的排序字段 解决方案:在vie ...

  8. html5——多媒体(四)

    全屏兼容 box.requestFullscreen(); box.webkitRequestFullScreen(); box.mozRequestFullScreen(); <!DOCTYP ...

  9. python 分割文件、组合文件

    import glob big_file = open('index.sql', 'rb') bak_file = 'index_bak' i = 1 while True: chunk = big_ ...

  10. java攻城狮之路--复习JDBC(数据库连接池 : C3P0、DBCP)

    复习数据库连接池 : C3P0.DBCP 1.数据库连接池技术的优点: •资源重用:      由于数据库连接得以重用,避免了频繁创建,释放连接引起的大量性能开销.在减少系统消耗的基础上,另一方面也增 ...