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个为反面 ...
随机推荐
- VS2010 使用TeeChart绘图控件 - 之一 - 控件和类的导入
vs2010的用法和vc6有很大的不同,特别是在一些函数调用那里,当然.控件导入也是很不一样的 安装好控件后就可以在工程里加入teechart控件了 加入方法有如下几种: 1.添加Teechart控件 ...
- uploadify3.2.1版插件在ASP.NET中的使用
0.先去官网下载插件 下载uploadify3.2.1插件 解压后只需要一下文件: (1) jQuery.uploadify.min.js (2) uploadify.css (3) uploadif ...
- ASP.NET SignalR Hubs API Guide - JavaScript Client
https://docs.microsoft.com/en-us/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-javascript- ...
- iOS通讯录(纯纯的干货)
一.iOS8.0 1.访问用户通讯录的两个框架 (1)AddressBookUI.framework 提供了联系人列表界面.联系人详情界面.添加联系人界面等,一般用于选择联系人 (2)AddressB ...
- php打包文件夹成zip文件
function addFileToZip($path,$zip){ $handler=opendir($path); //打开当前文件夹由$path指定. while(($filenam ...
- Akka源码分析-local-DeathWatch
生命周期监控,也就是死亡监控,是akka编程中常用的机制.比如我们有了某个actor的ActorRef之后,希望在该actor死亡之后收到响应的消息,此时我们就可以使用watch函数达到这一目的. c ...
- firefox 附加组件栏安装
firefox 在升级到 30的版本后,发现附加组件栏不兼容了. 搜索组件,add-on bar 会得到一个 new add-on bar的组件,安装完后发现上面不显示ip, 后来才发现,应该安装Th ...
- P1538 迎春舞会之数字舞蹈
题目背景 HNSDFZ的同学们为了庆祝春节,准备排练一场舞会. 题目描述 在越来越讲究合作的时代,人们注意的更多的不是个人物的舞姿,而是集体的排列. 为了配合每年的倒计时,同学们决定排出——“数字舞蹈 ...
- 使用less时的calc()函数问题
在使用less时写 width:calc(100%-30px); 但在浏览器检查元素的时候总会显示width:70%; 可以在Less中把calc的写法改写成下面这样: width : calc(~& ...
- Leetcode0523--Continuous Subarray Sum 连续和倍数
[转载请注明]https://www.cnblogs.com/igoslly/p/9341666.html class Solution { public: bool checkSubarraySum ...