FZU 2235

中文题,题意略。

这个题点少坐标范围大,直接离散化后建图搞。

这个题目卡vector,真是一脸懵逼。。。。。。。。。。。。

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#define maxn 100050
#define F 0x3f
#define INF 0x3f3f3f3f
using namespace std;

struct Point{
    int x,y;
    Point(int a = 0,int b = 0){
        x = a,y = b;
    }
    Point operator+ (const Point& p){
        return Point(p.x + x,p.y + y);
    }
    bool operator< (const Point& p) const{
        if(p.x != x) return x < p.x;
        else return y < p.y;
    }
    bool operator == (const Point& p) const{
        return (x == p.x) && (y == p.y);
    }
};
struct Edge{
    int to,nxt;
};

Edge edge[maxn<<3];
int head[maxn];
Point p1,p2;
Point mov[] = {Point(1,0),Point(-1,0),Point(0,1),Point(0,-1),Point(1,1),Point(-1,1),Point(-1,-1),Point(1,-1)};
Point store[maxn];
int N,tail,cnt;
int dist[maxn];

int bfs(int s,int t){
    memset(dist,F,sizeof(dist));
    queue<int> que;
    que.push(s);
    dist[s] = 0;
    while(que.size()){
        //printf("i cannot out\n");
        int temp = que.front();
        que.pop();
        for(int i = head[temp];i != -1;i = edge[i].nxt){
            int v = edge[i].to;
            //printf("i == %d\n",i);
            if(dist[v] != INF) continue;
            if( dist[v] > dist[temp] + 1){
                dist[v] = dist[temp] + 1;
                que.push(v);
            }
        }
    }
    return dist[t];
}
void add_e(int from,int to){
    edge[cnt].to = to,edge[cnt].nxt = head[from],head[from] = cnt++;
}
void init(){
    memset(head,-1,sizeof(head));
    cnt = tail = 0;
}

int main(){
    while(scanf("%d%d%d%d",&p1.x,&p1.y,&p2.x,&p2.y) == 4){
        init();
        scanf("%d",&N);
        int ri,ai,bi;
        store[tail++] = p1,store[tail++] = p2;
        for(int i = 0;i < N;++i){
            scanf("%d%d%d",&ri,&ai,&bi);
            for(int j = ai;j <= bi;++j){
                store[tail++] = Point(ri,j);
            }
        }
        sort(store,store + tail);
        tail = unique(store,store + tail) - store;
        for(int i = 0;i < tail;++i){
            int id1 = i + 1;
            //printf("from (%d,%d):\n",store[i].x,store[i].y);
            for(int j = 0;j < 8;++j){
                Point temp = store[i] + mov[j];
                int id2 = lower_bound(store,store + tail,temp) - store + 1;
                if(!(store[id2 - 1] == temp)) continue;
                //printf("to (%d,%d)\n",temp.x,temp.y);
                add_e(id1,id2);
            }
        }
        int s = lower_bound(store,store + tail,p1) - store + 1;
        int t = lower_bound(store,store + tail,p2) - store + 1;
        int ans = bfs(s,t);
        printf("%d\n",ans == INF ? -1 : ans);
    }
    return 0;
}

 

posted @ 2018-03-03 10:14  温和的提比略  阅读(289)  评论(0编辑  收藏  举报