Description
Rain has pummeled the cows'field, a rectangular grid of r rows and C columns (1 <= R <=, 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 toGetTheir hooves dirty whilethey eat. To prevent those muddy hooves, Farmer John would place a number of wooden boards over the muddy parts of the cows'field. Each of the boards are 1 unit wide, and can be any length long. Each board must is 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 than one board to cover. The boards may not be 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 mudinchThe field.
Input
1: Both space-2. r+1string'*'. ' representing a grassy patch. No spaces is present.
Output
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
To cover with a plank, the same row or column of ' * ' can be covered with a piece of wood, '. ' cannot be overwritten. How many planks can be used at least to cover all the ' * '?
The planks can only cover the continuous horizontal mud and the vertical mud, and the grass will be separated in the middle.
Problem-solving ideas: A two-point matching classic composition topic
Composition ideas:
A set of horizontal planks and points that are seen as one side, and a set of points on the other side of a vertical plank, if they intersect at a point
If you want to cover all the mud, and the minimum required planks, then the minimum point of coverage
So use Hungary to find the maximum number of matches
The composition of the code to look again!
1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 #defineN 5606 intn,m;7 8 CharMp[n][n];9 intCnt[n][n];Ten intCnt1[n][n]; One intFina[n][n]; A intMatch[n]; - intVis[n]; - intTmp,tmp1; the BOOLDfsintx) { - for(intI=1; i<=tmp1;i++){ - if(!vis[i] &&Fina[x][i]) { -vis[i]=1; + if(match[i]==-1||DFS (Match[i])) { -match[i]=x; + return true; A } at } - } - return false; - } - voidsolve () { -memset (match,-1,sizeof(Match)); in intans=0; - for(intI=1; i<=tmp;i++){ tomemset (Vis,0,sizeof(Vis)); + if(Dfs (i)) { -ans++; the } * } $printf"%d\n", ans);Panax Notoginseng - } the intMain () + { A while(SCANF ("%d%d", &n,&m) = =2){ theMemset (MP,0,sizeof(MP)); + for(intI=0; i<n;i++){ -scanf"%s", Mp[i]); $ } $memset (cnt,-1,sizeof(CNT)); -memset (cnt1,-1,sizeof(Cnt1)); -tmp=0; the for(intI=0; i<n;i++){ - intflag=0;Wuyi for(intj=0; j<m;j++){ the - if(flag==0&& mp[i][j]=='*'){ Wuflag=1; -cnt[i][j]=++tmp; About } $ Else if(flag==1&& mp[i][j]=='*'){ -cnt[i][j]=tmp; - } - Else if(mp[i][j]=='.'){ Aflag=0; + } the } - } $ thetmp1=0; the for(intj=0; j<m;j++){ the intflag=0; the for(intI=0; i<n;i++){ - if(flag==0&& mp[i][j]=='*'){ inflag=1; thecnt1[i][j]=++Tmp1; the } About Else if(flag==1&& mp[i][j]=='*'){ thecnt1[i][j]=Tmp1; the } the Else if(mp[i][j]=='.'){ +flag=0; - } the }Bayi } thememset (Fina,0,sizeof(FINA)); the for(intI=0; i<n;i++){ - for(intj=0; j<m;j++){ - if(cnt[i][j]!=-1&& cnt1[i][j]!=-1){ thefina[cnt[i][j]][cnt1[i][j]]=1; the } the } the } - the solve (); the } the return 0;94}View Code
POJ 2226 Muddy Fields (minimum point overlay + ingenious composition)