POJ-2226-Muddy (Fields)

Source: Internet
Author: User

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 ;}

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.