USACO / Shaping Regions (矩形分割)

来源:互联网 发布:java wsdl 生成客户端 编辑:程序博客网 时间:2024/05/17 20:27

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 32 2 18 18 20 8 19 19 38 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:
1111111111111111111133333333443333333331333333334433333333313333333344333333333133333333443333333331333333334433333333313333333344333333333133333333443333333331333333334433333333313333333344333333333133333333443333333331333333334433333333311122222244222222221111222222442222222211112222224422222222111122222244222222221111222222442222222211112222224422222222111111111144111111111111111111441111111111
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 912 843 1874 38题意:给出每个玻璃的颜色,求它们按一定的顺序叠放后垂直方向上能被看到的各种颜色的面积。代码:  矩形切割
/*ID:138_3531LANG: CTASK: rect1*/
#include<stdio.h>#define nmax 1001struct N{    int x1, y1, x2, y2, color;}N[nmax];int square[2501], currentColor, total;void Calculation(int x1, int y1, int x2, int y2, int index){    do//检查当前矩形是否没被其他矩形覆盖    {        index++;    }while (index <= total && (x1 >= N[index].x2 || x2 <= N[index].x1 || y1 >= N[index].y2 || y2 <= N[index].y1));    if (index > total)//如果没被其他矩形覆盖,则计算当前矩形面积,加到属于颜色为currentColor的集合    {        square[currentColor] += (x2 - x1) * (y2 - y1);    }    else//如果被覆盖,就将矩形切出没被N[index]覆盖的小矩形    {        if (x1 < N[index].x1)        {            Calculation(x1, y1, N[index].x1, y2, index);            x1 = N[index].x1;        }        if (x2 > N[index].x2)        {            Calculation(N[index].x2, y1, x2, y2, index);            x2 = N[index].x2;        }        if (y1 < N[index].y1)        {            Calculation(x1, y1, x2, N[index].y1, index);            y1 = N[index].y1;        }        if (y2 > N[index].y2)        {            Calculation(x1, N[index].y2, x2, y2, index);            y2 = N[index].y2;        }    }}int main(){    freopen("rect1.in", "r", stdin);    freopen("rect1.out", "w", stdout);    int i, pre, width, length;    scanf("%d%d%d", &width, &length, &total);    N[0].x1 = N[0].y1 = 0, N[0].x2 = width, N[0].y2 = length, N[0].color = 1;    pre = 0;    for (i = 1; i <= total; i++)    {        scanf("%d%d%d%d%d", &N[i].x1, &N[i].y1, &N[i].x2, &N[i].y2, &N[i].color);        if (pre < N[i].color)        {            pre = N[i].color;        }    }    for (i = 0; i <= total; i++)    {        currentColor = N[i].color;        Calculation(N[i].x1, N[i].y1, N[i].x2, N[i].y2, i);    }    for (i = 1; i <= pre; i++)    {        if (square[i] > 0)        {            printf("%d %d\n", i, square[i]);        }    }    fclose(stdin);    fclose(stdout);    //system("pause");    return 0;}
原创粉丝点击