USACO Section 3.1 Shaping Regions

Shaping Regions

N opaque rectangles (1 <= N <= 1000) of various colors are placed on a white sheet of paper whose size is A wide by B long. The rectangles are put with their sides parallel to the sheet's borders. All rectangles fall within the borders of the sheet so that different figures of different colors will be seen.

The coordinate system has its origin (0,0) at the sheet's lower left corner with axes parallel to the sheet's borders.

PROGRAM NAME: rect1

INPUT FORMAT

The order of the input lines dictates the order of laying down the rectangles. The first input line is a rectangle "on the bottom".

Line 1: A, B, and N, space separated (1 <= A,B <= 10,000)
Lines 2-N+1: Five integers: llx, lly, urx, ury, color: the lower left coordinates and upper right coordinates of the rectangle whose color is `color' (1 <= color <= 2500) to be placed on the white sheet. The color 1 is the same color of white as the sheet upon which the rectangles are placed.

SAMPLE INPUT (file rect1.in)

20 20 3
2 2 18 18 2
0 8 19 19 3
8 0 10 19 4

INPUT EXPLANATION

Note that the rectangle delineated by 0,0 and 2,2 is two units wide and two high. Here's a schematic diagram of the input:

11111111111111111111
33333333443333333331
33333333443333333331
33333333443333333331
33333333443333333331
33333333443333333331
33333333443333333331
33333333443333333331
33333333443333333331
33333333443333333331
33333333443333333331
33333333443333333331
11222222442222222211
11222222442222222211
11222222442222222211
11222222442222222211
11222222442222222211
11222222442222222211
11111111441111111111
11111111441111111111

The '4's at 8,0 to 10,19 are only two wide, not three (i.e., the grid contains a 4 and 8,0 and a 4 and 8,1 but NOT a 4 and 8,2 since this diagram can't capture what would be shown on graph paper).

OUTPUT FORMAT

The output file should contain a list of all the colors that can be seen along with the total area of each color that can be seen (even if the regions of color are disjoint), ordered by increasing color. Do not display colors with no area.

SAMPLE OUTPUT (file rect1.out)

1 91
2 84
3 187
4 38
题意:给定一个大小为 a x b 的矩阵,在第一象限内。起初每个整点的颜色都是 1 ,现在用 n 个不同颜色的矩形去覆盖,求 n 次覆盖后每种颜色有多少。
分析:漂浮法,想象一下一共有 n 层,从底向上依次上浮,有重叠的去掉。
View Code
/*
  ID: dizzy_l1
  LANG: C++
  TASK: rect1
*/
#include<iostream>
#include<cstring>
#include<cstdio>
#define MAXC 2501
#define MAXN 1001

using namespace std;

struct rect1
{
    int x1,y1,x2,y2,c;
}r[MAXN];
int ans[MAXC],n;

void Search(int x1,int y1,int x2,int y2,int c,int k)
{
    if(k==n)
    {
        ans[c]+=(x2-x1)*(y2-y1);
        return ;
    }
    if(x2<=r[k].x1||x1>=r[k].x2||y2<=r[k].y1||y1>=r[k].y2)
    {
        Search(x1,y1,x2,y2,c,k+1);
        return ;
    }

    if(x1<=r[k].x1)
    {
        Search(x1,y1,r[k].x1,y2,c,k+1);
        x1=r[k].x1;
    }
    if(x2>=r[k].x2)
    {
        Search(r[k].x2,y1,x2,y2,c,k+1);
        x2=r[k].x2;
    }
    if(y1<=r[k].y1)
    {
        Search(x1,y1,x2,r[k].y1,c,k+1);
        y1=r[k].y1;
    }
    if(y2>=r[k].y2)
    {
        Search(x1,r[k].y2,x2,y2,c,k+1);
    }
}

int main()
{
    freopen("rect1.in","r",stdin);
    freopen("rect1.out","w",stdout);
    int a,b,i;
    while(scanf("%d %d %d",&a,&b,&n)==3)
    {
        memset(ans,0,sizeof(ans));
        for(i=0;i<n;i++)
            scanf("%d%d%d%d%d",&r[i].x1,&r[i].y1,&r[i].x2,&r[i].y2,&r[i].c);
        for(i=0;i<n;i++)
        {
            Search(r[i].x1,r[i].y1,r[i].x2,r[i].y2,r[i].c,i+1);
        }
        ans[1]=a*b;
        for(i=2;i<MAXC;i++) ans[1]-=ans[i];
        for(i=1;i<MAXC;i++)
        {
            if(ans[i]) printf("%d %d\n",i,ans[i]);
        }
    }
    return 0;
}

你可能感兴趣的:(USACO Section 3.1 Shaping Regions)