Poj 2226 muddy fields (minimum vertex overwrite in a bipartite graph)

Source: Internet
Author: User
B-muddy fields Time limit:1000 ms Memory limit:65536kb 64bit Io format:% I64d & % i64usubmit status practice poj 2226

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. Question: Give a map, some of which have water troughs. Use boards to cover them, and find the minimum number of boards (horizontal or vertical ). Ideas:

In the bipartite graph, select the least number of points to associate these points with all edges (overwrite all edges), which is called the least point overwrite.
Think of each continuous Horizontal water hole as a point in set a, and each continuous Vertical Water hole as a point in Set B, and the intersection in the middle as a line,
Because each water unit must be covered, at least one of the two connected units is covered.
Therefore, the minimum vertex overwrite is required. Clever thinking.

While the minimum vertex overwrite = the maximum number of matches

 1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <string> 5 #include <queue> 6 #include <cstring> 7 #include <vector> 8 using namespace std; 9 #define maxn 510 10 int R, C;11 int h[maxn][maxn], z[maxn][maxn];12 char mp[55][55];13 int line[maxn], vis[maxn];14 int cnth, cntz, ans;15 vector <int> q[maxn];16 void init(){17     memset(h, 0, sizeof(h));18     memset(z, 0, sizeof(z));19     memset(q, 0, sizeof(q));20     cnth = 1;21     for(int i = 1; i <= R; i++){22         for(int j = 1; j <= C; j++){23             if(mp[i][j] == ‘*‘) h[i][j] = cnth;24             if(mp[i][j] == ‘*‘ && mp[i][j+1] != ‘*‘) cnth++;25             26         }27     }28     29     cntz = 1;30     for(int i = 1; i <= C; i++){31         for(int j = 1; j <= R; j++){32             if(mp[j][i] == ‘*‘) z[j][i] = cntz;33             if(mp[j][i] == ‘*‘ && mp[j+1][i] != ‘*‘) cntz++;34         }35     }36     37     for(int i = 1; i <= R; i++){38         for(int j = 1; j <= C; j++){39             if(mp[i][j] == ‘*‘){40                 q[h[i][j]].push_back(z[i][j]);41             }42         }43     }44 }45 bool find(int x){46     for(int i = 0; i < q[x].size(); i++){47         if(!vis[ q[x][i] ] ){48             vis[ q[x][i] ] = 1;49             if(!line[q[x][i]] || find(line[q[x][i]])){50                 line[q[x][i]] = x;51                 return true;52             }53         }54     }55     return false;56 }57 void hungry(){58     ans = 0;59     memset(line, 0, sizeof(line));60     for(int i = 1; i < 510; i++){61         memset(vis, 0, sizeof(vis));62         if(find(i)) ans++;63     }64     printf("%d\n", ans);65     66 }67 int main(){68     while(~scanf("%d%d", &R, &C)){69         memset(mp, ‘-1‘, sizeof(mp));70         for(int i = 1; i <= R; i++){71             for(int j = 1; j <= C; j++) cin>>mp[i][j];72         }73         init();74         hungry();75     }76 77 78     return 0;79 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.