IV Balkan Olympiad
You've just be assigned the project of implemented a windowing interface. This windowing interface is fairly simple, and fortunately, you don't have to display the actual windows. There are 5 basic operations:
- Create a window
- Bring a window to the top
- Put a window to the bottom
- Destroy a window
- Output what percentage of a window is visible (i.e., isn't covered by windows above it).
In the input, the operations appear in the following format:
- Create window: w(I,x,y,X,Y)
- Bring window to top: t(I)
- Put window on bottom: b(I)
- Destroy window: d(I)
- Output percentage visible: s(I)
(x,y) and (X,Y) are opposite corners of the window. When a window is created, it is put `on top'. You can't create a window with an identifier that is already in use, but you can destroy a window and then create a new one with the identifier of the destroyed window. Coordinates will be positive integers, and all windows will be of non-zero area (x != X and y != Y). The x and y coordinates are between 1 and 32767 inclusive.
PROGRAM NAME: window
INPUT FORMAT
The input file consists of a sequence of commands to your interpreter. They will be listed one per line. Terminate the program when no more input is available
SAMPLE INPUT (file window.in)
w(a,10,132,20,12) w(b,8,76,124,15) s(a)
OUTPUT FORMAT
Output lines only for the s() commands. Of course, there might be several s() commands (but no more than 500) so the output should be a sequence of percentages, one per line, stating the percentage of the windows that are visible. The percentages should be rounded to 3 decimal places.
SAMPLE OUTPUT (file window.out)
49.167
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define Min(a,b) a<b?a:b
#define Max(a,b) a<b?b:a
#define DELETED 0
typedef struct Rectangle
{
int LX,LY;
int RX,RY;
int color;
int Depth;
}Rectangle;
FILE *fin,*fout;
Rectangle Rec[123];
int MaxD=10000,MinD=10000;
int comp(const void *a,const void *b)
{
return *(int *)a-*(int *)b;
}
void Cover(int RecNum,int *X,int *Y,int (*Board)[130])
{
int x1,x2,y1,y2;
int i=0,j,Temp;
if(Rec[RecNum].RX<Rec[RecNum].LX){
Temp=Rec[RecNum].LX;
Rec[RecNum].LX=Rec[RecNum].RX;
Rec[RecNum].RX=Temp;
}
if(Rec[RecNum].RY<Rec[RecNum].LY){
Temp=Rec[RecNum].LY;
Rec[RecNum].LY=Rec[RecNum].RY;
Rec[RecNum].RY=Temp;
}
while(X[i]!=Rec[RecNum].LX)
i++;
x1=i;
while(X[i]!=Rec[RecNum].RX)
i++;
x2=i;
i=0;
while(Y[i]!=Rec[RecNum].LY)
i++;
y1=i;
while(Y[i]!=Rec[RecNum].RY)
i++;
y2=i;
for(i=x1;i<x2;i++)
for(j=y1;j<y2;j++)
Board[i][j]=RecNum;
}
void PrintPecent(char RecNum)
{
int X[130],Y[130],Board[130][130];
int i,j,m=0,n;
float Sum=0,Square;
memset(Board,0,sizeof(Board));
for(i=48;i<123;i++){
if(Rec[RecNum].Depth<=Rec[i].Depth){
X[m]=Rec[i].LX;
Y[m++]=Rec[i].LY;
X[m]=Rec[i].RX;
Y[m++]=Rec[i].RY;
}
}
qsort(X,m,sizeof(int),comp);
qsort(Y,m,sizeof(int),comp);
for(i=1,n=0;i<m;i++) //离散化
if(X[n]!=X[i])
X[++n]=X[i];
for(i=1,n=0;i<m;i++)
if(Y[n]!=Y[i])
Y[++n]=Y[i];
Cover(RecNum,X,Y,Board);
for(i=48;i<123;i++){
if(i!=RecNum&&Rec[RecNum].Depth<=Rec[i].Depth){
Cover(i,X,Y,Board);
}
}
for(i=0;i<130;i++)
for(j=0;j<130;j++)
if(Board[i][j]==RecNum)
Sum+=(X[i+1]-X[i])*(Y[j+1]-Y[j]);
Square=abs((Rec[RecNum].RX-Rec[RecNum].LX)*(Rec[RecNum].RY-Rec[RecNum].LY));
fprintf(fout,"%.3f\n",100*Sum/Square);
}
int main()
{
char Select;
char RecNum;
fin=fopen("window.in","r");
fout=fopen("window.out","w");
while((Select=getc(fin))!=EOF){
switch(Select){
case 'w':{
fscanf(fin,"(%c",&RecNum);
fscanf(fin,",%d,%d,%d,%d)\n",&Rec[RecNum].LX,&Rec[RecNum].LY,&Rec[RecNum].RX,&Rec[RecNum].RY);
Rec[RecNum++].Depth=++MaxD;
}break;
case 't':{
fscanf(fin,"(%c)\n",&RecNum);
Rec[RecNum].Depth=++MaxD;
}break;
case 'b':{
fscanf(fin,"(%c)\n",&RecNum);
Rec[RecNum].Depth=--MinD;
}break;
case 'd':{
fscanf(fin,"(%c)\n",&RecNum);
Rec[RecNum].Depth=DELETED;
}break;
case 's':{
fscanf(fin,"(%c)\n",&RecNum);
PrintPecent(RecNum);
}break;
}
}
return 0;
}
二维离散化。。
先排序然后离散,再一个个覆盖,每次要计算就对整体离散化一次。。还好数据弱,不然感觉要超时
顺带看了一下矩阵切割的递归,感觉方便很多,不过忘得差不多了额。。
这道做完之后心里憔悴,10天没动。。。