POJ 2226 Muddy Fields(二分匹配 巧妙的建图)

Posted qldabiaoge

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 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

题意:农夫John的养牛场,是一个R 行C 列的矩形,一场大雨后,养牛场低洼的地方都有了积水。John 的牛都很娇贵的,他们吃草的时候,不想把他们的蹄子给弄脏了。为了不让牛儿们把它们的蹄子弄脏,John 决定把有水的地方铺上木板。他的木板是宽度为1,长度没有限制的。

他想用最少数目的木板把所有有水的低洼处给覆盖上,前提是木板不能覆盖草地,但是可以重叠。

这道题,构图确实比比较巧妙,如果有连续的低洼处,假设是横排的,那么这几个连续的低洼处可以拿一个板子来覆盖,同样,如果竖排也有连续的低洼处,那么也可以拿一个板子来覆盖。这样,当一个低洼处既可以拿横着的板子,也可以拿竖着的板子覆盖时,就是相交了。那么这个交线就代表了一个低洼处,它既可以被横着盖,也可以被竖着盖。现在我们把所有横排的低洼处作为left(连续的低洼处作为1个板),竖着的也同理,以例题如下表式:
Sample:
4 4
*.*.
.***
***.
..*.

把行里面连在一起的坑连起来视为一个点,即一块横木板,编上序号,Sample则转化为:

1 0 2 0
0 3 3 3
4 4 4 0
0 0 5 0

把这些序号加入X集合,再按列做一次则为:

1 0 4 0
0 3 4 5
2 3 4 0
0 0 4 0

同样加入Y集合,一个坑只能被横着的或者被竖着的木板盖住,将原图的坑的也标上不同的序号,一共九个坑

1 . 2 .
. 3 4 5
6 7 8 .
. . 9 .

图中的2号低洼处既可以拿横着的2号板,也可以拿竖着的4号板来覆盖,那么2号版和四号板之间就有一条边,边实际表示了低洼处,例如2号低洼处的边为{2,4},可以理解为2号低洼处可以由2号板或4号板覆盖。用最少的点把所有的边连起来,这样就是最小点覆盖。

以上来自: http://hi.baidu.com/onlys_c/blog/item/9781e0dd858f2fd28d102919.html

 

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <queue>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <set>
 7 #include <iostream>
 8 #include <map>
 9 #include <stack>
10 #include <string>
11 #include <vector>
12 #define pi acos(-1.0)
13 #define eps 1e-6
14 #define fi first
15 #define se second
16 #define lson l,m,rt<<1
17 #define rson m+1,r,rt<<1|1
18 #define bug         printf("******
")
19 #define mem(a,b)    memset(a,b,sizeof(a))
20 #define fuck(x)     cout<<"["<<x<<"]"<<endl
21 #define f(a)        a*a
22 #define sf(n)       scanf("%d", &n)
23 #define sff(a,b)    scanf("%d %d", &a, &b)
24 #define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
25 #define pf          printf
26 #define FRE(i,a,b)  for(i = a; i <= b; i++)
27 #define FREE(i,a,b) for(i = a; i >= b; i--)
28 #define FRL(i,a,b)  for(i = a; i < b; i++)
29 #define FRLL(i,a,b) for(i = a; i > b; i--)
30 #define FIN freopen("in.txt","r",stdin)
31 #define lowbit(x)   x&-x
32 #pragma comment (linker,"/STACK:102400000,102400000")
33 using namespace std;
34 const int maxn =1004;
35 typedef long long LL;
36 int n, m, mp[maxn][maxn], x[maxn][maxn], y[maxn][maxn];
37 int num1, num2, dfscnt, a[maxn][maxn], vis[maxn], match[maxn];
38 char tu[maxn][maxn];
39 int dfs(int rt) {
40     for (int i = 1 ; i <= num2 ; i++) {
41         if (mp[rt][i]) {
42             if (vis[i] != dfscnt) {
43                 vis[i] = dfscnt;
44                 if (!match[i] || dfs(match[i])) {
45                     match[i] = rt;
46                     return 1;
47                 }
48             }
49         }
50     }
51     return 0;
52 }
53 int main() {
54     while(~scanf("%d%d", &n, &m)) {
55         for (int i = 1 ; i <= n ; i++) {
56             scanf("%s", tu[i]);
57             for (int j = 0 ; j < m ; j++) {
58                 if (tu[i][j] == *) a[i][j + 1] = 1;
59                 else a[i][j+1] = 0;
60             }
61         }
62         num1 = 0, num2 = 0;
63         for (int i = 1 ; i <= n ; i++) {
64             for (int j = 1 ; j <= m ; j++) {
65                 if (a[i][j] == 1) {
66                     num1++;
67                     while(j <= m && a[i][j] == 1) {
68                         x[i][j] = num1;
69                         j++;
70                     }
71                 }
72             }
73         }
74         for (int j = 1 ; j <= m ; j++) {
75             for (int i = 1 ; i <= n ; i++) {
76                 if (a[i][j] == 1) {
77                     num2++;
78                     while(i <= n && a[i][j] == 1) {
79                         y[i][j] = num2;
80                         i++;
81                     }
82                 }
83             }
84         }
85         for (int i = 1 ; i <= n ; i++) {
86             for (int j = 1 ; j <= m ; j++) {
87                 if (a[i][j] == 1)  mp[x[i][j]][y[i][j]] = 1;
88             }
89         }
90         dfscnt = 0;
91         int ans = 0;
92         for (int i = 1 ; i <= num1 ; i++) {
93             dfscnt++;
94             if (dfs(i)) ans++;
95         }
96         printf("%d
", ans);
97     }
98     return 0;
99 }

 



















































以上是关于POJ 2226 Muddy Fields(二分匹配 巧妙的建图)的主要内容,如果未能解决你的问题,请参考以下文章

POJ-2226 Muddy Fields---二分图匹配+巧妙构图

POJ2226 Muddy Fields(二分图最小点覆盖集)

POJ 2226 Muddy Fields 二分图(难点在于建图)

POJ2226Muddy Fields

poj2226Muddy Fields

POJ - 2226 Muddy Fields