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. Object-C,NSSet,不可变集合

    又到晚上了,继续码代码. 正在此时,老爸一个电话"海阔凭鱼跃,天高任鸟飞",老爸不在为老问题烦我了. 自由了,突然感觉压力好大啊. 将来混的太惨,可咋办啊- 第1个例子是,不可变集 ...

  2. Python 安装 httplib2

    简述 httplib2 是一个使用 Python 写的支持的非常全面的 HTTP 特性的库.需要 Python2.3 或更高版本的运行环境,0.5.0 版及其以后包含了对 Python3 的支持. 简 ...

  3. 严重: 文档无效: 找不到语法。 at (null:2:19)

    1.错误描写叙述 严重: 文档无效: 找不到语法. at (null:2:19) org.xml.sax.SAXParseException; systemId: file:/D:/MyEclipse ...

  4. Android 主界面长按创建快捷方式

    Android中创建快捷方式主要有两种方式.一是在代码中直接加入生成桌面快捷方式的代码:二是通过小部件加入; 这篇文章主要讲另外一种方法! 1.通过在AndroidManifest文件里为Activi ...

  5. VS2008 集成Lua解释器

    1. 登陆官网下载源代码 -> www.lua.org -> get started ->  installing  选择系统类型(这里是Windows的,所下面载 luaDist) ...

  6. 【C#】报表制作&lt;机房重构&gt;

    前言 和VB须要引用其它报表软件不同,VS自带报表设计的功能,初次尝试.就感受到了它的强大之处. 报表制作 话不多说.直接报表的制作过程. 1.首先,我们要先制作一个报表的容器.放到我们显示报表的窗口 ...

  7. A. Ilya and Diplomas( Codeforces Round #311 (Div. 2) )

    A. Ilya and Diplomas time limit per test 1 second memory limit per test 256 megabytes input standard ...

  8. Linux socket 编程中存在的五个隐患

    前言:         Socket API 是网络应用程序开发中实际应用的标准 API.尽管该 API 简单,但是   开发新手可能会经历一些常见的问题.本文识别一些最常见的隐患并向您显示如何避免它 ...

  9. poj_1974,最长回文字串manacher

    时间复杂度为O(n),参考:http://bbs.dlut.edu.cn/bbstcon.php?board=Competition&gid=23474 #include<iostrea ...

  10. MVC自定义错误日志异常处理

    MVC添加错误日志处理模块很简单,只要写个继承自HandleErrorAttribute的过滤器,重新OnException方法,贴个异常处理代码如下: public class ExceptionA ...