HDU 4185--oil skimming —————— "maximum matching, parity and parity of squares"

Source: Internet
Author: User

Oil skimmingTime limit:1000MS Memory Limit:32768KB 64bit IO Format:%i64d &%i64 U SubmitStatusPracticeHDU 4185

Description

Thanks to a certain "green" resources company, there are a new profitable industry of oil skimming. There is large slicks of crude oil floating in the Gulf of Mexico just waiting to being scooped up by enterprising oil baron S. One such oil baron have a special plane that can skim the surface of the water collecting oil on the water ' s surface. However, each scoop covers a 10m by 20m rectangle (going either east/west or North/south). It also requires that the rectangle was completely covered in oil, otherwise the product was contaminated by pure ocean wate R and thus unprofitable! Given a map of the oil slick, the oil baron would like you to compute the maximum number of scoops, is may extracted. The map is an NxN grid where each cell represents a 10m square of water, and each cell is marked as either being covered I n Oil or pure water.

Input

The input starts with an integer k (1 <= k <=) indicating the number of cases. Each case is starts with an integer n (1 <= n <=) indicating the size of the square grid. Each of the following n lines contains n characters this represent the cells of a row in the grid. A character of ' # ' represents an oily cell, and a character of '. ' Represents a pure water cell.

Output

One line should be produced, formatted exactly as follows: ' Case X:m ' where X is the case number (starting from 1) and M are the maximum number of scoops of oil which may be extracted.

Sample Input

1 6.......##....##.......#.....## ...

Sample Output

Case 1:3 topic: abstract model. Use the "#" in the 1*2 's strip overlay to overwrite and change to "." Cannot overwrite to ".". Ask you how many times you need to overwrite. Problem solving: Minimum vertex overlay: With the fewest points, make at least one end of each edge (selected). We can divide two parts according to the parity of the lattice. If there is a "#" in the adjacent position, an edge is attached. Finally, the maximum match can be obtained.
#include <stdio.h> #include <string.h> #include <math.h> #include <queue> #include <vector > #include <algorithm>using namespace std;const int maxn = 660;const int INF = 0X3F3F3F3F;VECTOR&LT;INT&GT;G[MAXN ];int MX[MAXN], MY[MAXN], DX[MAXN], DY[MAXN], USED[MAXN], Dis;char map[maxn][maxn];int lis[maxn][maxn];bool SearchP (int    _n) {queue<int>q;    memset (dx,-1,sizeof (DX));    memset (dy,-1,sizeof (dy));    int dis = INF;            for (int i = 1; I <= _n; i++) {if (mx[i] = = 1) {Dx[i] = 0;        Q.push (i);    }} int v; while (! Q.empty ()) {int u = q.front ();        Q.pop ();        if (Dx[u] > dis) break;            for (int i = 0; i < g[u].size (); i++) {v = g[u][i];                if (dy[v] = = 1) {Dy[v] = Dx[u] + 1;                if (my[v] = =-1) {dis = dy[v];                    }else{Dx[my[v]] = Dy[v] + 1;                Q.push (My[v]);      }            }  }} return dis! = INF;}    int dfs (int u) {int V;        for (int i = 0; i < g[u].size (); i++) {v = g[u][i];            if (!used[v] && dy[v] = = Dx[u] + 1) {used[v] = 1;            if (my[v]! =-1 && dy[v] = = dis) {continue;                } if (my[v] = = 1 | | DFS (MY[V])) {Mx[u] = V;                MY[V] = u;            return true; }}} return false;}    int maxmatch (int ln,int rn) {int ret = 0;    memset (mx,-1,sizeof (Mx));    memset (my,-1,sizeof (My));        while (SEARCHP (LN)) {memset (used,0,sizeof (used));            for (int i = 1; i <= ln; i++) {if (mx[i] = = 1 && dfs (i)) {ret++; }}} return ret;}    int main () {int T, cas = 0, n, m, N;    scanf ("%d", &t);        while (t--) {n = m = 0;        scanf ("%d", &n);        int N2 = N*n;        for (int i = 0; I <= N2; i++) {g[i].clear (); } for (int i= 0; I <= n+1;            i++) {map[i][0] = '. ';            Map[0][i] = '. ';            Map[n+1][i] = '. ';        Map[i][n+1] = '. ';            } for (int i = 1; i<= N; i++) {GetChar ();                for (int j = 1; J <= N; j + +) {scanf ("%c", &map[i][j]);                        if (map[i][j] = = ' # ') {if ((i+j)%2 = = 0) {++n;                        LIS[I][J] = n;                        if (map[i-1][j] = = ' # ') {G[n].push_back (lis[i-1][j]);                        } if (map[i][j-1] = = ' # ') {G[n].push_back (lis[i][j-1]);                        }}else{++m;                        Lis[i][j] = m;                        if (map[i-1][j] = = ' # ') {g[lis[i-1][j]].push_back (M); } if (map[i][j-1] = = ' # ') {G[lis[i][j-1]].Push_back (m);        }}}}} int res = Maxmatch (n,m);    printf ("Case%d:%d\n", ++cas,res); } return 0;} /*4.##...#. ###.... 4.##...#. ###.. #.4.##...#. ###...#*/

  

HDU 4185--oil skimming —————— "maximum matching, parity and parity of squares"

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.