2015弱校联盟(1) -J. Right turn
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的更多相关文章
- 2015弱校联盟(2) - J. Usoperanto
J. Usoperanto Time Limit: 8000ms Memory Limit: 256000KB Usoperanto is an artificial spoken language ...
- 2015弱校联盟(1) - C. Censor
C. Censor Time Limit: 2000ms Memory Limit: 65536KB frog is now a editor to censor so-called sensitiv ...
- 2015弱校联盟(1) - B. Carries
B. Carries Time Limit: 1000ms Memory Limit: 65536KB frog has n integers a1,a2,-,an, and she wants to ...
- 2015弱校联盟(1) - I. Travel
I. Travel Time Limit: 3000ms Memory Limit: 65536KB The country frog lives in has n towns which are c ...
- 2015弱校联盟(1) -A. Easy Math
A. Easy Math Time Limit: 2000ms Memory Limit: 65536KB Given n integers a1,a2,-,an, check if the sum ...
- 2015弱校联盟(1) - E. Rectangle
E. Rectangle Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld Java class name ...
- (2016弱校联盟十一专场10.3) B.Help the Princess!
题目链接 宽搜一下就行. #include <iostream> #include<cstdio> #include<cstring> #include<qu ...
- (2016弱校联盟十一专场10.3) A.Best Matched Pair
题目链接 #include<cstdio> #include<cstring> #include<algorithm> #include<stack> ...
- 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 ...
随机推荐
- 字典树(Trie Tree)
在图示中,键标注在节点中,值标注在节点之下.每一个完整的英文单词对应一个特定的整数.Trie 可以看作是一个确定有限状态自动机,尽管边上的符号一般是隐含在分支的顺序中的.键不需要被显式地保存在节点中. ...
- 常用js或jq效果汇总
实时监控输入框改变 $('#password').bind('input propertychange', function() {}
- Linux学习笔记---用户管理---帐号管理
root管理 (1)新增用户:useradd -u 指定UID -g 指定GID -G 作为组员添加到某个组 -M 不创建主用户目录 -m 创建主用户目录 -c 用户信息说明列 -d 指定某个目录为主 ...
- 【转】NumPy-快速处理数据
2.0 简介 标准安装的Python中用列表(list)保存一组值,可以用来当作数组使用,不过由于列表的元素可以是任何对象,因此列表中所保存的是对象的指针(为了保存各种类型的对象,只能牺牲空间).这样 ...
- Docker-compose命令详解
语法: Define and run multi-container applications with Docker. Usage: docker-compose [-f=<arg> ...
- chmod() has been disabled for security reasons
最近用 codeigniter 写一个小系统,引用了session 库,codeigniter默认的session存储方式为files.鉴于安全性,文件即肯定涉及到权限问题. 在类 UNIX 操作系统 ...
- SQL Sever 2008性能分析之执行计划
一直想找一些关于SQL语句性能调试的权威参考,但是有参考未必就能够做好调试 2的工作.我深信实践中得到的经验是最珍贵的,书本知识只是一个引导.本篇来源于<Inside Microsoft SQL ...
- JVM监控和Java应用程序调试
JConsole.VisualVM监控JVM(JMX) JAVA_OPTS后加:-Dcom.sun.management.jmxremote.port=8999 -Dcom.sun.managemen ...
- C++ Primer Pluse_8_课后题
#include <iostream> #include <string> #include<cstring> using namespace std; void ...
- Django model '__week_day'与python datetime的weekday()
上周出了个bug,按星期几查询数据的时候,发现查到的数据与显示的星期几并不相符,后来发现代码中按星期几查询,有的地方用的是Django QuerySet提供的'__week_day',有的地方用的是p ...