Poj2226muddy fields [minimum point coverage (better graph creation )]

Source: Internet
Author: User

For example, tell you a matrix, so that you can overwrite it with a 1 x (x is of any value) board with a deploy symbol *

Ask how many boards can cover all *

4 4 *.*..******...*.

Boards 1, 2, 3 and 4 are placed as follows:
1.2.
. 333
444.
... 2.
Board 2 overlaps boards 3 and 4.

Analysis:
The requirement is to overwrite all vertices.
Recall what you have done before
This type is probably the minimum vertex coverage or the minimum edge coverage of a bipartite graph.
How can we create a bipartite graph?
Because X is an uncertain value
So we try to make the greater the value of X, the better.
That is to say, each horizontal or vertical * region can be considered as a Unicom block.
You only need to place the horizontal connected blocks in the left set, and the vertical connected blocks in the right set to show the points to the left.
As long as all edges are covered, it will overwrite all edges *.
In this case, because the left and right sets are the connected blocks you want, you only need to find the minimum vertex coverage set.
Minimum vertex overwrite set = maximum matching of a Bipartite Graph

Code:
 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <vector> 5 using namespace std; 6  7 const int maxn = 55; 8 int G[maxn * maxn / 2][maxn * maxn / 2]; 9 10 int vis[maxn * maxn / 2];11 int Link[maxn * maxn / 2];12 int n;13 bool Find(int u) {14     for(int i = 1; i <= n; i++) {15         if(G[u][i] && !vis[i]) {16             vis[i] = 1;17             if(Link[i] == -1 || Find(Link[i])) {18                 Link[i] = u;19                 return true;20             }21         }22     }23     return false;24 }25 26 int solve() {27     int cnt = 0;28     memset(Link, -1, sizeof(Link));29     for(int i = 1; i <= n; i++) {30         memset(vis, 0, sizeof(vis));31         if(Find(i)) cnt++;32     }33     return cnt;34 }35 36 char mat[maxn][maxn];37 int vis1[maxn][maxn], vis2[maxn][maxn];38 int main() {39     int m;40     for(int i = 0; i <= 50; i++) {41         mat[0][i] = mat[i][0] = ‘.‘;42     }43     while(EOF != scanf("%d %d",&n, &m)) {44         for(int i = 1; i <= n; i++) {45             for(int j = 1; j <= m; j++) {46                 scanf("\n%c",&mat[i][j]);47             }48         }49         int tot1 = 1; int tot2 = 1;50         memset(vis1, 0, sizeof(vis1)); memset(vis2, 0, sizeof(vis2));51         for(int i = 1; i <= n; i++) {52             for(int j = 1; j <= m; j++) {53                 if(mat[i][j] == ‘*‘) {54                     if(!vis1[i][j - 1]) {55                         vis1[i][j] = tot1++;56                     } else {57                         vis1[i][j] = vis1[i][j - 1];58                     }59                     if(!vis2[i - 1][j]) {60                         vis2[i][j] = tot2++;61                     } else {62                         vis2[i][j] = vis2[i - 1][j];63                     }64                 }65             }66         }67         memset(G, 0, sizeof(G));68         for(int i = 1; i <= n; i++) {69             for(int j = 1; j <= m; j++) {70                 if(mat[i][j] == ‘*‘) {71                     G[vis1[i][j]][vis2[i][j]] = 1;72                 }73             }74         }75         n = max(tot1, tot2);76         printf("%d\n",solve());77     }78     return 0;79 }
View code

 

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.