hdu 4185 Oil Skimming(二分匹配)

//
//  main.cpp
//  wzazzy
//
//  Created by apple on 2018/10/23.
//  Copyright © 2018年 apple. All rights reserved.
//

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
#include<stack>
#include<list>
#include<map>
#include<set>
#include<vector>

typedef long long int ll;
const int maxn =1000+10;
const int maxm=10000;
const int mod =1e9+7;
const int INF=0x3f3f3f3f;
struct Edge
{
    int to,next;
}edge[maxn*maxn];
int head[maxn*maxn],tot;
void init()
{
    tot=0;
    memset(head,-1,sizeof(head));
}
void addedge(int u,int v)
{
    edge[tot].to=v;edge[tot].next=head[u];
    head[u]=tot++;
}
int linker[maxn*maxn];
bool used[maxn*maxn];
int sum;
bool dfs(int u)
{
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].to;
        if(!used[v])
        {
            used[v]=true;
            if(linker[v]==-1||dfs(linker[v]))
            {
                linker[v]=u;
                return true;
            }
        }
    }
    return false;
}
int hungary()
{
    int res=0;
    memset(linker,-1,sizeof(linker));
    for(int u=0;u<sum;u++)
    {
        memset(used,false,sizeof(used));
        if(dfs(u)) res++;
    }
    return res;
}
char map[maxn][maxn];
int vis[maxn][maxn];
int dis[2][2]={{1,0},{0,1}};
int main(int argc, const char * argv[])
{
    int t;scanf("%d",&t);
    int cc=1;
    while(t--)
    {
        memset(map,0,sizeof(map));
        memset(vis,0,sizeof(vis));
        sum=0;
        int n;scanf("%d",&n);
        init();
        for(int i=0;i<n;i++) scanf("%s",map[i]);
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
                if(map[i][j]=='#')
                    vis[i][j]=sum++;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(map[i][j]!='.')
                    for(int k=0;k<4;k++)
                    {
                        int x=i+dis[k][0],y=j+dis[k][1];
                        if(y>=0&&y<n&&x>=0&&x<n&&map[x][y]!='.')
                            addedge(vis[i][j],vis[x][y]),addedge(vis[x][y],vis[i][j]);
                    }
            }
        }
        printf("Case %d: %d\n",cc++,hungary()/2);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/wzazzy/article/details/83317754