Oil Skimming---hdu4185(最大匹配)

题目链接

题意:有一个地图.代表水#代表油每个单元格是10*10的,现有10*20的勺子可以提取出水上漂浮的油,问最多可以提取几勺的油;

每次提取的时候勺子放的位置都要是油,不然就被污染而没有价值了;

所以就是求最大匹配的;关键是建立边与边的关系,可以让有油的地方编号为1 2 3。。。然后再连接上下左右的点;

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define N 660
#define INF 0xfffffff
int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1} };
int G[N][N], a[N][N], cnt, vis[N], used[N];///a[i][j]代表这个位置#的编号从1开始;
char maps[N][N];
bool Find(int u)
{
    for(int i=1; i<cnt; i++)
    {
        if(!vis[i] && G[u][i])
        {
            vis[i] = 1;
            if(!used[i] || Find(used[i]))
            {
                used[i] = u;
                return true;
            }
        }
    }
    return false;
}
int main()
{
    int T, t=1, n;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d", &n);
        cnt=1;
        memset(used, 0, sizeof(used));
        memset(G, 0, sizeof(G));
        memset(a, 0, sizeof(a));
        memset(maps, 0, sizeof(maps));
        for(int i=0; i<n; i++)
        {
            scanf("%s", maps[i]);
            for(int j=0; j<n; j++)
            {
                if(maps[i][j] == ‘#‘)
                    a[i][j] = cnt++;
            }
        }
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<n; j++)
            {
                if(maps[i][j]==‘#‘)
                for(int k=0; k<4; k++)/// 上下左右建立关系;
                {
                    int x = i+dir[k][0];
                    int y = j+dir[k][1];
                    if(x>=0 && y>=0 && x<n && y<n && maps[x][y]==‘#‘)
                    {
                        int p=a[x][y];
                        int q=a[i][j];
                        G[p][q] = G[q][p] = 1;///建图;
                    }
                }
            }
        }
        int ans = 0;
        for(int i=1; i<cnt; i++)
        {
            memset(vis, 0, sizeof(vis));
            if(Find(i))
                ans++;
        }
        printf("Case %d: %d\n", t++, ans/2);
    }
    return 0;
}

时间: 2025-01-31 09:12:22

Oil Skimming---hdu4185(最大匹配)的相关文章

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

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

要用1×2的板子尽量多的覆盖##区域,且不能交叉,求至多可以覆盖多少板子. 每一个#向向下或向右相邻的#建边.求最大匹配就可以了. 其实这题数据是比较弱的把,应该是#的个数在600以内把.. #include <iostream> #include <cstdlib> #include <cstring> #include <string> #include <cstdio> #include <cmath> #include <

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

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

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

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&

HDU4185 Oil Skimming(匈牙利)

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