POJ-2226-Muddy (Fields)
Http://poj.org/problem? Id = 2226
A piece of wood can cover a row or a column of land. The wood blocks are laid by row and then by column.
*. *. By Row 1 0 2 0 by column 1 0 4 0
. *** 0 3 3 3 0 3 4 5
* **. 4 4 4 0 2 3 4 0
.. *. 0 0 5 0 0 4 0
Concatenate an edge for each * row and column to form a bipartite graph. The question is transformed into a minimum vertex overwrite, that is, the maximum bipartite match.
# Include <cstdio> # include <cstring> # include <cstdlib> using namespace std; int n1, n2; char map [1005] [1005]; // open the array to a greater value, otherwise, WAint mapx [1005] [1005], mapy [1005] [1005]; int ma [1005] [1005]; int result [1005], visit [1005]; int x, y; int find (int a) {int I; for (I = 1; I <= y; I ++) {if (! Visit [I] & ma [a] [I]) {visit [I] = 1; if (! Result [I] | find (result [I]) {result [I] = a; return 1 ;}} return 0 ;}int main () {int I, j, ans; while (scanf ("% d", & n1, & n2 )! = EOF) {for (I = 0; I <n1; I ++) scanf ("% s", map [I]); memset (mapx, 0, sizeof (mapx); memset (mapy, 0, sizeof (mapy); x = 0; for (I = 0; I <n1; I ++) for (j = 0; j <n2; j ++) if (map [I] [j] = '*') {++ x; while (j <n2 & map [I] [j] = '*') {mapx [I] [j] = x; j ++ ;}} y = 0; for (j = 0; j <n2; j ++) for (I = 0; I <n1; I ++) if (map [I] [j] = '*') {++ y; while (I <n1 & map [I] [j] = '*') {mapy [I] [j] = y; I ++ ;}} for (I = 0; I <n1; I ++) for (j = 0; j <n2; j ++) ma [mapx [I] [j] [mapy [I] [j] = 1; ans = 0; memset (result, 0, sizeof (result); for (I = 1; I <= x; I ++) {memset (visit, 0, sizeof (visit )); ans + = find (I);} printf ("% d \ n", ans);} return 0 ;}