Muddy Fields+POJ+二分图最大匹配
来源:互联网 发布:单片机最小系统 编辑:程序博客网 时间:2024/05/11 18:08
Muddy Fields
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 8173 Accepted: 3012
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
解决方案:此题由于不能跨过草地,所以,直接套用模板就不行了。先把每行,每列的'*'连成一块,在建立二分图,求其最大匹配,最大匹配即为最小覆盖点。
code:#include<iostream>#include<cstdio>#include<vector>#include<cstring>using namespace std;char Map[55][55];int U[55][55];int V[55][55];int Left[55*55];bool solve[55*55];vector<int >G[55*55];int R,C;int v,u;bool dfs(int u){ int len=G[u].size(); for(int i=0; i<len; i++) { int v=G[u][i]; if(!solve[v]) { solve[v]=true; if(Left[v]==-1||dfs(Left[v])) { Left[v]=u; return true; } } } return false;}int main(){ while(~scanf("%d%d",&R,&C)) { for(int i=0; i<=R*C; i++) { G[i].clear(); } memset(Left,-1,sizeof(Left)); for(int i=0; i<R; i++) { scanf("%s",Map[i]); } u=v=1; memset(U,0,sizeof(U)); memset(V,0,sizeof(V)); for(int i=0; i<R; i++) { for(int j=0; j<C; j++) { if(Map[i][j]=='*') { while(j<=C&&Map[i][j]=='*') { U[i][j]=u; j++; } u++; } } } for(int i=0; i<C; i++) for(int j=0; j<R; j++) { if(Map[j][i]=='*') { while(j<=R&&Map[j][i]=='*') { V[j][i]=v; j++; } v++; } } for(int i=0; i<R; i++) for(int j=0; j<C; j++) { if(Map[i][j]=='*') { G[U[i][j]].push_back(V[i][j]); } } int cnt=0; for(int i=1; i<u; i++) { memset(solve,false,sizeof(solve)); if(dfs(i)) { cnt++; } } printf("%d\n",cnt); } return 0;}
0 0
- Muddy Fields+POJ+二分图最大匹配
- 【二分图+最大匹配+有难度】poj 2226 Muddy Fields
- poj 2226 Muddy Fields(二分图最大匹配)
- POJ 2226 Muddy Fields(二分匹配【最大流】)
- POJ 2226 Muddy Fields 二分图巧妙建图 + 二分图最大匹配
- POJ 2226Muddy 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【二分匹配】
- 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(二分匹配 巧妙的建图)
- 维吾尔污染物入无人
- [HDOJ 4929] Another Letter Tree [LCA+动态规划]
- 编辑文章 - 博客频道 - CSDN.NET
- 马士兵struts2视频教程第二十四集
- HDU 4417 划分树+二分
- Muddy Fields+POJ+二分图最大匹配
- 【KMP算法详解——适合初学KMP算法的朋友】
- 和客户客户和客户即可史蒂夫史蒂夫
- 设置IIS网站独立帐号确保服务器安全
- MFC右键弹出菜单的示例
- Web端服务器推送技术原理分析及dwr框架简单的使用
- 太惹人驿帖u影忒u人体也iu影
- 关于javascript取出jason数据的属性名
- UVA 1262 - Password