码迷,mamicode.com
首页 > 其他好文 > 详细

poj 2226 Muddy Fields(最小点覆盖+巧妙构图)

时间:2015-08-31 23:14:10      阅读:331      评论:0      收藏:0      [点我收藏+]

标签:

 

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, dont 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 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 using namespace std;
 5 #define N 560
 6 int n,m;
 7 
 8 char mp[N][N];
 9 int cnt[N][N];
10 int cnt1[N][N];
11 int fina[N][N];
12 int match[N];
13 int vis[N];
14 int tmp,tmp1;
15 bool dfs(int x){
16     for(int i=1;i<=tmp1;i++){
17         if(!vis[i] && fina[x][i]) {
18             vis[i]=1;
19             if(match[i]==-1 || dfs(match[i])){
20                 match[i]=x;
21                 return true;
22             }
23         }
24     }
25     return false;
26 }
27 void solve(){
28     memset(match,-1,sizeof(match));
29     int ans=0;
30     for(int i=1;i<=tmp;i++){
31         memset(vis,0,sizeof(vis));
32         if(dfs(i)){
33             ans++;
34         }
35     }
36     printf("%d\n",ans);
37 
38 }
39 int main()
40 {
41     while(scanf("%d%d",&n,&m)==2){
42         memset(mp,0,sizeof(mp));
43         for(int i=0;i<n;i++){
44             scanf("%s",mp[i]);
45         }
46         memset(cnt,-1,sizeof(cnt));
47         memset(cnt1,-1,sizeof(cnt1));
48         tmp=0;
49         for(int i=0;i<n;i++){
50             int flag=0;
51             for(int j=0;j<m;j++){
52 
53                 if(flag==0 && mp[i][j]==*){
54                     flag=1;
55                     cnt[i][j]=++tmp;
56                 }
57                 else if(flag==1 && mp[i][j]==*){
58                     cnt[i][j]=tmp;
59                 }
60                 else if(mp[i][j]==.){
61                     flag=0;
62                 }
63             }
64         }
65 
66         tmp1=0;
67         for(int j=0;j<m;j++){
68             int flag=0;
69             for(int i=0;i<n;i++){
70                 if(flag==0 && mp[i][j]==*){
71                     flag=1;
72                     cnt1[i][j]=++tmp1;
73                 }
74                 else if(flag==1 && mp[i][j]==*){
75                     cnt1[i][j]=tmp1;
76                 }
77                 else if(mp[i][j]==.){
78                     flag=0;
79                 }
80             }
81         }
82         memset(fina,0,sizeof(fina));
83         for(int i=0;i<n;i++){
84             for(int j=0;j<m;j++){
85                 if(cnt[i][j]!=-1 && cnt1[i][j]!=-1){
86                     fina[cnt[i][j]][cnt1[i][j]]=1;
87                 }
88             }
89         }
90 
91         solve();
92     }
93     return 0;
94 }
View Code

 

poj 2226 Muddy Fields(最小点覆盖+巧妙构图)

标签:

原文地址:http://www.cnblogs.com/UniqueColor/p/4774156.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!