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. Mysql学习总结(23)——MySQL统计函数和分组查询

    1.使用count统计条数:select count(字段名...) from tablename; 2.使用avg计算字段的平均值:select avg(字段名) from tablename: 这 ...

  2. SLF4j 和 common-logging

    http://blog.csdn.net/xydds/article/details/51606010

  3. word break相关问题的解法

    https://leetcode.com/problems/word-break/?tab=Description 以及 https://leetcode.com/problems/concatena ...

  4. 主程的晋升攻略(4):TCP、消息分包和协议设计

    在<主程的晋升攻略(3):IP.DNS和CDN>中,一次网络请求经过DNS解析知道了目的IP,如今就要发出网络包,这里我们说一说TCP的相关话题. TCP是一种流式协议 讲网络编程的教科书 ...

  5. OneNote

    OneNote导致生成OneNote Table Of Contents.onetoc2 https://answers.microsoft.com/en-us/msoffice/forum/all/ ...

  6. Nginx中的upstream 分配方法

    轮询 轮询是upstream的默认分配方式,即每个请求按照时间顺序轮流分配到不同的后端服务器,如果某个后端服务器down掉后,能自动剔除. upstream www_cc_com { server 1 ...

  7. CSS常用原子类base.css

    在写css文件时,一些常用的属性我们完全可以把它单独提出来,提高复用性,能增加开发效率,下面是一些网站推荐的常用原子类,也是零度逍遥常用的,规定了一些字体,内外边距和宽高属性,一般写在base.css ...

  8. The while statement

    Computers are often used to automate repetitive tasks. Repeating identical or similar tasks without ...

  9. POJ 3668 枚举?

    枚举两点,算一下斜率 sort一遍 判个重 输出解 25行 搞定- //By SiriusRen #include <cmath> #include <cstdio> #inc ...

  10. HBase框架基础(四)

    * HBase框架基础(四) 上一节我们介绍了如何使用HBase搞一些MapReduce小程序,其主要作用呢是可以做一些数据清洗和分析或者导入数据的工作,这一节我们来介绍如何使用HBase与其他框架进 ...