hdu4185 Oil Skimming

要用1×2的板子尽量多的覆盖##区域,且不能交叉,求至多可以覆盖多少板子。

每一个#向向下或向右相邻的#建边。求最大匹配就可以了。

其实这题数据是比较弱的把,应该是#的个数在600以内把。。

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#define inf 0x3f3f3f3f
#pragma comment(linker, "/STACK:16777216")
#define eps 1e-6
#define ll long long
using namespace std;

int n,cnt,mx[605],my[605],vis[605],e[605][605],mp[605][605];
char s[605][605];

int path(int i)
{
    int j;
    for(j=1;j<cnt;j++)
    {
        if(e[i][j]&&!vis[j])
        {
            vis[j]=1;
            if(my[j]==-1||path(my[j]))
            {
                my[j]=i;
                mx[i]=j;
                return 1;
            }
        }
    }
    return 0;
}

int hungary()
{
    int res=0;
    memset(mx,-1,sizeof mx);
    memset(my,-1,sizeof my);
    for(int i=1;i<cnt;i++)
    {
        if(mx[i]==-1)
        {
            memset(vis,0,sizeof vis);
            res+=path(i);
        }
    }
    return res;
}

int main()
{
    int icy,cas=1,i,j;
    scanf("%d",&icy);
    while(icy--)
    {
        scanf("%d",&n);
        memset(e,0,sizeof e);
        memset(mp,0,sizeof mp);
        for(i=0;i<n;i++)
            scanf("%s",s[i]);
        cnt=1;
        for(i=0;i<n;i++)
            for(j=0;j<n;j++)
                if(s[i][j]=='#')
                     mp[i][j]=cnt++;
        for(i=0;i<n;i++)
            for(j=0;j<n;j++)
            {
                if(mp[i][j])
                {
                    if(i<n-1&&mp[i+1][j])
                        e[mp[i][j]][mp[i+1][j]]=e[mp[i+1][j]][mp[i][j]]=1;
                    if(j<n-1&&mp[i][j+1])
                        e[mp[i][j+1]][mp[i][j]]=e[mp[i][j]][mp[i][j+1]]=1;
                }
            }
        memset(vis,0,sizeof vis);
        int ans=hungary();
        printf("Case %d: %d\n",cas++,ans/2);
    }
    return 0;
}

hdu4185 Oil Skimming

时间: 2024-10-05 05:04:48

hdu4185 Oil Skimming的相关文章

hdu4185 Oil Skimming(二分匹配)

# include <stdio.h> # include <algorithm> # include <string.h> using namespace std; int n,cot; int map[660],vis[660],pp[660][660],u[660][660]; int bfs(int x) { for(int i=1;i<=cot;i++) { if(!vis[i]&&pp[x][i]) { vis[i]=1; if(!ma

HDU4185 Oil Skimming —— 最大匹配

题目链接:https://vjudge.net/problem/HDU-4185 Oil Skimming Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3016    Accepted Submission(s): 1262 Problem Description Thanks to a certain "green" re

匈牙利算法求最大匹配(HDU-4185 Oil Skimming)

如下图:要求最多可以凑成多少对对象 ? 大佬博客:https://blog.csdn.net/cillyb/article/details/55511666 模板: int link[maxn],vis[maxn]; bool dfs(int x) { for(int i = 1; i <= num; i++) { if(!vis[i] && cp[x][i]) { vis[i] = 1; if(link[i] == 0 || dfs(link[i])) { link[i] = x;

HDU4185 Oil Skimming(匈牙利)

题意: n*n的图 相邻两##可除去 问最多除去多少 奇偶建图,OK /* *********************************************** //Author :devil //Created Time :2016/5/12 14:48:15 //************************************************ */ #include <cstdio> #include <cstring> #include <io

hdu 4185 Oil Skimming(二分匹配)

Oil Skimming Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 883    Accepted Submission(s): 374 Problem Description Thanks to a certain "green" resources company, there is a new profitable

HDU 4185 ——Oil Skimming——————【最大匹配、方格的奇偶性建图】

Oil Skimming Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 4185 Description Thanks to a certain "green" resources company, there is a new profitable industry of oil skimming. There ar

J - Oil Skimming 二分图的最大匹配

Description Thanks to a certain "green" resources company, there is a new profitable industry of oil skimming. There are large slicks of crude oil floating in the Gulf of Mexico just waiting to be scooped up by enterprising oil barons. One such

Oil Skimming HDU - 4185(匹配板题)

Oil Skimming Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3426    Accepted Submission(s): 1432 Problem Description Thanks to a certain "green" resources company, there is a new profitabl

4185 Oil Skimming 最大匹配 奇偶建图

题目大意: 统计相邻(上下左右)的‘#’的对数. 解法: 与题目hdu1507 Uncle Tom's Inherited Land*类似,需要用奇偶建图.就是行+列为奇数的作为X集合,偶尔作为Y集合,都是‘#’就连边.最后求最大匹配. 数据有点大,直接建图会出错(我试过).可以按照‘#’出现的顺序给顶点编号. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<queue&