USACO3.1.4--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
题解:用染色法显然要超空间。所以得用其他的方法,其实我想到了叫做矩形切割的东东,不过我觉得两个两个矩形之间的位置关系太多了,一一列举实在麻烦。。。所以放弃了,只好去看题解。想不到题解就是用矩形切割,所以我找了篇薛矛大牛的论文看了下,想不到矩形切割是如此的简单清晰,综合起来就四种情况,实在太强大了。以下是薛矛大牛的论文矩形切割的部分:
ORZ薛矛大神的矩形切割论文!!!
1 /* 2 ID:spcjv51 3 PROG:rect1 4 LANG:C 5 */ 6 #include<stdio.h> 7 #include<string.h> 8 #define MAXS 2501 9 typedef struct 10 { 11 int x1,y1,x2,y2,color; 12 } rectangles; 13 long rs[MAXS]; 14 rectangles rec[1005]; 15 int a,b,n; 16 int max(int a,int b) 17 { 18 return a>b?a:b; 19 } 20 int min(int a,int b) 21 { 22 return a<b?a:b; 23 } 24 void cut(int x1,int y1,int x2,int y2,int step,int colors) 25 { 26 int k1,k2,k3,k4; 27 while((step<=n)&&(x2<=rec[step].x1||rec[step].x2<=x1||y2<=rec[step].y1||rec[step].y2<=y1)) step++; 28 if(x1==x2||y1==y2) return; 29 if(step>n) 30 { 31 rs[colors]+=(x2-x1)*(y2-y1); 32 return; 33 } 34 k1=max(x1,rec[step].x1); 35 k2=min(x2,rec[step].x2); 36 if(x1<k1) cut(x1,y1,k1,y2,step+1,colors); 37 if(k2<x2) cut(k2,y1,x2,y2,step+1,colors); 38 k3=max(y1,rec[step].y1); 39 k4=min(y2,rec[step].y2); 40 if(y1<k3) cut(k1,y1,k2,k3,step+1,colors); 41 if(k4<y2) cut(k1,k4,k2,y2,step+1,colors); 42 43 } 44 int main(void) 45 { 46 freopen("rect1.in","r",stdin); 47 freopen("rect1.out","w",stdout); 48 long i; 49 scanf("%d%d%d",&a,&b,&n); 50 memset(rs,0,sizeof(rs)); 51 for(i=1; i<=n; i++) 52 scanf("%d%d%d%d%d",&rec[i].x1,&rec[i].y1,&rec[i].x2,&rec[i].y2,&rec[i].color); 53 rs[rec[n].color]=(rec[n].x2-rec[n].x1)*(rec[n].y2-rec[n].y1); 54 rs[1]=a*b; 55 for(i=n-1; i>=1; i--) 56 if(rec[i].color!=1) 57 cut(rec[i].x1,rec[i].y1,rec[i].x2,rec[i].y2,i+1,rec[i].color); 58 for(i=2;i<MAXS;i++) 59 rs[1]-=rs[i]; 60 for(i=1; i<MAXS; i++) 61 if(rs[i]) 62 printf("%ld %ld\n",i,rs[i]); 63 return 0; 64 }