BNUoj 50401 Right turn 转向问题

J. 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 ( 0n103 ). 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

Submit Status


此题的题意是,在一个无穷大的图中 ,输入一个n个障碍的坐标,,没遇到障碍,就会一直笔直的走,遇到障碍就向右转向.

判断一共需要转几次方向.

转向无数次时输出-1.(一直转圈)

#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#define INF 0x3f3f3f3f

using namespace std;

struct node
{
	int x,y;
}p[10000];
int vis[10000][4];
int num=0,n;
int PAN()
{
   memset(vis,0,sizeof(vis));
   int x0=0,y0=0,dir=0,x1,y1,biao;
   while(1)
   {
   	 if(dir==0)
	 {
	 	biao=-1;
	 	x1=INF,y1=y0;
	 	for(int i=0;i<n;i++)
		{
			if(p[i].y==y1&&p[i].x>x0&&p[i].x<x1)
			{
				x1=p[i].x;
				biao=i;
			}
		}
		if(biao==-1)    //在此方向的前方没有障碍
			return num;
		if(vis[biao][dir])   //访问过了说明重复了
			return -1;
		x0=x1-1;
	 }
	 else if(dir==1)
	 {
	 	biao=-1;
	 	x1=x0,y1=-INF;
	 	for(int i=0;i<n;i++)
		{
			if(p[i].x==x1&&p[i].y<y0&&p[i].y>y1)
			{
				y1=p[i].y;
				biao=i;
			}
		}
		if(biao==-1)
			return num;
		if(vis[biao][dir])
			return -1;
		y0=y1+1;
	 }
	 else if(dir==2)
	 {
	 	biao=-1;
	 	x1=-INF,y1=y0;
	 	for(int i=0;i<n;i++)
		{
			if(p[i].y==y1&&p[i].x<x0&&p[i].x>x1)
			{
				x1=p[i].x;
				biao=i;
			}
		}
		if(biao==-1)
			return num;
		if(vis[biao][dir])
			return -1;
		x0=x1+1;
	 }
	 else if(dir==3)
	 {
	 	biao=-1;
	 	x1=x0,y1=INF;
	 	for(int i=0;i<n;i++)
		{
			if(p[i].x==x1&&p[i].y>y0&&p[i].y<y1)
			{
				y1=p[i].y;
				biao=i;
			}
		}
		if(biao==-1)
			return num;
		if(vis[biao][dir])
			return -1;
		y0=y1-1;
	 }
	 num++;
	 vis[biao][dir]=1;  //访问过的就标记
	 dir=(dir+1)%4;    //四个方向遍历
   }
}
int main()
{
	while(~scanf("%d",&n))
	{
		num=0;
		for(int i=0;i<n;i++)
		{
			scanf("%d%d",&p[i].x,&p[i].y);
		}
		printf("%d\n",PAN());

	}
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值