poj3168 Barn Expansion【计算几何 平面扫描】

Farmer John has N (1 <= N <= 25,000) rectangular barns on his farm, all with sides parallel to the X and Y axes and integer corner coordinates in the range 0..1,000,000. These barns do not overlap although they may share corners and/or sides with other barns. 

Since he has extra cows to milk this year, FJ would like to expand some of his barns. A barn has room to expand if it does not share a corner or a wall with any other barn. That is, FJ can expand a barn if all four of its walls can be pushed outward by at least some amount without bumping into another barn. If two barns meet at a corner, neither barn can expand. 

Please determine how many barns have room to expand.
Input
Line 1: A single integer, N 

Lines 2..N+1: Four space-separated integers A, B, C, and D, describing one barn. The lower-left corner of the barn is at (A,B) and the upper right corner is at (C,D).
Output
Line 1: A single integer that is the number of barns that can be expanded.
Sample Input
5
0 2 2 7
3 5 5 8
4 2 6 4
6 1 8 6
0 0 8 1
Sample Output
2
Hint
Explanation of the sample: 

There are 5 barns. The first barn has its lower-left corner at (0,2) and its upper-right corner at (2,7), and so on. 

Only two barns can be expanded --- the first two listed in the input. All other barns are each in contact with at least one other barn.

思路:

把四条边拆开 存到两个数组里

排序 y方向的先按照x排 再按照y方向上的起点排

x方向同理


遍历 对于每一个点 所有loc和他相同的 看看在不在重合范围内 并且更新范围

对于这个点本身的计数要特别一点 不然会重复



用cin cout会T 还是不长记性哦

可能真的痛经痛傻了 洗洗睡了洗洗睡了

#include <iostream>
#include <algorithm>
#include <cstring>

#include <cstdio>

#include <cmath>

using namespace std;
#define PI 3.1415926
#define EPS 1.0e-6



struct Point {
    Point(){}
    Point(double x, double y, double z):x(x), y(y), z(z){}
    double x,y, z;

};
struct rect{
    Point left_bottom;
    Point right_up;
};
struct barn{
    int index;
    int st, ed, loc;
}hh[500005], ll[500005];

bool cmp(barn a, barn b)
{
    if(a.loc == b.loc)
        return a.st < b.st;
    return a.loc < b.loc;
}




int n, ans;
rect rec[25005];
bool vis[25005];

void solve(barn *bar, int n)
{
    int i = 0;
    while(i < n){
        /*if(vis[bar[i].index]){
            i++;
            continue;
        }*/
        int pos = bar[i].loc;
        int cnt = 0;
        int first = i;
        int ed = bar[i].ed;
        i++;
        while(i < n && bar[i].loc == pos && bar[i].st <= ed){
            if(bar[i].ed > ed) ed = bar[i].ed;
            if(!vis[bar[i].index]){
                vis[bar[i].index] = true;
                ans--;
            }
            cnt++;
            i++;
        }
        if(cnt){
            if(!vis[bar[first].index]){
                vis[bar[first].index] = true;
                ans--;
            }

        }
    }
}

int main()
{
    //while(cin>>n){
    scanf("%d",&n);
        ans = n;
        //memset(vis, 0, sizeof(vis));
        for(int i = 0; i < n; i++){
            int left, bottom, right, up;
            scanf("%d%d%d%d",&left, &bottom, &right, &up);
            hh[2 * i].index = hh[2 * i + 1].index = i;
            ll[2 * i].index = ll[2 * i + 1].index = i;
            hh[2 * i].st = hh[2 * i + 1].st = bottom;
            hh[2 * i].ed = hh[2 * i + 1].ed = up;
            hh[2 * i].loc = left;hh[2 * i + 1].loc = right;
            ll[2 * i].st = ll[2 * i + 1].st = left;
            ll[2 * i].ed = ll[2 * i + 1].ed = right;
            ll[2 * i].loc = bottom; ll[2 * i + 1].loc = up;
        }

        sort(hh, hh + 2 * n, cmp);
        sort(ll, ll + 2 * n, cmp);
        solve(hh, 2 * n);
        solve(ll, 2 * n);
        //int ans = 0;
        //for(int i = 0; i < n; i++){
        //    if(!vis[i])ans++;
        //}
        printf("%d\n", ans);
    //}
    return 0;

}


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wybooooooooo/article/details/79966374
个人分类: poj 计算几何
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭