BNUoj 50401 Right turn 转向问题

J. Right turn

Time Limit: 1000ms
Memory Limit: 65536KB
64-bit integer IO format: %lld      Java class name: Main
Submit Status
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());

	}
}


你可能感兴趣的:(BNUoj 50401 Right turn 转向问题)