Draw a Mess (并查集)

几何绘画与像素覆盖算法

It's graduated season, every students should leave something on the wall, so....they draw a lot of geometry shape with different color. 

When teacher come to see what happened, without getting angry, he was surprised by the talented achievement made by students. He found the wall full of color have a post-modern style so he want to have an in-depth research on it. 

To simplify the problem, we divide the wall into n*m (1 ≤ n ≤ 200, 1 ≤ m ≤ 50000) pixels, and we have got the order of coming students who drawing on the wall. We found that all students draw four kinds of geometry shapes in total that is Diamond, Circle, Rectangle and Triangle. When a student draw a shape in pixel (i, j) with color c (1 ≤ c ≤ 9), no matter it is covered before, it will be covered by color c. 

There are q (1 ≤ q ≤ 50000) students who have make a drawing one by one. And after q operation we want to know the amount of pixels covered by each color. 

Input

There are multiple test cases. 

In the first line of each test case contains three integers n, m, q. The next q lines each line contains a string at first indicating the geometry shape: 

* Circle: given xc, yc, r, c, and you should cover the pixels(x, y) which satisfied inequality (x - xc) 2 + (y - yc) 2 ≤ r 2 with color c; 
* Diamond: given xc, yc, r, c, and you should cover the pixels(x, y) which satisfied inequality abs(x - xc) + abs(y - yc) ≤ r with color c; 
* Rectangle: given xc, yc, l, w, c, and you should cover the pixels(x, y) which satisfied xc ≤ x ≤ xc+l-1, yc ≤ y ≤ yc+w-1 with color c; 
* Triangle: given xc, yc, w, c, W is the bottom length and is odd, the pixel(xc, yc) is the middle of the bottom. We define this triangle is isosceles and the height of this triangle is (w+1)/2, you should cover the correspond pixels with color c; 

Note: all shape should not draw out of the n*m wall! You can get more details from the sample and hint. (0 ≤ xc, x ≤ n-1, 0 ≤ yc, y ≤ m-1) 

Output

For each test case you should output nine integers indicating the amount of pixels covered by each color. 

题解:想了好久,感觉要用到并查集,然后有点无从下手,然后参考了网上的博客,用暴力去给行涂色,再利用并查集的操作来维护列即可,但是G++通过不了

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
using namespace std;
 
const double pi=3.14;

double eps=0.000001;
 
int fa[100005];
int vis[100005];
int find(int x)
{
    if (fa[x]==x)
        return x;
    else return fa[x]=find(fa[x]);
}
struct node
{
    char op[12];
    int x,y,z,d;
    int e;
    node() {}
};
 
node tm[100005];
int ans[10];
int main()
{
    int n,m,k;
    while(scanf("%d%d%d",&n,&m,&k)!=EOF)
    {
        memset(ans,0,sizeof ans);
        for (int i=1; i<=k; i++)
        {
            scanf("%s%d%d%d%d",tm[i].op,&tm[i].x,&tm[i].y,&tm[i].z,&tm[i].d);
            if (tm[i].op[0]=='R') scanf("%d",&tm[i].e);
        }
 
        for (int j=0; j<n; j++)
        {
            for (int i=0; i<=m; i++) fa[i]=i,vis[i]=0;
            for (int i=k; i>=1; i--)
            {
                int l,r,col=tm[i].d;
                if (tm[i].op[0]=='C')
                {
                    int up=tm[i].x+tm[i].z;
                    int down=tm[i].x-tm[i].z;
                    if (!(j>=down&&j<=up ))continue;
                    int tmp=tm[i].z*tm[i].z-(tm[i].x-j)*(tm[i].x-j);
                    tmp=sqrt(tmp);
                    l=tm[i].y-tmp;
                    r=tm[i].y+tmp;
                }
                if (tm[i].op[0]=='D')
                {
                    int up=tm[i].x+tm[i].z;
                    int down=tm[i].x-tm[i].z;
                    if (!(j>=down&&j<=up ))continue;
                    l=tm[i].z-abs(j-tm[i].x);
                    r=tm[i].y+l;
                    l=tm[i].y-l;
                }
                if (tm[i].op[0]=='R')
                {
                    col=tm[i].e;
                    int up=tm[i].x+tm[i].z-1;
                    int down=tm[i].x;
                    if (!(j>=down&&j<=up ))continue;
                    l=tm[i].y;
                    r=tm[i].y+tm[i].d-1;
                }
                if (tm[i].op[0]=='T')
                {
                    int up=tm[i].x+(tm[i].z+1)/2-1;
                    int down=tm[i].x;
                    if (!(j>=down&&j<=up ))continue;
                    int tmp=(tm[i].z-1)/2+(tm[i].x-j);
                    l=tm[i].y-tmp;
                    r=tm[i].y+tmp;
                }
                l=max (l,0);
                r=min(r,m-1);
                int fx=find(l);
                for (int i=r; i>=l;)
                {
                    int fy=find(i);
                    if (!vis[fy]) ans[col]++;
                    vis[fy]=1;
                    if (fx!=fy) fa[fy]=fx;
                    i=fy-1;
                }
            }
        }
        for (int i=1; i<=9; i++)
        {
            if (i>1) printf(" ");
            printf("%d",ans[i]);
        }
        printf("\n");
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/Staceyacm/p/10781965.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值