Right turn

Time Limit: 1000ms
Memory Limit: 65536KB

64-bit integer IO format: %lld      Java class name: Main

 
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
题目大意:
一个无限大的网格,其中有n个障碍,遇到障碍时只能右拐,没有障碍只能直走,问能否走出去,若能走出需要右拐几下,若不能输出-1,起点为(0,0)。
由此可见是一个dfs问题,四个单方向,当走上重复的道路时即进入死循环时无结果,剩下需要四个方向单独考虑。

所以障碍前换方向,向右拐时x=node[pos].x;y=node[pos].y-1;;向下拐时,x=node[pos].x-1;y=node[pos].y;;向左拐时,x=node[pos].x;y=node[pos].y+1;;向上拐时x=node[pos].x=1;
y=node[pos].y;
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
using namespace std;
const int inf=0x3f3f3f3f;
int dir[][]={{,},{,-},{-,},{,}};//控制方向,顺序不可以乱
int a[][];
int mark,ans,n;
struct Node
{
int x,y;
}node[];
void dfs(int cnt)
{
if(mark) return;
int dis=ans%;
for(int i=;i<;i++)
{
if(a[cnt][i]==dis)//死循环
{
mark=;
return;
}
if(a[cnt][i]==-)
{
a[cnt][i]=dis;//更新节点
break;
}
}
int k=-;
if(dir[dis][]==)
{
if(dir[dis][]==)//1,0右拐
{
int x=node[cnt].x;
int y=node[cnt].y-;
int xx=inf;
for(int i=;i<=n;i++)
{
if(node[i].x>x && node[i].x<xx && node[i].y==y)
{
xx=node[i].x;
k=i;
}
}
if(k==-)
{
mark=;
return;
}
else
{
ans++;
dfs(k);
}
}
else//-1,0左拐
{
int x=node[cnt].x;
int y=node[cnt].y+;
int xx=-inf;
for(int i=;i<=n;i++)
{
if(node[i].x<x && node[i].x>xx && node[i].y==y)
{
xx=node[i].x;
k=i;
}
}
if(k==-)
{
mark=;
return;
}
else
{
ans++;
dfs(k);
}
}
}
else
{
if(dir[dis][]==-)//0,-1下拐
{
int x=node[cnt].x-;
int y=node[cnt].y;
int yy=-inf;
for(int i=;i<=n;i++)
{
if(node[i].y<y && node[i].y>yy && node[i].x==x)
{
yy=node[i].y;
k=i;
}
}
if(k==-)
{
mark=;
return;
}
else
{
ans++;
dfs(k);
}
}
else//0,1上拐
{
int x=node[cnt].x+;
int y=node[cnt].y;
int yy=inf;
for(int i=;i<=n;i++)
{
if(node[i].y>y && node[i].y<yy && node[i].x==x)
{
yy=node[i].y;
k=i;
}
}
if(k==-)
{
mark=;
return;
}
else
{
ans++;
dfs(k);
}
}
}
return;
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
mark=;
ans=;
for(int i=;i<=n;i++)
{
scanf("%d%d",&node[i].x,&node[i].y);//储存障碍点
}
memset(a,-,sizeof(a));
node[].x=;//初始化(0,1)
node[].y=;
dfs();
if(mark==) puts("-1");
else printf("%d\n",ans);
}
}

Right turn(四川省第七届)的更多相关文章

  1. 山东省第七届ACM省赛------Triple Nim

    Triple Nim Time Limit: 2000MS Memory limit: 65536K 题目描述 Alice and Bob are always playing all kinds o ...

  2. 第七届河南省赛F.Turing equation(模拟)

    10399: F.Turing equation Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 151  Solved: 84 [Submit][St ...

  3. 山东省第七届ACM省赛------Memory Leak

    Memory Leak Time Limit: 2000MS Memory limit: 131072K 题目描述 Memory Leak is a well-known kind of bug in ...

  4. 山东省第七届ACM省赛------Reversed Words

    Reversed Words Time Limit: 2000MS Memory limit: 131072K 题目描述 Some aliens are learning English. They ...

  5. 山东省第七届ACM省赛------The Binding of Isaac

    The Binding of Isaac Time Limit: 2000MS Memory limit: 65536K 题目描述 Ok, now I will introduce this game ...

  6. 山东省第七届ACM省赛------Fibonacci

    Fibonacci Time Limit: 2000MS Memory limit: 131072K 题目描述 Fibonacci numbers are well-known as follow: ...

  7. 山东省第七届ACM省赛------Julyed

    Julyed Time Limit: 2000MS Memory limit: 65536K 题目描述 Julyed is preparing for her CET-6. She has N wor ...

  8. 第七届河南省赛10403: D.山区修路(dp)

    10403: D.山区修路 Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 69  Solved: 23 [Submit][Status][Web Bo ...

  9. 第七届河南省赛10402: C.机器人(扩展欧几里德)

    10402: C.机器人 Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 53  Solved: 19 [Submit][Status][Web Boa ...

随机推荐

  1. 收集整理的openstack java封装 api的第三方实现的选择

    Apache jclouds 地址:http://jclouds.apache.org/guides/openstack/ 一个开源库,java实现,支持cloudstack,openstack以及各 ...

  2. ffmpeg命令详解(转)

    FFmpeg是一套可以用来记录.转换数字音频.视频,并能将其转化为流的开源计算机程序.采用LGPL或GPL许可证.它提供了录制.转换以及流化音视频的完整解决方案.它包含了非常先进的音频/视频编解码库l ...

  3. poj 2337 Catenyms 【欧拉路径】

    题目链接:http://poj.org/problem?id=2337 题意:给定一些单词,假设一个单词的尾字母与还有一个的首字母同样则能够连接.问能否够每一个单词用一次,将全部单词连接,能够则输出字 ...

  4. [MST] Store Store in Local Storage

    For an optimal user and developer experience, storing state in local storage is often a must. In thi ...

  5. 去除ListView 上下边界蓝色或黄色阴影

    默认的情况下,在 ListView 滑动到顶部或者是底部的时候,会有黄色或者蓝色的阴影出现.在不同的版本号上解决办法是不同的,在 2.3 版本号之前能够在 ListView 的属性中通过设置 andr ...

  6. POJ 1012 Joseph(打表题)

    题意:约瑟夫环的变形.要求寻找到一个杀人循环节m使后半节的坏人先被所有杀光. 直接链表模拟出结果,再打表即可: 代码:(凝视的是打表码) #include<iostream> #inclu ...

  7. js 使用(不断更新...)

    1.JS 对象(Object)和字符串(String)互转 var jsObj = {}; jsObj.testArray = [1, 2, 3, 4, 5]; jsObj.name = 'CSS3' ...

  8. BZOJ 3143 高斯消元+贪心....

    思路: 先算一下每条边经过次数的期望 转化为每个点经过次数的期望 边的期望=端点的期望/度数 统计一下度数 然后高斯消元 贪心附边权--. //By SiriusRen #include <cm ...

  9. Git放弃本地更改恢复到资源库版本

    使用git版本控制工具在本地clone一份代码后,如果发现修改错误想恢复到资源库版本,下面两行可以轻松加愉快的搞定: git clean -xdf git checkout -f git的更多详细用法 ...

  10. rails 开发随手记 7

    jQuery 1.9 中如何修改选择项 $("select option:contains(teacher5)").prop('selected', 'selected'); 效果 ...