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);
}
}

最新文章

  1. DotNetBar 第1课,设置整体窗口样式
  2. 深入.net(数据类型)
  3. 关于JS变量提升的一些坑
  4. gspx请求周期(备忘)
  5. 随机数是骗人的,.Net、Java、C为我作证
  6. ASP.NET 如何发现问题的方法
  7. 手把手集成web端手写公式功能
  8. 【PSR规范专题(2)】PSR-1 基本代码规范
  9. 关于 未能加载文件或程序集“MySql.Web, Version=6.7.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d”或它的某一个依赖项。系统找不到指定的文件。
  10. SimpleDateFormat 的性能和线程安全性
  11. 获取前端post方式传过来的JSON格式的数据的代码
  12. Sublime Text 3 安装 Emmet 插件
  13. 201521123019 《java程序设计》 第14周学习总结
  14. MYSQL 三元 函数
  15. StrokesPlus 谷歌搜索结果转https
  16. C/C++知识补充(2) C/C++操作符/运算符的优先级 & 结合性
  17. mac电脑链接安卓手机的方法
  18. javaweb Servlet接收Android请求,并返回json数据
  19. iOS开发之--改变系统导航的颜色,字体,还有返回样式的自定义
  20. WPF 应用完全模拟 UWP 的标题栏按钮

热门文章

  1. CAD二次开发(02)-添加对象到模型空间
  2. PatentTips - Controlling voltage and frequency
  3. 洛谷 P1033 自由落体
  4. POJ 3207 Ikki&amp;#39;s Story IV - Panda&amp;#39;s Trick(2-sat)
  5. 编译并使用boost库(win7+boost1.63+vs2015+32位or 64位),超详细,boost于vs2017下编译(64/32bit)
  6. [JSOI2008] [BZOJ1567] Blue Mary的战役地图 解题报告 (hash)
  7. 基于JavaSwing的例子-非连接数据库
  8. python ftp
  9. jsLittle源码封装对象合并
  10. 1、Go base64编码