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+;
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的更多相关文章
- 模拟+贪心 SCU 4445 Right turn
题目传送门 /* 题意:从原点出发,四个方向,碰到一个点向右转,问多少次才能走出,若不能输出-1 模拟:碰到的点横坐标相等或纵坐标相等,然而要先满足碰到点最近, 当没有转向或走到之前走过的点结束循环. ...
- SCU 4445 Right turn(dfs)题解
思路:离散化之后,直接模拟就行,标记vis开三维 代码: #include<iostream> #include<algorithm> #include<cstdio&g ...
- SCU 4445 Right turn
模拟. 每次找一下即将要遇到的那个点,这个数据范围可以暴力找,自己的写的时候二分了一下.如果步数大于$4*n$一定是$-1$. #include<bits/stdc++.h> using ...
- 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 ...
- ACM:SCU 4437 Carries - 水题
SCU 4437 Carries Time Limit:0MS Memory Limit:0KB 64bit IO Format:%lld & %llu Practice ...
- ACM: SCU 4438 Censor - KMP
SCU 4438 Censor Time Limit:0MS Memory Limit:0KB 64bit IO Format:%lld & %llu Practice D ...
- ACM: SCU 4440 Rectangle - 暴力
SCU 4440 Rectangle Time Limit:0MS Memory Limit:0KB 64bit IO Format:%lld & %llu Practic ...
- webrtc进阶-信令篇-之三:信令、stun、turn、ice
webRTC支持点对点通讯,但是webRTC仍然需要服务端: . 协调通讯过程中客户端之间需要交换元数据, 如一个客户端找到另一个客户端以及通知另一个客户端开始通讯. . 需要处理NAT(网 ...
- HDU-4869 Turn the pokers
原题: Turn the pokers 思路:假设正面为0,反面为1.牌就像这样 000000....... .考虑到假如可以实现最终反面个数为m, 牌共n张, 则这n张排任取m个为反面 ...
随机推荐
- [BZOJ 1660] Bad Hair Day
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1660 [算法] Sprease Table + 二分 时间复杂度 : O(NlogN ...
- [python基础] python生成wordcloud并保存
1.核心包 #jieba.pandas用来处理数据,数据源以xls格式存储的,这里用pandas进行处理import jieba from jieba import analyse import pa ...
- IDEA Spark Streaming 操作(套接字流)
import org.apache.spark.SparkConf import org.apache.spark.streaming.{Seconds, StreamingContext} obje ...
- hibernate基础学习---hierbnate2级缓存
1:开启二级缓存sessionFactory需要安装jar包 2:在实体类配置文件添加(配置二级缓存).我的配置文件是Account.hbm.xml <?xml version="1. ...
- Balanced Lineup(线段树)
http://poj.org/problem?id=3264 题意:n个数,q个询问,输出[l,r]中最大值与最小值的差. #include <stdio.h> #include < ...
- 爬虫—Ajax数据爬取
一.什么是Ajax 有时候我们使用浏览器查看页面正常显示的数据与使用requests抓取页面得到的数据不一致,这是因为requests获取的是原始的HTML文档,而浏览器中的页面是经过JavaScri ...
- vue---思维导图
持续更新啦啦啦啦
- JavaScript--Date 日期对象
日期对象可以储存任意一个日期,并且可以精确到毫秒数(1/1000 秒). 定义一个时间对象 : var Udate=new Date(); 注意:使用关键字new,Date()的首字母必须大写. 使 ...
- Select2插件ajax方式加载数据并刷新页面数据回显
今天在优化项目当中,有个要在下拉框中搜索数据的需求:最后选择使用selec2进行开发: 官网:http://select2.github.io/ 演示: 准备工作: 文件需要引入select2.ful ...
- Java中的异常注意点
在java中 使用throw关键字抛出异常 使用throws关键字声明异常 public static void main(String[] args) throws Exception{ ...