POJ 2226 Muddy Fields 二分匹配
来源:互联网 发布:企业应用开发实战源码 编辑:程序博客网 时间:2024/05/16 01:57
Muddy Fields
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 7773 Accepted: 2866
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.
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.
* 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.
Boards 1, 2, 3 and 4 are placed as follows:
1.2.
.333
444.
..2.
Board 2 overlaps boards 3 and 4.
Source
USACO 2005 January Gold
题目链接:POJ 2226 Muddy Fields
题目大意:给你一块n*m的方格型农场,其中' * '是泥地,' . '是草地,你可以用一块任意长的木板覆盖一行或一列中连续的泥地,其中木板之间可以相互覆盖。问要将所有的泥地覆盖需要至少多少的木板?
题目分析:对每一块泥地,预处理出最优情况下它被覆盖时覆盖他的行木板的编号X[ i ][ j ]以及列木板的编号Y[ i ][ j ]。什么时候是最优情况?当且仅当该泥地与同一行连续或者同一列连续的泥地同一编号。这样在预处理之后就成功将行列二分,符合二分图的性质,将题目转化为二分图的最小点覆盖。对每块木板建边(X[ i ][ j ], Y[ i ][ j ]),求最大匹配即可。(最大匹配 = 最小点覆盖)
建图:每遇到一次草地就对编号++,每遇到泥地,泥地编号等于当前编号。
代码如下:
#include <stdio.h> #include <string.h> #include <algorithm> #define clear(A, X, SIZE) memset(A, X, sizeof(A[0]) * (SIZE + 1)) using namespace std; const int maxN = 2500; const int maxE = 1000000; struct Edge{ int v, n; }edge[maxE]; int adj[maxN], cntE, vis[maxN], link[maxN], X[maxN][maxN], Y[maxN][maxN]; int n, m, row, col; char s[maxN][maxN];void addedge(int u, int v){ edge[cntE].v = v; edge[cntE].n = adj[u]; adj[u] = cntE++; } int find(int u){ for(int i = adj[u]; ~i; i = edge[i].n) if(!vis[edge[i].v]){ int v = edge[i].v; vis[v] = 1; if(link[v] == -1 || find(link[v])){ link[v] = u; return 1; } } return 0; } int match(){ int ans = 0, i; clear(link, -1, col); for(i = 1; i <= row; ++i){ clear(vis, 0, col); ans += find(i); } return ans; } void work(){ row = 1, col = 1; for(int i = 0; i < n; ++i) scanf("%s", s[i]); for(int i = 0; i < n; ++i, ++row) for(int j = 0; j < m; ++j){ if(s[i][j] == '*') X[i][j] = row; else X[i][j] = 0, ++row; } for(int i = 0; i < m; ++i, ++col) for(int j = 0; j < n; ++j){ if(s[j][i] == '*') Y[j][i] = col; else Y[j][i] = 0, ++col; } --row, --col; clear(adj, -1, row); cntE = 0; for(int i = 0; i < n; ++i) for(int j = 0; j < m; ++j){ if(X[i][j]) addedge(X[i][j], Y[i][j]); } printf("%d\n", match()); } int main(){ while(~scanf("%d%d", &n, &m)) work(); return 0; }
0 0
- poj 2226 Muddy Fields 二分匹配匈牙利
- POJ 2226 Muddy Fields 二分匹配
- Poj 2226 Muddy Fields【二分匹配】
- 【二分图+最大匹配+有难度】poj 2226 Muddy Fields
- poj 2226 Muddy Fields(二分图匹配)
- POJ 2226 - Muddy Fields(二分图匹配)
- POJ 2226 Muddy Fields(二分匹配-hungary)
- POJ 2226 Muddy Fields(二分图匹配)
- POJ 2226 Muddy Fields(二分匹配【最大流】)
- poj 2226 Muddy Fields(二分图最大匹配)
- Muddy Fields+POJ+二分图最大匹配
- poj 2226 Muddy Fields 二分图
- poj 2226 Muddy Fields(二分图)
- 【二分图】poj 2226 Muddy Fields
- poj 2226 Muddy Fields 最大匹配
- POJ 2226 Muddy Fields 二分图巧妙建图 + 二分图最大匹配
- POJ 2226Muddy Fields(二分图最大匹配之最小点覆盖)
- POJ 2226 Muddy Fields(二分匹配 巧妙的建图)
- 增量PCA
- UITableView
- java多线程经典案例
- jquery ztree 3.4 基本使用
- WaitForSingleObject
- POJ 2226 Muddy Fields 二分匹配
- OpenGl
- 高性能WEB开发 - 图片篇
- 使用快速排序算法对列表进行排序——Leetcode系列(四)
- webwork 集合自动赋值
- WaitForSingleObject介绍
- ireport crosstab header
- C++面试汇总
- 【Kinect开发笔记之(二)】Kinect for windows发展历程