USACO5.3.2 Window Area(window)

对于创建,置顶,置底,删除一个窗体 ,很简单就不赘述了。重点在最后一问输出面积百分比,即矩形切割的题目。

所谓的矩形切割,矩形a与矩形b重叠,其中a在下面,b在上面,我们要知道a有多少面积可以被看见。其实就是用dfs的方法,在多个矩形重叠的情况下,一般先按重叠次序排一下序,然后从要询问的那个开始一次与他上面相邻的矩形比较,每次把不重叠的左边、右边、上边、下边一部分分别割成4个小矩形,然后每个小矩形递归的向上计算(也可以使用队列迭代),然后最终的总和就是结果。


/*
ID: xsy97051
PROB: window
LANG: C++
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;

struct window
{  
	char name;
	double area;
    int x1,y1,x2,y2;
} a[600],exchange;
string st;
int N=0;

window insert(int x1u,int y1u,int x2u,int y2u,char name)
{
      window use;
      use.name=name;
      use.x1=min(x1u,x2u);
      use.x2=max(x1u,x2u);
      use.y1=min(y1u,y2u);
      use.y2=max(y1u,y2u);
      use.area=(abs(x1u-x2u)*1.0)*(abs(y1u-y2u)*1.0);
      return use;
}

int find(char name)
{
	for(int i=1;i<=N;i++)
    	if(a[i].name==name)
        	return i;
    return 0;
}

int figure(int x1,int y1,int x2,int y2,int where)
{
	int sum=0;
	while(where<=N && (x1>=a[where].x2 || x2<=a[where].x1 || y1>=a[where].y2 || y2<=a[where].y1))
        where++;
	if(where>N)   return (x2-x1)*(y2-y1);
    if(x1<a[where].x1)
   	{
   		sum+=figure(x1,y1,a[where].x1,y2,where+1);
   		x1=a[where].x1;
   	}
    if(x2>a[where].x2)
   	{
   		sum+=figure(a[where].x2,y1,x2,y2,where+1);
   		x2=a[where].x2;
   	}
    if(y1<a[where].y1)   
    {
    	sum+=figure(x1,y1,x2,a[where].y1,where+1);
    	y1=a[where].y1;
    }
    if(y2>a[where].y2)   
    {
    	sum+=figure(x1,a[where].y2,x2,y2,where+1);
    	y2=a[where].y2;
    }
	return sum;
}

int main()
{
	freopen("window.in","r",stdin);
	freopen("window.out","w",stdout);
	while(cin>>st)
	{
		if(st[0]=='w')
		{
		    int now=4,x1=0,y1=0,x2=0,y2=0;
            char name=st[2];
            while(st[now]!=','){x1=(x1*10+st[now]-'0');now++;};		now++;
            while(st[now]!=','){y1=(y1*10+st[now]-'0');now++;};		now++;
            while(st[now]!=','){x2=(x2*10+st[now]-'0');now++;};		now++;
            while(st[now]!=')'){y2=(y2*10+st[now]-'0');now++;};		now++;
            a[++N]=insert(x1,y1,x2,y2,name);   	
		}
		if(st[0]=='t')
		{
			int where=find(st[2]);
            swap(a[where],exchange);
            for(int i=where;i<N;i++)
            	a[i]=a[i+1];
            a[N]=exchange;
		}
		if(st[0]=='b')
		{
			int where=find(st[2]);
            swap(a[where],exchange);
            for(int i=where;i>1;i--)
            	a[i]=a[i-1];
            a[1]=exchange;	
		}
		if(st[0]=='d')
		{
			int where=find(st[2]);
            swap(a[where],exchange);
            for(int i=where;i<N;i++)
            a[i]=a[i+1];
            N--;
		}
		if(st[0]=='s')
		{
			int where=find(st[2]);
            double ans;
            ans=figure(a[where].x1,a[where].y1,a[where].x2,a[where].y2,where+1);
            ans/=a[where].area;
            ans*=100.0;
            printf("%.3lf\n",ans);
		}
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值