J. Right turn

Time Limit: 1000ms

Memory Limit: 65536KB

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

暴力搜索,对于每个点最多有四次访问,就会有循环

#include <bits/stdc++.h>
#define LL long long
#define fread() freopen("in.in","r",stdin)
#define fwrite() freopen("out.out","w",stdout) using namespace std; const int INF = 0x3f3f3f3f; typedef struct node
{
int x;
int y;
bool Dir[4];
} Node; typedef struct Dirc//记录所在点的位置及其方向
{
int x;
int y;
int D;
} DI; Node Pn[1010]; int n; void init()
{
for(int i=1; i<=n; i++)
{
scanf("%d %d",&Pn[i].x,&Pn[i].y);
memset(Pn[i].Dir,false,sizeof(Pn[i].Dir));
}
} int bfs()
{
queue<DI>Q;
DI a,b;
a.x=0;
a.y=0;
a.D=0;
Q.push(a);
int num=0;
while(!Q.empty())
{
b=Q.front();
Q.pop(); if(b.D==0||b.D==3)//(0:x轴正向,1:y轴负向 2:x轴负向 3:y轴正向)
{
int DD=INF;
int flag;
for(int i=1; i<=n; i++)
{
if(b.D==0)
{
if(Pn[i].y==b.y&&Pn[i].x>b.x)
{
if(DD>Pn[i].x)
{
DD=Pn[i].x;
flag=i;
}
}
}
else
{
if(Pn[i].x==b.x&&Pn[i].y>b.y)
{
if(DD>Pn[i].y)
{
DD=Pn[i].y;
flag=i;
}
}
}
}
if(DD==INF)
{
return num;
}
if(Pn[flag].Dir[b.D])
{
return -1;
}
else
{
Pn[flag].Dir[b.D]=true;
a.D=(b.D+1)%4;
if(b.D==0)
{
a.x=DD-1;
a.y=b.y;
}
else
{
a.x=b.x;
a.y=DD-1;
}
Q.push(a);
num++;
}
}
else if(b.D==2||b.D==1)
{
int DD = -INF;
int flag;
for(int i=1; i<=n; i++)
{
if(b.D==2)
{
if(Pn[i].y==b.y&&Pn[i].x<b.x)
{
if(DD<Pn[i].x)
{
DD=Pn[i].x;
flag=i;
}
}
}
else
{
if(Pn[i].x==b.x&&Pn[i].y<b.y)
{
if(DD<Pn[i].y)
{
DD=Pn[i].y;
flag=i;
}
}
}
}
if(DD==-INF)
{
return num;
}
if(Pn[flag].Dir[b.D])
{
return -1;
}
else
{
Pn[flag].Dir[b.D]=true;
a.D=(b.D+1)%4;
if(b.D==2)
{
a.x=DD+1;
a.y=b.y;
}
else
{
a.x=b.x;
a.y=DD+1;
}
Q.push(a);
num++;
}
}
}
return -1;
} int main()
{ while(~scanf("%d",&n))
{
init();
printf("%d\n",bfs());
} return 0;
}

2015弱校联盟(1) -J. Right turn的更多相关文章

  1. 2015弱校联盟(2) - J. Usoperanto

    J. Usoperanto Time Limit: 8000ms Memory Limit: 256000KB Usoperanto is an artificial spoken language ...

  2. 2015弱校联盟(1) - C. Censor

    C. Censor Time Limit: 2000ms Memory Limit: 65536KB frog is now a editor to censor so-called sensitiv ...

  3. 2015弱校联盟(1) - B. Carries

    B. Carries Time Limit: 1000ms Memory Limit: 65536KB frog has n integers a1,a2,-,an, and she wants to ...

  4. 2015弱校联盟(1) - I. Travel

    I. Travel Time Limit: 3000ms Memory Limit: 65536KB The country frog lives in has n towns which are c ...

  5. 2015弱校联盟(1) -A. Easy Math

    A. Easy Math Time Limit: 2000ms Memory Limit: 65536KB Given n integers a1,a2,-,an, check if the sum ...

  6. 2015弱校联盟(1) - E. Rectangle

    E. Rectangle Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld Java class name ...

  7. (2016弱校联盟十一专场10.3) B.Help the Princess!

    题目链接 宽搜一下就行. #include <iostream> #include<cstdio> #include<cstring> #include<qu ...

  8. (2016弱校联盟十一专场10.3) A.Best Matched Pair

    题目链接 #include<cstdio> #include<cstring> #include<algorithm> #include<stack> ...

  9. 2016弱校联盟十一专场10.5---As Easy As Possible(倍增)

    题目链接 https://acm.bnu.edu.cn/v3/contest_show.php?cid=8506#problem/A problem description As we know, t ...

随机推荐

  1. 【HDU4630 No Pain No Game】 dp思想+线段树的离线操作

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4630 题意:给你n个数据范围在[1,n]中的数,m个操作,每个操作一个询问[L,R],让你求区间[L, ...

  2. 一個相當簡潔的名字—Dyson Humidifier

    現在不止是白領級人物開始網購使用加濕器對皮膚的一個乾燥祈禱防護作用.目前新研究的一款家用的加濕器,戴森首款推出. 作為戴森的首款加濕器產品,該公司賦予了它一個相當簡潔的名字——Dyson Humidi ...

  3. 《JAVA开发环境的熟悉》实验报告——20145337

  4. MySQL数据库初用(5.6版本)第一课

    参考:http://wenku.baidu.com/link?url=NlX55fDDQ02wESO1HNkxpvju2xATwe9Fym0MfojWddXbYaJcjEKKRF9z9EX4b7shV ...

  5. 让dwz 在td里显示图片

    让dwz 在td里显示图片 <!@{foreach from = $list item = element}@> <tr target="gid" rel=&qu ...

  6. java之浮点数(笔记)

    1.在计算机中,浮点数并不同等于小数. public static void main(String[] args) { double b1 = 0.1; double b2 = 0.2; doubl ...

  7. html5:地理信息 LBS基于地理的服务和百度地图API的使用

    地理位置请求 单次定位请求getCurrentPosition(请求成功函数,请求失败函数,数据收集方式) 多次定位请求watchPosition(请求成功函数,请求失败函数,数据收集方式) 关闭更新 ...

  8. Yii源码阅读笔记(二十八)

    Yii/web中的Controller类,实现参数绑定,启动csrf验证功能,重定向页面功能: namespace yii\web; use Yii; use yii\base\InlineActio ...

  9. 集合中list、ArrayList、LinkedList、Vector的区别、Collection接口的共性方法以及数据结构的总结

    List (链表|线性表) 特点: 接口,可存放重复元素,元素存取是有序的,允许在指定位置插入元素,并通过索引来访问元素 1.创建一个用指定可视行数初始化的新滚动列表.默认情况下,不允许进行多项选择. ...

  10. ultraedit正则使用

    下面是从UltraEdit文档中摘录的语法说明: 正则表达式 (UltraEdit 语法): 符号 功能 % 匹配行首 – 表示搜索字符串必须在行首,但不包括任何选定的结果字符中的行终止字符. $ 匹 ...