Muddy fields
Time limit:1000 ms |
|
Memory limit:65536 K |
Total submissions:3483 |
|
Accepted:1299 |
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
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
67 8.
... 9.
For example, the No. 7 pit can be covered by the horizontal No. 4 Board and the vertical No. 3 board, and the corresponding horizontal boards (4) and vertical boards (3) of each point can be covered) if an edge is connected in the middle, the problem is to find as few sides as possible to cover these points. According to the XXXXX theorem, the maximum number of matching is obtained.
# Include <stdio. h>
# Include <string. h>
Int R, C;
Char STR [51] [51];
Int X [51] [51], Y [51] [51];
Int mat [1255] [1255];
Int link [1255];
Bool usedif [1255];
Int numb, numa;
Bool can (int t)
{
For (INT I = 1; I <= numb; I ++)
If (usedif [I] = 0 & mat [T] [I] = 1)
{
Usedif [I] = 1;
If (link [I] =-1 | can (link [I])
{
Link [I] = T;
Return true;
}
}
Return false;
}
Int maxmatch ()
{
Int num = 0;
Memset (link,-1, sizeof (Link ));
For (INT I = 1; I <= NUMA; I ++)
{
Memset (usedif, 0, sizeof (usedif ));
If (CAN (I) num ++;
}
Return num;
}
Int main ()
{
While (scanf ("% d", & R, & C )! = EOF)
{
For (INT I = 0; I <r; I ++) scanf ("% s", STR [I]);
NUMA = 0;
For (INT I = 0; I <r; I ++)
For (Int J = 0; j <C; j ++)
{
If (j = 0)
{
If (STR [I] [J] = '*') x [I] [J] = ++ NUMA;
}
Else
{
If (STR [I] [J] = '*')
{
If (STR [I] [J] = STR [I] [J-1]) x [I] [J] = NUMA;
Else X [I] [J] = ++ NUMA;
}
}
}
Numb = 0;
For (Int J = 0; j <C; j ++)
For (INT I = 0; I <r; I ++)
{
If (I = 0)
{
If (STR [I] [J] = '*') Y [I] [J] = ++ numb;
}
Else
{
If (STR [I] [J] = '*')
{
If (STR [I] [J] = STR [I-1] [J]) Y [I] [J] = numb;
Else y [I] [J] = ++ numb;
}
}
}
Memset (MAT, 0, sizeof (MAT ));
For (INT I = 0; I <r; I ++)
For (Int J = 0; j <C; j ++)
{
Mat [x [I] [J] [Y [I] [J] = 1;
}
Printf ("% d/N", maxmatch ());
}
Return 0;
}