Hdu_1507_uncle Tom's inherited land* (binary image matching)

Source: Internet
Author: User

Uncle Tom ' s inherited land* Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 2208 Accepted Submission (s): 913
Special Judge


Problem descriptionyour old Uncle Tom inherited a piece of the land from his great-great-uncle. Originally, the property had been in the shape of a rectangle. A long time ago, however, he great-great-uncle decided to divide the land into a grid of small squares. He turned some of the squares into ponds, for he loved to hunt ducks and wanted to attract them to his property. (You cannot is sure, for you has not been to the place, but he may has made so many ponds then the land could now consist of several disconnected islands.)

Your Uncle Tom wants to sell the inherited land, but local rules now regulate property sales. Your uncle had been informed that, at his great-great-uncle's request, a law had been passed which establishes that proper Ty can only is sold in rectangular lots the size of the squares of your uncle ' s property. Furthermore, ponds is not the Salable property.

Your Uncle asked Your help to determine the largest number of properties he could sell (the remaining squares would become Recreational parks).


Inputinput would include several test cases. The first line of a test case contains, integers N and M, representing, respectively, the number of rows and columns of The Land (1 <= N, M <= 100). The second line would contain an integer k indicating the number of squares that has been turned into ponds ((N x M)-K <= 50). Each of the next K lines contains, integers X and Y describing the position of a square which was turned into a pond (1 <= X <= N and 1 <= Y <= M). The end of input is indicated by N = M = 0.

Outputfor each test case in the input your program should first output one line, containing an integer p representing the Maximum number of properties which can be sold. The next P lines specify each pair of squares which can be sold simultaneity. If there is more than one solution, anyone is acceptable. There is a blank line after each test case. See sample below for clarification of the output format.

Sample Input
4 461 11 42 24 14 24 44 344 23 22 23 10 0

Sample Output
4 (1,3)-(2,1)-(3,1) (2,3)-(3,3) (2,4)-(3,4) 3 (a)-(2,1) (+)-(1,3) (2,3)-(3,3)

Test instructions: To give you a n*m matrix, some of the dots are not available, and now ask you to use the 1*2 square can put up how much, can not overlap, and output each pair of grid coordinates.
Analysis: This problem is very obvious is the two-figure maximum matching problem, think of the adjacent two squares with a side, but this way, each square there are two sides. Think for a long time ah just don't know how to deal with, Konjac Konjac said not to think of ways can only see the solution. In the construction of the map as long as according to the sum of the parity of the map is good, this way there will not be two sides of each pair of squares. From this topic learned the parity of the building, worth a deep understanding.
Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=1507
Code Listing:
#include <map> #include <cmath> #include <queue> #include <stack> #include <cstdio># include<string> #include <cstring> #include <iostream> #include <algorithm>using namespace std    ; Const int MAXV = + 5;struct edge{int x; int y;} Edge[maxv];int n,m;int k,x,y;int cnt,sum;int match[maxv];bool vis[maxv];int num[maxv][maxv];bool GRAPH[MAXV][MAXV];    BOOL Truegraph[maxv][maxv];void init () {cnt=0; sum=0;    memset (num,0,sizeof (num));    memset (match,-1,sizeof (match));    memset (graph,true,sizeof (graph)); memset (truegraph,false,sizeof (Truegraph));}    void getgraph (int x,int y) {if (x-1>=1&&graph[x-1][y]) truegraph[num[x][y]][num[x-1][y]]=true;    if (X+1<=n&&graph[x+1][y]) truegraph[num[x][y]][num[x+1][y]]=true;    if (y-1>=1&&graph[x][y-1]) truegraph[num[x][y]][num[x][y-1]]=true; if (y+1<=m&&graph[x][y+1]) truegraph[num[x][y]][num[x][y+1]]=true;} void Mem () {for (int. i=1;i<=n;i++) {for (int) j=1;j<=m;j++) {if (Graph[i][j]) {num[i][j]=++cnt;                Edge[cnt].x=i;            Edge[cnt].y=j; }}} for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) if (graph[i][j]&& (I+J) & 1)) Getgraph (I,J);}            bool Dfs (int u) {for (int v=1;v<=cnt;v++) {if (!vis[v]&&truegraph[u][v]) {vis[v]=true;                if (Match[v]==-1 | | dfs (MATCH[V])) {match[v]=u;            return true; }}}return false;}        void Getmap () {for (int u=1;u<=cnt;u++) {memset (vis,false,sizeof (VIS));    if (DFS (U)) sum++;    }}void print () {printf ("%d\n", sum);        for (int i=1;i<=cnt;i++) {int j=match[i];        if (match[i]!=-1) {printf ("(%d,%d)-(%d,%d) \ n", edge[i].x,edge[i].y,edge[j].x,edge[j].y);    }}}void doit () {mem ();    Getmap (); Print ();} int main () {while (scanf ("%d%d", &n,&m)!=eof) {if (n==0&&amP        m==0) break;        Init ();        scanf ("%d", &k);            for (int i=0;i<k;i++) {scanf ("%d%d", &x,&y);        Graph[x][y]=false;    } doit (); }return 0;}


Hdu_1507_uncle Tom's inherited land* (binary image matching)

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.