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