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

5 篇文章 0 订阅

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 <bits/stdc++.h>
using namespace std;

int n;
struct Node{    //点
    int x, y, dir;
}current;
vector<Node> visited;
map<int, set<int> > _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<int>::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<len; i++)
        if(visited[i] == current) return true;
    return false;
}

int main(){
    #ifdef sxk
        freopen("in.txt", "r", stdin);
    #endif // sxk

    int x, y;
    while(scanf("%d", &n) == 1){
        _x.clear();
        _y.clear();
        visited.clear();
        for(int i=0; i<n; i++){
            scanf("%d%d", &x, &y);
            _x[x].insert(y);
            _y[y].insert(x);
        }
        current.x = current.y = current.dir = 0;
        for(int i=0; ; i++){
            visited.push_back(current);
            if(!move()){
                printf("%d\n", i);
                break;
            }
            if(is_circle()){
                puts("-1");
                break;
            }
        }
    }
    return 0;
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值