poj 2226 Muddy Fields (二分匹配)

Muddy Fields
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7340   Accepted: 2715

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.

Source

 1 //9008K    63MS    C++    1570B    2013-11-10 20:07:57
 2 /*
 3 
 4 (网摘思路)
 5 题意:给定一个矩阵,其中有一些地方有水,用一些长度任意,宽度为1的木板盖住这些有水的地方,不能挡住草地,木板可覆盖,问至少需要几块板子。
 6 
 7 分析:二分图最小点集覆盖。二分图建图方法如下,把每段连续的横向的水洼看成是一个X集合中的点,每段连续的纵向的水洼看成是Y集合中的点。
 8 矩阵每个有水单元看成是一个边,它连接了它所在的横向水洼在X集合中对应的点和它所在的纵向水洼在Y集合中对应的点。(对于一段连续的水洼,
 9 如果要铺木板,一定要铺得尽量长)这样最小点集覆盖的意义就变成了,矩阵中的每个有水的单元(二分图中的每条边)所在的横向和纵向的水洼
10 (在X集合和Y集合中的两个端点)至少有一个被覆盖。求至少需要选几个点。
11 
12 */
13 #include<stdio.h>
14 #include<string.h>
15 struct node{
16     int x;
17     int y;
18 }edge[55][55];
19 char c[55][55];
20 int g[1500][1500];
21 int vis[1500];
22 int match[1500];
23 int n,m,N,M;
24 void build() //建图 
25 {
26     memset(g,0,sizeof(g));
27     N=M=0;
28     for(int i=0;i<n;i++)  //处理行,作二分图的一集合 
29         for(int j=0;j<m;j++){
30             while(c[i][j]=='.' && j<m) j++;
31             while(c[i][j]=='*' && j<m){
32                 edge[i][j].x=N;
33                 j++;
34             }
35             N++;
36         }    
37     for(int j=0;j<m;j++) //处理列,作二分图的另一集合 
38         for(int i=0;i<n;i++){
39             while(c[i][j]=='.' && i<n) i++;
40             while(c[i][j]=='*' && i<n){
41                 edge[i][j].y=M;
42                 i++;
43             }
44             M++;
45         } 
46     for(int i=0;i<n;i++) //建图 
47         for(int j=0;j<m;j++)
48             if(c[i][j]=='*')
49                 g[edge[i][j].x][edge[i][j].y]=1; 
50 }
51 int dfs(int x)
52 {
53     for(int i=0;i<M;i++){
54         if(!vis[i] && g[x][i]){
55             vis[i]=1;
56             if(match[i]==-1 || dfs(match[i])){
57                 match[i]=x;
58                 return 1;
59             }
60         }
61     }
62     return 0;
63 } 
64 int hungary() //匈牙利算法 
65 {
66     memset(match,-1,sizeof(match));
67     int ans=0;
68     for(int i=0;i<N;i++){
69         memset(vis,0,sizeof(vis));
70         if(dfs(i)) ans++;
71     }
72     return ans;
73 }
74 int main(void)
75 {
76     while(scanf("%d%d%*c",&n,&m)!=EOF)
77     {
78         for(int i=0;i<n;i++)
79             scanf("%s",c[i]);
80         build();
81         printf("%d
",hungary());
82     }
83     return 0;
84 }
原文地址:https://www.cnblogs.com/GO-NO-1/p/3417011.html