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. CMD和DOS的区别

    很多人喜欢在这里把XP下的CMD和我们的DOS混淆,因此今天我特别发帖说明:CMD和DOS是完全不同的!请各位始终牢记这一点,否则在高手面前就丢人了!因为CMD是32位应用程序(而DOS是16位操作系 ...

  2. fopen的第一个参数不能有'\n'

    我刚才写了个程序, 需要操作两个文件.  我用fgets获取标准输入流stdin中的文件名, 然后用fopen打开. 结果编译器总是抱错.  调试了一下,发现fopen返回的是NULL. 我在此处加了 ...

  3. MyEclipse安装lombok

    1. 将lombok.jar复制到myeclipse.ini所在的文件夹 2. 打开myeclipse.ini,插入以下两行: -Xbootclasspath/a:lombok.jar-javaage ...

  4. 利用session完成用户登陆

    package cn.itcast.cookie; import java.io.IOException; import java.io.PrintWriter; import java.util.L ...

  5. Myeclipse8.5 subscription expired自己动手获取Myeclipse的注册码

    步骤: 1.在myeclipse中新建一个java project 2.在src目录下建立一个名为MyEclipseGen的类 3.将下面的代码复制到该类中,并运行. import java.io.* ...

  6. HTTP Error 500 - Server Error.

    Error Details: The FastCGI process exited unexpectedly Error Number: -1073741819 (0xc0000005). Error ...

  7. YII2 blockui

    https://packagist.org/packages/ayrozjlc/yii2-blockui

  8. CSS权威指南 - 基本视觉格式化 4

    改变元素显示 没有讨论与表格相关的.列表list-item的值.之后讨论. 改变显示角色 显示为块级元素 将一串链接(行内元素)改变垂直放置,若有如下一连串的链接: <div id=" ...

  9. SVN 分支管理

    平时在工作中使用 SVN 只是限于 commit,update 这样的操作,至多再 reslove 解决一下冲突,没有用过分支管理.开发过程中一般都是一个功能开发完成之后整体进行提交,而最近在项目中有 ...

  10. Excel文件读写

    C#读写Excel的方式有好几种,具体参考文章: http://www.cnblogs.com/huipengkankan/archive/2011/07/28/2120407.html 昨天大致研究 ...