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. HDU 4069 数独

    好久没做题了,建图搞了好久…… 然后,判是否有多解的时候会把原来的答案覆盖掉…… 这里没注意,弄了一下午…… 代码: #include <iostream> #include <cs ...

  2. grep的使用【转】

    grep的作用是显示匹配一个或多个模式的文本行.时常会作为管道(|)的第一步,以便对匹配的数据作进一步处理.grep常用于查找和替换文本的.在传统上,grep有3个版本:grep.egrep(扩展gr ...

  3. 埃及分数 迭代加深搜索 IDA*

    迭代加深搜索 IDA* 首先枚举当前选择的分数个数上限maxd,进行迭代加深 之后进行估价,假设当前分数之和为a,目标分数为b,当前考虑分数为1/c,那么如果1/c×(maxd - d)< a ...

  4. Mysql导入Sql文件时报Error Code: 2013 - Lost connection to MySQL server during query

    MySql 有时我们导入sql文件,文件过大,导致Error Code: 2013 - Lost connection to MySQL server during query这种错误 执行以下: S ...

  5. maven跳过单元测试-maven.test.skip和skipTests的区别以及部分常用命令

    -DskipTests,不执行测试用例,但编译测试用例类生成相应的class文件至target/test-classes下. -Dmaven.test.skip=true,不执行测试用例,也不编译测试 ...

  6. 使用JMX透过防火墙远程监控tomcat服务

    https://my.oschina.net/mye/blog/64879 http://blog.csdn.net/l1028386804/article/details/51547408 http ...

  7. C++根据扩展名获取文件图标、类型

    简述 在Windows系统中,根据扩展名来区分文件类型,比如:.txt(文本文件)..exe(可执行程序).*.zip(压缩文件),下面,我们来根据扩展名来获取对应的文件图标.类型. 简述 源码 源码 ...

  8. [SDOI2010] 古代猪文 (快速幂+中国剩余定理+欧拉定理+卢卡斯定理) 解题报告

    题目链接:https://www.luogu.org/problemnew/show/P2480 题目背景 “在那山的那边海的那边有一群小肥猪.他们活泼又聪明,他们调皮又灵敏.他们自由自在生活在那绿色 ...

  9. 8.变量内存CPU原理

    编译器先明确是什么类型,然后明确变量名,变量表管理所有的变量,不在变量表内部的变量不能引用.每个变量对应一整块内存 a+1所计算出来的值在寄存器中,只有变量可以被赋值,变量必须在内存里面 c语言内嵌汇 ...

  10. BZOJ 2115 DFS+高斯消元

    思路: 先搞出来所有的环的抑或值 随便求一条1~n的路径异或和 gauss消元找异或和最大 贪心取max即可 //By SiriusRen #include <cstdio> #inclu ...