Codeforces Gym 100341I Hungry Queen 2 Set水题

题目大意:

就是给出平面中n个点, 然后给出行走路径问最多走多少步不会出现不能到达以及碰撞的情况


大致思路:

首先如果只考虑上下左右走的话很明显用Set来存坐标然后用upper_bound查询两个点之间是否有被拦截即可

那么斜着走的话, 只需要将坐标轴旋转, 例如逆时针旋转45度于是(x, y)在新坐标系中映射的位置是(x - y, x + y)(放大了sqrt(2)倍

于是就和上下左右一样了


代码如下:

Result  :  Accepted     Memory  :  13576 KB     Time  :  590 ms

#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <set>
using namespace std;

#define mp make_pair
#define x first
#define y second

#define maxn 100010

int n;
pair<int, int > p[maxn];
set<pair<int, int> > Sx;
set<pair<int, int> > Sy;
set<pair<int, int> > Gx;
set<pair<int, int> > Gy;
set<pair<int, int> > :: iterator it;

pair<int, int> change(pair<int, int> pp)
{
    return mp(pp.x - pp.y, pp.x + pp.y);
}

pair<int, int> cure(pair<int, int> pp)
{
    return mp((pp.x + pp.y) / 2, (pp.y - pp.x) / 2);
}

bool solve(pair<int, int> now, pair<int, int> nex)
{
    if(now.x == nex.x)//up or down
    {
        if(now.y < nex.y)//go up
        {
            it = Sx.upper_bound(mp(now.x, now.y));
            if(*it == nex) return 1;
            else return 0;
        }
        else//go down
        {
            it = Sx.upper_bound(mp(nex.x, nex.y));
            if(it != Sx.end())
            {
                pair<int, int> f = *it;
                if(f.x == nex.x && f.y > nex.y && f.y < now.y) return 0;
            }
            return 1;
        }

    }
    if(now.y == nex.y)//left or right
    {
        if(now.x < nex.x)//go right
        {
            it = Sy.upper_bound(mp(now.y, now.x));
            if(*it == mp(nex.y, nex.x)) return 1;
            else return 0;
        }
        else//go left
        {
            it = Sy.upper_bound(mp(nex.y, nex.x));
            if(it != Sy.end())
            {
                pair<int, int> f = *it;
                if(f.x == now.y && f.y > nex.x && f.y < now.x) return 0;
            }
            return 1;
        }
    }
    if(abs(now.x - nex.x) == abs(now.y - nex.y))//go diagonal
    {
        pair<int, int> cnow = change(now);
        pair<int, int> cnex = change(nex);
        if(now.x - nex.x < 0 && now.y - nex.y < 0)//go change up
        {
            it = Gx.upper_bound(mp(cnow.x, cnow.y));
            if(*it == cnex) return 1;
            else return 0;
        }
        if(now.x - nex.x > 0 && now.y - nex.y > 0)//go change down
        {
            it = Gx.upper_bound(mp(cnex.x, cnex.y));
            if(it != Gx.end())
            {
                pair<int, int> f = *it;
                if(f.x == cnex.x && f.y > cnex.y && f.y < cnow.y) return 0;
            }
            return 1;
        }
        if(now.x - nex.x > 0 && now.y - nex.y < 0)//go change left
        {
            it = Gy.upper_bound(mp(cnex.y, cnex.x));
            if(it != Gy.end())
            {
                pair<int, int> f = *it;
                if(f.x == cnow.y && f.y > cnex.x && f.y < cnow.x) return 0;
            }
            return 1;
        }
        if(now.x - nex.x < 0 && now.y - nex.y > 0)//go change right
        {
            it = Gy.upper_bound(mp(cnow.y, cnow.x));
            if(*it == mp(cnex.y, cnex.x)) return 1;
            else return 0;
        }
    }
    return 0;
}

int main()
{
    freopen("queen2.in", "r", stdin);
    freopen("queen2.out", "w", stdout);
    scanf("%d", &n);
    Sx.clear();
    Sy.clear();
    Gx.clear();
    Gy.clear();
    for(int i = 1; i <= n; i++)
    {
        int u, v;
        scanf("%d %d", &u, &v);
        p[i] = make_pair(u, v);
        Sx.insert(mp(p[i].x, p[i].y));
        Sy.insert(mp(p[i].y, p[i].x));
        pair<int, int> pp = change(p[i]);
        Gx.insert(pp);
        Gy.insert(mp(pp.y, pp.x));
    }
    pair<int, int> now = mp(0, 0);
    int ans = 0;
    for(int i = 1; i <= n; i++)
    {
        if(solve(now, p[i]))
        {
            now = p[i];
            Sx.erase(mp(p[i].x, p[i].y)), Sy.erase(mp(p[i].y, p[i].x));
            pair<int, int> pp = change(p[i]);
            Gx.erase(pp);
            Gy.erase(mp(pp.y, pp.x));
            ans = i;
        }
        else break;
    }
    printf("%d\n", ans);
    return 0;
}
/*
4
0 2
1 1
1 -3
1 0

5
0 2
2 0
1 1
5 5
6 6
*/


你可能感兴趣的:(codeforces,2,Queen,Hungry,Gym,100341I)