矩形切割。话说usaco题目精简了之后,没有遇到网上说的第三章说的类似题目。
这题一直看不懂别人怎么切的。
睡了一觉醒来后就自己明白了。
/* ID: modengd1 PROG: window LANG: C++ */ #include <iostream> #include <stdio.h> #include <memory.h> #include <vector> using namespace std; struct WD { bool exist; int x0,y0,x1,y1; int high; }; WD allWD[62]; int H,L; int getindex(char ch) { if(ch<=‘z‘&&ch>=‘a‘) return ch-‘a‘; if(ch<=‘Z‘&&ch>=‘A‘) return ch-‘A‘+26; if(ch<=‘9‘&&ch>=‘0‘) return ch-‘0‘+52; } int getarea(int deep,int xx,int yy,int x_,int y_,int h) { if(xx>=x_||yy>=y_)//矩形不合法 return 0; if(deep==62)//浮到了最顶层 { return (x_-xx)*(y_-yy); } if(!allWD[deep].exist||h>=allWD[deep].high||allWD[deep].x1<=xx||allWD[deep].x0>=x_||allWD[deep].y1<=yy||allWD[deep].y0>=y_)//不相交 { return getarea(deep+1,xx,yy,x_,y_,h); } else//切割为上下左右四个部分 { int ret=0; ret+=getarea(deep+1,xx,yy,allWD[deep].x0,y_,h);//上 ret+=getarea(deep+1,allWD[deep].x1,yy,x_,y_,h);//下 ret+=getarea(deep+1,max(xx,allWD[deep].x0),yy,min(x_,allWD[deep].x1),allWD[deep].y0,h);//左 ret+=getarea(deep+1,max(xx,allWD[deep].x0),allWD[deep].y1,min(x_,allWD[deep].x1),y_,h);//右 return ret; } } int main() { freopen("window.in","r",stdin); freopen("window.out","w",stdout); char op; char id; WD newOne; H=L=0; for(int i=0;i<62;i++) allWD[i].exist=false; while(~scanf("%c",&op)) { getchar(); scanf("%c",&id); if(op==‘w‘) { int xx,yy,x_,y_; scanf(",%d,%d,%d,%d",&xx,&yy,&x_,&y_); newOne.x0=min(xx,x_);newOne.y0=min(yy,y_);//输入时将所有矩形的对角修改为左上角和右下角 newOne.x1=max(xx,x_);newOne.y1=max(yy,y_); newOne.high=++H; newOne.exist=true; allWD[getindex(id)]=newOne; } else if(op==‘t‘) { allWD[getindex(id)].high=++H; } else if(op==‘b‘) { allWD[getindex(id)].high=--L; } else if(op==‘d‘) { allWD[getindex(id)].exist=false; } else { int index=getindex(id); int ans=getarea(0,allWD[index].x0,allWD[index].y0,allWD[index].x1,allWD[index].y1,allWD[index].high); int div=(allWD[index].x1-allWD[index].x0)*(allWD[index].y1-allWD[index].y0); printf("%.3f\n",(double)ans*100/div); } getchar();getchar(); } return 0; }
时间: 2024-10-21 23:41:17