`
Simone_chou
  • 浏览: 183926 次
  • 性别: Icon_minigender_2
  • 来自: 广州
社区版块
存档分类
最新评论

Muddy Fields(二分图最大匹配 + 匈牙利算法)

    博客分类:
  • POJ
 
阅读更多

 

Muddy Fields
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7569   Accepted: 2794

Description

Rain has pummeled the cows' field, a rectangular grid of R rows and C columns (1 <= R <= 50, 1 <= C <= 50). While good for the grass, the rain makes some patches of bare earth quite muddy. The cows, being meticulous grazers, don't want to get their hooves dirty while they eat. 

To prevent those muddy hooves, Farmer John will place a number of wooden boards over the muddy parts of the cows' field. Each of the boards is 1 unit wide, and can be any length long. Each board must be aligned parallel to one of the sides of the field. 

Farmer John wishes to minimize the number of boards needed to cover the muddy spots, some of which might require more than one board to cover. The boards may not cover any grass and deprive the cows of grazing area but they can overlap each other. 

Compute the minimum number of boards FJ requires to cover all the mud in the field.

Input

* Line 1: Two space-separated integers: R and C 

* Lines 2..R+1: Each line contains a string of C characters, with '*' representing a muddy patch, and '.' representing a grassy patch. No spaces are present.

Output

* Line 1: A single integer representing the number of boards FJ needs.

Sample Input

4 4
*.*.
.***
***.
..*.

Sample Output

4

Hint

OUTPUT DETAILS: 

Boards 1, 2, 3 and 4 are placed as follows: 
1.2. 
.333 
444. 
..2. 
Board 2 overlaps boards 3 and 4.

 

       题意:

       给出 N(1 ~ 50) 和 M (1 ~ 50),代表有 N 行 M 列, 每个点状态要不是 “ . ”,要不就是“ * ”,需要用木板覆盖所有的 * ,每块木板长短不限,打横或者打竖放,木板与木板间可以相交搭建。输出最少需要的木板数。

 

        思路:

        二分图最大匹配 + 匈牙利算法。求最小顶点覆盖(用最小的点去覆盖所有的边)。这题与之前做的有道题很类似,横纵坐标建图,但是那个是可以一次性覆盖不连续的点的,而这题是必须一次性要覆盖连续的点。所以对连通块编号,分为横连通块和竖连通块每个点都可以由一个横连通块和纵连通块组成,由此可以建成一个图,找出最少的连通块来覆盖所有的点,故所有的点都化成了线,要覆盖所有的边则是最小顶点覆盖,求出最大匹配即可。思路很重要,感觉这题比较难想。

 

       AC:

#include <cstdio>
#include <string.h>
using namespace std;

typedef struct {
    int x,y;
}node;
node no[55][55];
char s[55][55];
int w[1500][1500],linker[1500],vis[1500];
int n,m,un,vn;

bool dfs(int u) {
    for(int v = 1;v <= vn;v++)
        if(w[u][v] && !vis[v]) {
            vis[v] = 1;
            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 = 1;u <= un;u++) {
        memset(vis,0,sizeof(vis));
        if(dfs(u)) res++;
    }
    return res;
}

void build() {
    un = 0;  //行连通块结点
    for(int i = 1;i <= n;++i)
        for(int j = 1;j <= m;++j) {
            if(s[i][j] == '*') {
                if(j == 1) no[i][j].x = ++un;
                if(j > 1) {
                    if(s[i][j - 1] == '*') no[i][j].x = un;
                    else    no[i][j].x = ++un;
                }
            }
        }

    vn = 0;  //列连通块结点
    for(int j = 1;j <= m;++j)
        for(int i = 1;i <= n;++i) {
            if(s[i][j] == '*') {
                if(i == 1) no[i][j].y = ++vn;
                if(i > 1) {
                    if(s[i - 1][j] == '*') no[i][j].y = vn;
                    else    no[i][j].y = ++vn;
                }
            }
        }

    for(int i = 1;i <= n;++i)
        for(int j = 1;j <= m;++j)
            if(s[i][j] == '*')
               w[no[i][j].x][no[i][j].y] = 1;
}

int main() {
    //freopen("test.in","r",stdin);
    memset(w,0,sizeof(w));
    scanf("%d%d",&n,&m);
    for(int i = 1;i <= n;++i)
        for(int j = 1;j <= m;++j)
            scanf(" %c",&s[i][j]);

    build();

    printf("%d\n",hungary());
    return 0;
}

 

 

 

 

分享到:
评论

相关推荐

    Muddy

    Muddy

    panopticon-MuddyWater

    Panopticon-MuddyWater

    muddy:Node.js多用户地牢(MUD)框架

    输入泥泞的目录并安装依赖项: cd muddy (符号化,只需进入目录) npm install 设置MySQL数据库: 下载并安装(如果尚未安装)。 为泥泞创建数据库架构,然后导入文件。 使用您MySQL信息在浑浊的目录中创建一个名为...

    Muddy-Waters:Muddy Waters是我使用Node.js和Write编写的MuD

    Muddy Waters是我使用Node.js编写MuD并编写的。 要运行项目,请导航至终端中的项目文件夹-&gt;运行命令[npm run cli] 浑水将开始运行... 游戏开始后,可以使用“ / help”命令找到命令列表。 创建帐户时将生成用于保存...

    muddy-driver:GIGA Gamejam 2018的疯狂游戏

    摩的大飚客(Muddy Driver) GIGA Gamejam 2018的疯狂游戏。 要求 一台电脑。 四名球员。 四部手机。 和良好的局域网。 怎么玩 首先克隆代码,然后运行: npm install 打开client/src/config.ts ,将url更改为${...

    AbsoluteZero:Python APT Backdoor-Botnet Python MuddyWater娱乐

    Python APT后门僵尸网络/ Python MuddyWater Recreation 该项目是Python APT后门程序,针对Red Team Post Exploitation Tool进行了优化,它可以生成二进制有效负载或纯python源。 最终存根使用多态加密为自身提供第...

    MuddyC3v1.0.1-:这是MuddyWater APT C3 v1.0.1来源

    MuddyC3v1.0.1-:这是MuddyWater APT C3 v1.0.1来源

    Runescape-Web-Walker-Engine:为TriBot撰写的Rune Scape世界漫游者

    Runescape-Web-Walker-Engine RuneScape基于图的地图的路径查找,具有超过1000万个节点,这些节点经过优化,可以在&lt;200ms&gt; Explv的地图导入到Tribot。 不要克隆到脚本目录中! Tribot将无法检测到它。 将dax_api...

    Public-API:polyhaven.com 本身以及任何第三方应用程序使用的公共 API

    提供用于获取资产列表、类别以及有关单个资产的信息的端点。 目前住在GET端点:/assets 资产列表,包括其各自的元数据。 要过滤返回的数据,可以在请求 URL 中提供一些... 例如: /info/muddy_autumn_forest/files/[i

    RandomPoemGenerator:使用规则生成诗歌

    ADJECTIVE: black|white|dark|light|bright|murky|muddy|clear ||$END NOUN: heart|sun|moon|thunder|fire|time|wind|sea|river|flavor|wave|willow|rain|tree|flower|field|meadow|pasture|harvest|water|father|...

    fluent-plugin-simplearithmetic:用于计算消息的 Fluent 插件

    该插件基于 Muddy Dixon 编写的 。 该插件没有 fluent-plugin-datacalculator 提供的汇总功能。安装 $ fluent-gem install fluent-plugin-simplearithmetic教程假设你有这样的消息: { 'apple': 7, 'orange': 3, '...

    Poem-Generator:根据英语基本规则写诗

    ADJECTIVE: black|white|dark|light|bright|murky|muddy|clear ||$END NOUN: heart|sun|moon|thunder|fire|time|wind|sea|river|flavor|wave|willow|rain|tree|flower|field|meadow|pasture|harvest|water|father|...

    MuddysBundle

    Muddy's Bundle 是一个资源包选择器,由 、 和。 有很多小的美学调整和实用调整。 选择并下载您的包。 请务必在 上支持该项目! 接触 如果你发现了一个错误,或者想提出一些建议,请。 或者,您可以联系。 如果...

    go-anomalydetector

    去异常检测器 Goによる异常検知 执照 最初这个实现来自: : 版权所有 (c) 2013- Muddy Dixon 版权所有 (c) 2013- Naotoshi Seo 2014 年重新实施 - Shuhei Tanuma Apache 许可证,版本 2.0

    SnapLensStudio-Raycasting:射线投射任何网格并检索位置和旋转信息

    Raycasting on any mesh, muddy solution. Returns position and rotation to place an object on the mesh where ray intersects. Issues: - Mesh UVs are irreversibly overwritten with projected UVs. ...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    Parameters that are both input and output (often classes/structs) muddy the waters, and, as always, consistency with related functions may require you to bend the rule. Names and Order of Includes ...

    C程序设计语言(第2版·新版中文)

    16个muddy12345 15个zk6010 15个yosen 15个lleheaven 公司简介|招贤纳士|广告服务|银行汇款帐号|联系方式|版权声明|法律顾问|问题报告北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号世纪乐知(北京)网络...

    GIS基础概念知识学习

    GIS相关的基础知识,对刚入门的用户比较好的全面了解GIS相关知识。

Global site tag (gtag.js) - Google Analytics