poj 2226 Muddy Fields(最小点覆盖+巧妙构图)
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 : Two space-separated integers: R and C * Lines ..R+: Each line contains a string of C characters, with '*' representing a muddy patch, and '.' representing a grassy patch. No spaces are present.
Output
* Line : A single integer representing the number of boards FJ needs.
Sample Input
*.*.
.***
***.
..*.
Sample Output
Hint
Boards 1, 2, 3 and 4 are placed as follows:
1.2.
.333
444.
..2.
Board 2 overlaps boards 3 and 4.
Source
题目大意:用木板将'*'覆盖,同一行或同一列的'*'可以用一块木板覆盖,'.'不能被覆盖。问最少用多少块木板可以把全部的'*'覆盖?
木板只能够覆盖连续的横着的泥巴和竖着的泥巴,中间有草地就要隔开
解题思路:二分匹配的经典构图题目
构图思路:
将横着的木板和看成一边的点的集合,将竖着的木板看成另外一边的点的集合,如果他们相交于一点就连边
如果要把所有的泥巴覆盖,又要所需要的木板最少,那么就是求最小点覆盖
所以用匈牙利求最大匹配数即可
构图的代码要好好再看看!
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define N 560
int n,m; char mp[N][N];
int cnt[N][N];
int cnt1[N][N];
int fina[N][N];
int match[N];
int vis[N];
int tmp,tmp1;
bool dfs(int x){
for(int i=;i<=tmp1;i++){
if(!vis[i] && fina[x][i]) {
vis[i]=;
if(match[i]==- || dfs(match[i])){
match[i]=x;
return true;
}
}
}
return false;
}
void solve(){
memset(match,-,sizeof(match));
int ans=;
for(int i=;i<=tmp;i++){
memset(vis,,sizeof(vis));
if(dfs(i)){
ans++;
}
}
printf("%d\n",ans); }
int main()
{
while(scanf("%d%d",&n,&m)==){
memset(mp,,sizeof(mp));
for(int i=;i<n;i++){
scanf("%s",mp[i]);
}
memset(cnt,-,sizeof(cnt));
memset(cnt1,-,sizeof(cnt1));
tmp=;
for(int i=;i<n;i++){
int flag=;
for(int j=;j<m;j++){ if(flag== && mp[i][j]=='*'){
flag=;
cnt[i][j]=++tmp;
}
else if(flag== && mp[i][j]=='*'){
cnt[i][j]=tmp;
}
else if(mp[i][j]=='.'){
flag=;
}
}
} tmp1=;
for(int j=;j<m;j++){
int flag=;
for(int i=;i<n;i++){
if(flag== && mp[i][j]=='*'){
flag=;
cnt1[i][j]=++tmp1;
}
else if(flag== && mp[i][j]=='*'){
cnt1[i][j]=tmp1;
}
else if(mp[i][j]=='.'){
flag=;
}
}
}
memset(fina,,sizeof(fina));
for(int i=;i<n;i++){
for(int j=;j<m;j++){
if(cnt[i][j]!=- && cnt1[i][j]!=-){
fina[cnt[i][j]][cnt1[i][j]]=;
}
}
} solve();
}
return ;
}
poj 2226 Muddy Fields(最小点覆盖+巧妙构图)的更多相关文章
- POJ 2226 Muddy Fields (最小点覆盖集,对比POJ 3041)
题意 给出的是N*M的矩阵,同样是有障碍的格子,要求每次只能消除一行或一列中连续的格子,最少消除多少次可以全部清除. 思路 相当于POJ 3041升级版,不同之处在于这次不能一列一行全部消掉,那些非障 ...
- POJ - 2226 Muddy Fields (最小顶点覆盖)
*.*. .*** ***. ..*. 题意:有一个N*M的像素图,现在问最少能用几块1*k的木条覆盖所有的 * 点,k为>=1的任意值. 分析:和小行星那题很像.小行星那题是将一整行(列)看作 ...
- POJ 2226 Muddy Fields(最小顶点覆盖)
POJ 2226 Muddy Fields 题目链接 题意:给定一个图,要求用纸片去覆盖'*'的位置.纸片能够重叠.可是不能放到'.'的位置,为最少须要几个纸片 思路:二分图匹配求最小点覆盖.和放车那 ...
- poj 2226 Muddy Fields (转化成二分图的最小覆盖)
http://poj.org/problem?id=2226 Muddy Fields Time Limit: 1000MS Memory Limit: 65536K Total Submissi ...
- poj 2226 Muddy Fields(最小覆盖点+构图)
http://poj.org/problem?id=2226 Muddy Fields Time Limit: 1000MS Memory Limit: 65536K Total Submissi ...
- [POJ] 2226 Muddy Fields(二分图最小点覆盖)
题目地址:http://poj.org/problem?id=2226 二分图的题目关键在于建图.因为“*”的地方只有两种木板覆盖方式:水平或竖直,所以运用这种方式进行二分.首先按行排列,算出每个&q ...
- POJ 2226 Muddy Fields(最小点覆盖)题解
题意:一片r*c的地,有些地方是泥地,需要铺地板.这些地板宽1,长无限,但只能铺在泥地上不能压到其他地方,问你铺满所有泥地最少几块 思路:我们把一行中连续的泥地看成整体,并把所有横的整体里的点编成一个 ...
- poj 2226 Muddy Fields(最小点覆盖)
题意: M*N的矩阵,每个格不是*就是#. *代表水坑,#代表草地. 农民要每次可以用一块宽为1,长不限的木板去铺这个矩阵.要求这块木板不能覆盖草地.木板可以重复覆盖(即一块木板与另一块木板有 ...
- POJ 2226 Muddy Fields (二分图匹配)
[题目链接] http://poj.org/problem?id=2226 [题目大意] 给出一张图,上面有泥和草地,有泥的地方需要用1*k的木板覆盖, 有草地的地方不希望被覆盖,问在此条件下需要的最 ...
随机推荐
- hadoop部署工具与配置工具
https://github.com/xianglei/phpHiveAdmin 随着Hadoop的推出,大数据处理实现了技术上的落地.但是对于一般的公司和开发者而言,Hadoop依旧是一个陌生或者难 ...
- ExtJS4.2学习(11)——高级组件之Grid
大纲: 1.首先,搭建起来一个最基础的Grid组件: 2.其次,利用前边MVC架构将代码重构: 3.再者,介绍下Grid的一些特性. 一.搭建基础的Grid组件 在文章的开始,我们首先简单的搭建一个G ...
- DFBle.swift
//// DFBle.swift// DFBle//// Created by LeeYaping on 15/9/2.// Copyright (c) 2015年 lisper. All r ...
- Android NOtification 使用(震动 闪屏 铃声)
一. Notification 简介 在 android 系统中,在应用程序可能会遇到几种情况需要通知用户,有的需要用户回应,有的则不需要,例如: * 当保存文件等事件完成,应该会出现一个小的消息,以 ...
- Linux下一些基本操作
一.忘记root密码 1. sudo passwd root 2. 输入新密码. 二.查看内核版本: 1.查看内核版本命令:1) cat /proc/version 2) uname -a 3) u ...
- Java基础知识强化36:StringBuffer类之StringBuffer的概述
1. StringBuffer类概述: (1)String的缺陷: 我们如果对字符串进行拼接操作,每次拼接,都会构造一个新的String对象,既耗时,又浪费空间.如下图: (2)StringBuffe ...
- Android -------- eclipse平台上的单元测试框架
eclipse平台上单元测试框架 继承android.test.AndroidTestCase类 清单文件中设置 设置指令集,与application标签同级 <instrumentation ...
- 多线程、多任务管理 简单demo
需求:假设多个任务需要执行,每个任务不是一时半会能完成(需要能看到中间执行状况): 多个任务 根据条件不同 可能需要不同的处理 分析: 多线程并发执行多任务: 对任务进行管理,追踪中间执行状态: 运用 ...
- javascrip中setTimeout和setInterval
1: http://www.jb51.net/article/68258.htm 2: http://www.jb51.net/article/26679.htm
- POJ1276:Cash Machine(多重背包)
Description A Bank plans to install a machine for cash withdrawal. The machine is able to deliver ap ...