Artwork(哈希表+并查集)

Description

  A template for an artwork is a white grid of n × m squares. The artwork will be created by painting q horizontal and vertical black strokes. A stroke starts from square (x1, y1), ends at square (x2,y2)(x1=x2(x2,y2)(x1=x2 or y1=y2)y1=y2) and changes the color of all squares (x, y) to black where x1 ≤ x ≤ x2 and y1 ≤ y ≤ y2. The beauty of an artwork is the number of regions in the grid. Each region consists of one or more white squares that are connected to each other using a path of white squares in the grid, walking horizontally or vertically but not diagonally. The initial beauty of the artwork is 1. Your task is to calculate the beauty after each new stroke. Figure A.1 illustrates how the beauty of the artwork varies in Sample Input 1.

 

Input

    The first line of input contains three integers n, m and q (1 ≤ n, m ≤ 1000, 1 ≤ q ≤ 104). Then follow q lines that describe the     strokes.Each line consists of four integers x1,y1,x2,y2(1 ≤ x1 ≤ x2 ≤ n, 1 ≤ y1 ≤ y2 ≤ m). Either x1 = x2 or y1=y2(or both).

Output

For each of the q strokes, output a line containing the beauty of the artwork after the stroke.

Sample Input

4 6 5
2 2 2 6
1 3 4 3
2 5 3 5
4 6 4 6
1 6 4 6

Sample Output

1
3
3
4
3

代码

#include<stdio.h>
#include<string.h>
#define MAX 1000005
struct stock{
  int x1,y1;
  int x2,y2;
}pos[10005];
int num[MAX],father[MAX],ans[10005];
//num表示当前位置涂了几个黑点,ans[n]表示第n次画横线时白色连通块的个数
int dir[4][2]={{0,1},{-1,0},{0,-1},{1,0}};
int m,n,Que,temp;
void init()
{
    int i;
    for(i=1;i<=temp;i++)
    {
        father[i]=i;
        num[i]=0;
    }
}
int Hash(int x,int y)  //用哈希表表示
{
    int num=(x-1)*m+y;
    return num;
}
int Find(int x)
{
    return x==father[x]?x:father[x]=Find(father[x]);
}
void Merge(int x,int y) //将两个相邻连通块连接在一起
{
    int Fx=Find(x),Fy=Find(y);
    if(Fx==Fy) return ;
    temp--; //表示连通块个数
    father[Fx]=Fy;
}
int check(int x,int y)
{
    if(x>=1&&y>=1&&x<=n&&y<=m)
        return 1;
    else
        return 0;
}
void work(int x,int y) //将刚出现的白块连到连通块中
{
    int tx,ty;
    for(int i=0;i<4;i++)
    {
        tx=x+dir[i][0];
        ty=y+dir[i][1];
        if(check(tx,ty)&&!num[Hash(tx,ty)])
            Merge(Hash(tx,ty),Hash(x,y));
    }
}
int main()
{
    int i,j,x,y;
    scanf("%d%d%d",&n,&m,&Que);
    temp=n*m; init();
    //画黑线
    for(i=1;i<=Que;i++)
    {
        scanf("%d%d%d%d",&pos[i].x1,&pos[i].y1,&pos[i].x2,&pos[i].y2);
        for(x=pos[i].x1;x<=pos[i].x2;x++)
        {
            for(y=pos[i].y1;y<=pos[i].y2;y++)
            {
               if(num[Hash(x,y)]==0) //位置(x,y)涂黑
                    temp--;
               num[Hash(x,y)]+=1;
            }
        }
    }
    //求出最后一个图的白色连通块的个数
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=m;j++)
        {
            if(num[Hash(i,j)]==0)
                work(i,j);
        }
    }
    //向前面的图推
    for(i=Que;i>=1;i--)
    {
        ans[i]=temp;
        //一步一步撤去黑线
        for(x=pos[i].x1;x<=pos[i].x2;x++)
        {
            for(y=pos[i].y1;y<=pos[i].y2;y++)
            {
                num[Hash(x,y)]-=1;
                if(num[Hash(x,y)]==0)
                {
                    temp+=1;
                    work(x,y);
                }
            }
        }
    }
    for(i=1;i<=Que;i++)
    {
       printf("%d\n",ans[i]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/ZCMU_2024/article/details/81568430