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. go语言笔记——数组长度不可变,但是元素值是可变的!!!

    数组声明的格式是: var identifier [len]type 例如: var arr1 [5]int 在内存中的结构是: 对索引项为 i 的数组元素赋值可以这么操作:arr[i] = valu ...

  2. 杂项:LDAP

    ylbtech-杂项:LDAP 1.返回顶部 1. LDAP是轻量目录访问协议,英文全称是Lightweight Directory Access Protocol,一般都简称为LDAP.它是基于X. ...

  3. PL/SQL编程基础

    1. PL/SQL块的基础结构 DECLARE /* * 定义部分——定义常量.变量.复杂数据类型.游标.用户自定义异常 */ BEGIN /* * 执行部分——PL/SQL语句和SQL语句 */ E ...

  4. SpringBoot集成Redis来实现缓存技术方案

    概述 在我们的日常项目开发过程中缓存是无处不在的,因为它可以极大的提高系统的访问速度,关于缓存的框架也种类繁多,今天主要介绍的是使用现在非常流行的NoSQL数据库(Redis)来实现我们的缓存需求. ...

  5. 简单理解jsonp原理

    对于javascript程序员来说,发送ajax请求获取后台数据然后把数据和模板拼接成字符串渲染回DOM实现无刷新更新页面这样的操作可谓是轻车熟路.但众所周知,ajax有一个不好,就是不能跨域传输数据 ...

  6. [Apple开发者帐户帮助]八、管理档案(4)

    您可以编辑,下载或删除在开发人员帐户中创建的配置文件.例如,如果您撤消了证书或禁用了配置文件中包含的设备,请编辑配置文件.或重新置备的个人资料,如果因为你它是无效的功能的应用程式服务. 注意: Xco ...

  7. 6月来了,Java还是第一!

    2019年6月了,话说现在很多小孩子都开始接触幼儿编程了,我也经常看到幼儿编程的广告,编程门槛真的是越来越低. 除此之外,也有大量其他行业的从业者想转软件开发的,编程那么广,语言那么多,那么在这么多编 ...

  8. Netty--数据通信和心跳检测

    数据通信 概述: netty的ReadTimeOut实现方案3 服务端: public class Server { public static void main(String[] args) th ...

  9. CentOS6.5下简单的MySQL数据库操作

    1.登录成功之后退出的话,直接输入quit或者exit即可.

  10. Android常用的Dialog对话框用法

    Android的版本有很多通常开发的时候对话框大多数使用自定义或是 Google提供的V4, V7 兼容包来开发保持各个版本的对话框样式统一,所以这里使用的是V7 包里的AlertDialog. im ...