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个为反面 ...
随机推荐
- Codeforces--622A--Infinite Sequence(数学)
Infinite Sequence Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:26214 ...
- P2453 [SDOI2006]最短距离 dp
自己想出来了!这个dp比较简单,而且转移也很简单,很自然,直接上代码就行了. 题干: 一种EDIT字母编辑器,它的功能是可以通过不同的变换操作可以把一个源串X [l..m]变换为新的目标串y[1..n ...
- JSP-Runoob:JSP JavaBean
ylbtech-JSP-Runoob:JSP JavaBean 1.返回顶部 1. JSP JavaBean JavaBean是特殊的Java类,使用J ava语言书写,并且遵守JavaBean AP ...
- codevs3370 选学霸(背包dp,并查集)
3372 选学霸 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Master 题目描述 Description 老师想从N名学生中选M人当学霸,但有K对人实力相 ...
- [Swift通天遁地]九、拔剑吧-(15)搭建具有滑出、视差、3D变形等切换效果的引导页
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- 【转】vue.js三种安装方式
Vue.js(读音 /vjuː/, 类似于 view)是一个构建数据驱动的 web 界面的渐进式框架.Vue.js 的目标是通过尽可能简单的 API 实现响应的数据绑定和组合的视图组件.它不仅易于上手 ...
- Java中static方法
今天学习到了并且应用到了java中的静态方法,并且了解到它的好处与缺点. ● 生命周期(Lifecycle): 静态方法(Static Method)与静态成员变量一样,属于类本身,在类装载的时候被装 ...
- ACM_寻找第N小序列
寻找第N小序列 Time Limit: 2000/1000ms (Java/Others) Problem Description: Now our hero finds the door to th ...
- ACM_滚动AC
滚动AC Time Limit: 2000/1000ms (Java/Others) Problem Description: 小光最近拉了几个同学入ACM的坑,为鼓励A题,就增加奖励制度:每AC三道 ...
- Jquery音频播放插件下载地址(有Html、JS、CSS、音频)
有详细的html文件.全部JS代码文件.Css样式文件.测试音频资料 音频播放插件下载链接(百度云): http://pan.baidu.com/s/1pKC904F 提取码评论留邮箱发送,谢谢!