POJ 3168 Barn Expansion(几何啊 )

来源:互联网 发布:米维斯s和卡马d1c知乎 编辑:程序博客网 时间:2024/05/17 22:19

题目链接:http://poj.org/problem?id=3168


Description

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

50 2 2 73 5 5 84 2 6 46 1 8 60 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.

Source

USACO 2005 December Gold

题意:

给若干个矩形,他们之间只有接触,没有重叠,求有多少个矩形没有和其他矩形相接触。

PS:

分别按照横坐标和纵坐标排序!判断相邻!

代码如下:

#include <cstdio>#include <iostream>#include <cstring>#include <algorithm>using namespace std;const int maxn = 25047;struct NODE{    int x1, y1;    int x2, y2;} d[maxn];struct node{    int f;    int down, up, id;} a[2*maxn];int vis[maxn];int n;bool cmp(node a, node b){    if(a.f == b.f)        return a.down < b.down;    return a.f < b.f;}void solve_x(){    for(int i = 1; i <= n; i++)    {        a[i*2-1].f = d[i].x1;        a[i*2-1].down = d[i].y1;        a[i*2-1].up = d[i].y2;        a[i*2].f = d[i].x2;        a[i*2].down = d[i].y1;        a[i*2].up = d[i].y2;        a[i*2-1].id = a[i*2].id = i;    }    sort(a+1,a+n+n+1,cmp);    for(int i = 1, j; i <= 2*n; i = j)    {        for(j = i; j <= 2*n; j++)        {            if(a[i].f != a[j].f)                break;        }        int up = a[i].up;        for(int k = i+1; k < j; k++)        {            if(a[k].down <= up)            {                vis[a[k].id] = vis[a[k-1].id] = 1;            }            if(up < a[k].up)            {                up = a[k].up;            }        }    }}void solve_y(){    for(int i = 1; i <= n; i++)    {        a[i*2-1].f = d[i].y1;        a[i*2-1].down = d[i].x1;        a[i*2-1].up = d[i].x2;        a[i*2].f = d[i].y2;        a[i*2].down = d[i].x1;        a[i*2].up = d[i].x2;        a[i*2-1].id = a[i*2].id = i;    }    sort(a+1,a+n+n+1,cmp);    for(int i = 1, j; i <= 2*n; i = j)    {        for(j = i; j <= 2*n; j++)        {            if(a[i].f != a[j].f)                break;        }        int up = a[i].up;        for(int k = i+1; k < j; k++)        {            if(a[k].down <= up)            {                vis[a[k].id] = vis[a[k-1].id] = 1;            }            if(up < a[k].up)            {                up = a[k].up;            }        }    }}int main(){    int t;    while(~scanf("%d",&n))    {        for(int i = 1; i <= n; i++)        {            scanf("%d%d",&d[i].x1,&d[i].y1);            scanf("%d%d",&d[i].x2,&d[i].y2);        }        memset(vis,0,sizeof(vis));        solve_x();        solve_y();        int ans = 0;        for(int i = 1; i <= n; i++)        {            if(!vis[i])            {                ans++;            }        }        printf("%d\n",ans);    }    return 0;}


1 0
原创粉丝点击