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. c# 结构体、枚举类型及函数调用

    一.结构体 结构体:就是一个自定义的集合,里面可以放各种类型的元素,用法大体跟集合一样. 枚举类型和结构体都属于值类型. 二.枚举类型 1.枚举类型之针对字符串,对于索引,无意义2.常量的集合,这些常 ...

  2. CKPT进程工作机制

    CKPT进程工作示意图 2.CKPT进程工作机制 检查点进程被触发的条件为: a> 当发生日志组切换时: b>  用户提交了事务时(commit): c>  Redo log buf ...

  3. windows安装django

    Window 下安装 Django 如果你还未安装Python环境需要先下载Python安装包. 1.Python 下载地址:https://www.python.org/downloads/ 2.D ...

  4. 【HDU4612】 双连通分量求桥

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4612 题目大意:给你一个无向图,问你加一条边后最少还剩下多少多少割边. 解题思路:好水的一道模板题.先 ...

  5. 应该掌握的MySQL命令、MySQL语句

    一.MySQL常用的命令: 1. 连接数据库:mysql>mysql -uroot -p回车,再输入密码   mysql -h 192.168.0.200 -P 3306 -u root -p2 ...

  6. MySQL执行存储过程权限

    http://url.cn/f2bj78 MySQL grant不能在on后面写多个对象

  7. VC 菜单前的勾的切换

    if (pMenu->GetSubMenu(2)->GetMenuState(ID_STOP_SPOT_OP_MOSUE,MF_BYCOMMAND) == MF_UNCHECKED) { ...

  8. vscode 编写python如何禁止 flake8 提示 line too long

    使用vscode编写python还是挺舒服的,但是如果给vscode安装了语法校验插件,例如flake8,会常常提示一些非常苛刻的语法问题,其中最让人不能忍受的就是line to long. 一行仅能 ...

  9. Unit01-OOP-对象和类(上)

    Unit01-OOP-对象和类(上) 1.什么是类?什么是对象?  1)现实生活是由很多很多对象组成的    基于对象抽出了类  2)对象:真实存在的单个的个体    类:类型.类别,代表一类个体  ...

  10. DuiLib学习笔记2——写一个简单的程序

    我们要独立出来自己创建一个项目,在我们自己的项目上加皮肤这才是初衷.我的新建项目名为:duilibTest 在duilib根目录下面有个 Duilib入门文档.doc 我们就按这个教程开始入门 首先新 ...