SCU 4445 Right turn (模拟)

Right turn

frog is trapped in a maze. The maze is infinitely large and divided into grids. It also consists of nn obstacles, where the ii-th obstacle lies in grid (xi,yi)(xi,yi).

frog is initially in grid (0,0)(0,0), heading grid (1,0)(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 11 integer nn (0≤n≤1030≤n≤103). Each of the following nn lines contains 22 integers xi,yixi,yi. (|xi|,|yi|≤109,(xi,yi)≠(0,0)|xi|,|yi|≤109,(xi,yi)≠(0,0), all (xi,yi)(xi,yi) are distinct)

Output

For each test, write 11 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>
using namespace std;
struct point
{
	int x, y;
	int id;
}a[1005], t;
int n;
bool v[1005][4];

bool move(point &now, int dir)
{
	int mx = numeric_limits<int>::max();
	bool flag = false;
	
	if(dir == 0)
	{
		for(int i = 0; i < n; i++)
		if(now.x == a[i].x && a[i].y > now.y)
		{
			if(a[i].y - now.y < mx)
			{
				mx = a[i].y - now.y;
				t = a[i];
				t.y--;
			}
			flag = true;
		}
	}
	else if(dir == 1)
	{
		for(int i = 0; i < n; i++)
		if(now.y == a[i].y && a[i].x > now.x)
		{
			if(a[i].x - now.x < mx)
			{
				mx = a[i].x - now.x;
				t = a[i];
				t.x--;
			}
			flag = true;
		}
	}	
	else if(dir == 2)
	{
		for(int i = 0; i < n; i++)
		if(now.x == a[i].x && a[i].y < now.y)
		{
			if(now.y - a[i].y < mx)
			{
				mx = now.y - a[i].y;
				t = a[i];
				t.y++;
			}
			flag = true;
		}
	}
	else
	{
		for(int i = 0; i < n; i++)
		if(now.y == a[i].y && a[i].x < now.x)
		{
			if(now.x - a[i].x < mx)
			{
				mx = now.x - a[i].x;
				t = a[i];
				t.x++;
			}
			flag = true;
		}
	}
	
	if(flag)	now = t;
	
	return flag;
}

int main()
{
	while(cin >> n)
	{
		memset(v,0 ,sizeof v);
		
		int ans = 0;
		for(int i = 0; i < n; i++)
		{
			cin >> a[i].x >> a[i].y;
			a[i].id = i+1;
		}
		
		point my = point{0,0,0};
		
		for(int i = 1; move(my, i%4); i++)
		{
			ans ++;
			if(v[my.id][i%4])
			{
				ans = -1;
				break;
			}
			v[my.id][i%4] = true;
		}
		
		cout << ans << endl;
	}
	
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值