链接:

http://acm.hdu.edu.cn/showproblem.php?pid=4185

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82834#problem/G

 

与上题(H)相似

 

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 510
#define INF 0x3f3f3f3f


// un是匹配左边的定点数, vn是匹配右边的定点数
int n, un, vn, used[N], p[N], Hash[N][N], g[N][N];
char G[N][N];

//匈牙利算法, 从左边开始找增广路
int Find(int u)
{
    for(int j=0; j<vn; j++)
    {
        if(!used[j] && g[u][j])
        {
            used[j] = 1;
            if(p[j]==-1 || Find(p[j]))
            {
                p[j] = u;
                return true;
            }
        }
    }
    return false;
}


//最大匹配数
int hungary()
{
    int ans = 0;

    memset(p, -1, sizeof(p));
    for(int i=0; i<un; i++)
    {
        memset(used, 0, sizeof(used));
        if(Find(i)) ans++;
    }
    return ans;
}


int main()
{
    int t, k=1;
    scanf("%d", &t);
    while(t--)
    {
        int i, j, tol=0;

        scanf("%d", &n);
        memset(G, 0, sizeof(G));
        memset(Hash, 0, sizeof(Hash));
        memset(g, 0, sizeof(g));

        for(i=0; i<n; i++)
        {
            scanf("%s", G[i]);
            for(j=0; j<n; j++)
            if(G[i][j]=='#')
                Hash[i][j]=tol++;
        }

        for(i=0; i<n; i++)
        for(j=0; j<n; j++)
        {
            if(G[i][j]=='#')
            {
                 if(i>0 && G[i-1][j]=='#')     g[Hash[i][j]][Hash[i-1][j]]=1;
                 if(i<n-1 && G[i+1][j]=='#')   g[Hash[i][j]][Hash[i+1][j]]=1;
                 if(j>0 && G[i][j-1]=='#')     g[Hash[i][j]][Hash[i][j-1]]=1;
                 if(j<n-1 && G[i][j+1]=='#')   g[Hash[i][j]][Hash[i][j+1]]=1;
            }
        }

        un = vn = tol;
        printf("Case %d: %d\n", k++, hungary()/2);

    }
    return 0;
}

 

posted on 2015-08-11 20:58  栀蓝  阅读(160)  评论(0编辑  收藏  举报

levels of contents