【[Offer收割]编程练习赛11 C】岛屿3

【题目链接】:http://hihocoder.com/problemset/problem/1487

【题意】

中文题

【题解】

岛屿的数目对应了这个图中联通块的数目;
面积则对应有多少个方块;
周长。。。周长就是周长
每次新增加一个方块的时候;
对于联通块;
把每个坐标转换成一维的数字;
然后写个并查集;
对于周长;
查看这个格子周围4个格子;
如果有格子和它相邻;
则那一面不会算在周长里面;
则减掉;
如果没有格子相邻则加上一个单位的周长

【Number Of WA

0

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 1100;
const int ZDD = 1000999;

int n,bb[ZDD];
bool bo[N][N];
LL ltk,mj,zc;

int zh(int x,int y)
{
    return (x-1)*1000+y;
}

int zbb(int x)
{
    if (bb[x]==x)
        return x;
    else
        return bb[x] = zbb(bb[x]);
}

int main()
{
    //freopen("F:\rush.txt","r",stdin);
    ios::sync_with_stdio(false),cin.tie(0);//scanf,puts,printf就别用了!
    rep1(i,1,1000000) bb[i] = i;
    cin >> n;
    rep1(i,1,n)
    {
        int x,y;
        cin >> x >> y;
        x++,y++;
        if (bo[x][y])
        {
            cout << ltk <<' '<<mj<<' '<<zc<<endl;
            continue;
        }
        bo[x][y] = true,ltk++,mj++;
        rep1(j,1,4)
        {
            int x1 = x+dx[j],y1 = y+dy[j];
            if (bo[x1][y1])
            {
                zc--;
                int yy = zh(x1,y1),xx = zh(x,y);
                int r1 = zbb(yy),r2 = zbb(xx);
                if (r1!=r2)
                {
                    bb[r1] = r2;
                    ltk--;
                }
            }
            else
                zc++;
        }
        cout << ltk << ' ' << mj << ' ' << zc << endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/7626401.html