Question link: http://poj.org/problem? Id = 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.
Source
Usaco 2005 January gold
Farmer John's cattle farm is a rectangle in Column C of column R. After heavy rain, water is accumulated in the low-lying areas of the cattle farm. John's cows are very delicate. When they eat grass, they don't want to dirty their hooves. To prevent the cows from getting their shoot dirty, John decided to put the place with water on the plank. His plank is 1 in width and has no length limit.
He wanted to cover all the low-lying areas with water with the fewest purpose boards, provided that the boards could not cover the grass, but could overlap.
The composition of this question is indeed more clever. If there is a continuous low-lying area, assuming it is horizontal, then these consecutive low-lying areas can be covered by a board. Similarly, if the vertical bars have continuous low-lying areas, you can also use a board to cover them. In this way, when a low-lying area can take both the horizontal board and the vertical board cover, it is the intersection. Then this line represents a low-lying place, which can be either horizontally or vertically covered. Now we take all the low-lying areas of the horizontal row as left (continuous low-lying areas as one board), and the same is true for vertical positions. The following table is used as an example:
Sample:
4
*.*.
.***
***.
..*.
Concatenate the pits in the rows as a point, that is, a horizontal board with serial numbers. The sample is converted:
1 0 2 0
0 3 3 3
4 4 4 0
0 0 5 0
Add these sequence numbers to the X set and then perform the following operations by column:
1 0 4 0
0 3 4 5
2 3 4 0
0 0 4 0
Similarly, if you add the y set, one pit can only be covered by a horizontal or vertical wooden board, and different serial numbers are marked for the original image pit. There are nine pits in total.
1. 2.
. 3 4 5
6 7 8.
... 9.
In the figure, the No. 2 low-lying area can be either a horizontal plate 2 or a vertical plate 4, so there is an edge between plate 2 and Board 4, the side actually represents a low-lying area. For example, the side of No. 2 low-lying area is {2, 4}. It can be understood that No. 2 low-lying area can be covered by plate 2 or plate 4. Connect all edges with the least vertices, which means the least vertices are covered.
From: http://hi.baidu.com/onlys_c/blog/item/9781e0dd858f2fd28d102919.html
The Code is as follows:
#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>using namespace std;const int MAXN = 1017;int LN, RN;int g[MAXN][MAXN];char s[MAXN][MAXN];int U[MAXN][MAXN], V[MAXN][MAXN];int linker[MAXN];bool used[MAXN];int dfs(int L){ for(int R = 1; R <= RN; R++) { if(g[L][R] && !used[R]) { used[R] = true; if(linker[R]==-1 || dfs(linker[R])) { linker[R] = L; return 1; } } } return 0;}int hungary(){ int res = 0; memset(linker,-1,sizeof(linker)); for(int L = 1; L <= LN; L++) { memset(used,false,sizeof(used)); if(dfs(L)) res++; } return res;}int main(){ int R, C; while(~scanf("%d%d",&R,&C)) { int u = 0, v = 0; memset(g,0,sizeof(g)); for(int i = 1; i <= R; i++) { for(int j = 1; j <= C; j++) { cin>>s[i][j]; } } for(int i = 1; i <= R; i++) { for(int j = 1; j <= C; j++) { if(s[i][j] == '*') { u++; while(j <= C && s[i][j] == '*') { U[i][j] = u; j++; } } } } for(int j = 1; j <= C; j++) { for(int i = 1; i <= R; i++) { if(s[i][j] == '*') { v++; while(i <= R && s[i][j] == '*') { V[i][j] = v; i++; } } } } for(int i = 1; i <= R; i++) { for(int j = 1; j <= C; j++) { if(s[i][j] == '*') { g[U[i][j]][V[i][j]] = 1; } } } LN = u, RN = v; int ans = hungary(); printf("%d\n",ans); } return 0;}
Poj 2226 muddy fields (bipartite matching clever graph creation)