弱校联萌十一大决战之强力热身 J. Right turn (模拟)

J. Right turn
Time Limit: 1000msMemory 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 (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




解析:碰到一个障碍物就右转弯,沿着四个方向一层一层不断向外扩展,大致形状像一个折线构成的阿基米德螺线,时刻记住,在当前方向向外扩展,并且扩展的点距当前点最近就行了,详见代码




AC代码:

#include 
using namespace std;

int n;
struct Node{    //点
    int x, y, dir;
}current;
vector visited;
map > _x, _y;

bool operator == (Node a, Node b){    //定义点相等
    return a.x == b.x && a.y == b.y && a.dir == b.dir;
}

bool move(){
    int x = current.x, y = current.y;
    set::iterator it;
    if(current.dir == 0){    //x轴正方向
        it = _y[y].upper_bound(x);    //找纵坐标相等且横坐标比当前点大且距离最小的点
        if(it != _y[y].end()){
            current.x = *it - 1;
            current.dir = 1;    //右转
            return true;
        }
    }
    if(current.dir == 1){    //y轴负方向
        it = _x[x].lower_bound(y);    //找横坐标相等且纵坐标比当前点小且距离最近的点
        if(it != _x[x].begin()) it --;
        if(it != _x[x].end() && *it < current.y){
            current.y = *it + 1;
            current.dir = 2;    //右转
            return true;
        }
    }
    if(current.dir == 2){    //x轴负方向
        it = _y[y].lower_bound(x);    //找纵坐标相等且横坐标比当前点小且距离最小的点
        if(it != _y[y].begin()) it --;
        if(it != _y[y].end() && *it < current.x){
            current.x = *it + 1;
            current.dir = 3;    //右转
            return true;
        }
    }
    if(current.dir == 3){    //y轴正方向
        it = _x[x].upper_bound(y);    //找横坐标相等且纵坐标比当前点大且距离最小的点
        if(it != _x[x].end()){
            current.y = *it - 1;
            current.dir = 0;    //右转
            return true;
        }
    }
    return false;
}

bool is_circle(){    //判断是否出现环
    int len = visited.size();
    for(int i=0; i




你可能感兴趣的:(模拟,BNU)