首页 > 代码库 > HDU 1507 Uncle Tom's Inherited Land*

HDU 1507 Uncle Tom's Inherited Land*

 

Uncle Tom‘s Inherited Land*

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 148 Accepted Submission(s): 97
 
Problem Description
Your old uncle Tom inherited a piece of land from his great-great-uncle. Originally, the property had been in the shape of a rectangle. A long time ago, however, his 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 be sure, for you have not been to the place, but he may have made so many ponds that the land may now consist of several disconnected islands.)
Your uncle Tom wants to sell the inherited land, but local rules now regulate property sales. Your uncle has been informed that, at his great-great-uncle‘s request, a law has been passed which establishes that property can only be sold in rectangular lots the size of two squares of your uncle‘s property. Furthermore, ponds are not salable property.
Your uncle asked your help to determine the largest number of properties he could sell (the remaining squares will become recreational parks). 
 
Input
Input will include several test cases. The first line of a test case contains two integers N and M, representing, respectively, the number of rows and columns of the land (1 <= N, M <= 100). The second line will contain an integer K indicating the number of squares that have been turned into ponds ( (N x M) - K <= 50). Each of the next K lines contains two 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.
 
Output
            For 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 are 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,2)--(1,3)(2,1)--(3,1)(2,3)--(3,3)(2,4)--(3,4)3(1,1)--(2,1)(1,2)--(1,3)(2,3)--(3,3)

 

这条是HDU比较老的,也比较经典的题目.

题目意思就是要求一些 土地能够与相邻的土地匹配,然后求一个最大匹配数 .

关键就在于把一个矩阵分为 x ,y 两个集合 。 然后一个点与相邻4个点所在集合不同,

其实就像国际象棋棋盘一样分为黑白,这样就就不会出现重边了。

构图的时候用数字表示一下点,能通过点查出数字,然后也能够通过数字找回点。

最后求一次匈牙利就行了。

 

 

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;typedef long long LL;const int N = 210;int lx[N];bool vis[N];int n,d,m;int g[N][N] , mp[N][N] , id[N][N] ,num[N][N];int ID , tot ;struct node{    int x,y;}ans[55];bool dfs( int u ){    for(int v =1 ;v <= ID ;++v ){        if(!vis[v] && g[u][v]){            vis[v] = 1;            if( lx[v] == -1 || dfs(lx[v]) ){                lx[v] = u ;                return true;            }        }    }    return false;}int solve(){    int res=0 ;    memset( lx , -1 ,sizeof lx );    for( int i =1 ; i<= ID ;++i){        memset( vis , false , sizeof vis );        if( dfs( i ) ) res ++;    }    return res;}int main(){    #ifdef LOCAL        freopen("in.txt","r",stdin);    #endif    ios::sync_with_stdio(0);    int cas = 1 ,x , y ;    for(int i =1 ; i <= 100 ; ++i ){        for(int j = 1; j <=100 ;++j ){            if( num[i][j]  ) {                num[i-1][j] = num[i+1][j] = num[i][j-1] = num[i][j+1] = 0;            }            else {                num[i-1][j] = num[i+1][j] = num[i][j-1] = num[i][j+1] = 1;            }        }    }    while(~scanf("%d%d",&n,&m)){        if(!n)break;        memset( g, 0 , sizeof g );        memset( mp , 0 , sizeof mp );        ID = 0 ;        scanf("%d",&d);        while(d--){            scanf("%d%d",&x,&y);            mp[x][y] = 1;        }        for(int i = 1; i <= n ; ++i){            for(int j =1 ; j<= m ;++j ){                if( mp[i][j] )continue;                id[i][j] = ++ID;                ans[ID].x = i , ans[ID].y = j;            }        }        for(int i =1 ; i<= n ;++i ){            for(int j =1 ; j <= m ;++j){                if( !mp[i][j] && num[i][j] ){                    if( i > 1 && !mp[i-1][j] ) g[id[i][j]][id[i-1][j]] =1 ;                    if( i < n && !mp[i+1][j] ) g[id[i][j]][id[i+1][j]] =1 ;                    if( j > 1 && !mp[i][j-1] ) g[id[i][j]][id[i][j-1]] =1 ;                    if( j < m && !mp[i][j+1] ) g[id[i][j]][id[i][j+1]] =1 ;                }            }        }        printf("%d\n",solve());        for(int i =1 ; i <= ID ; ++i ){            if(lx[i] == -1 )continue;            printf("(%d,%d)--(%d,%d)\n",ans[i].x,ans[i].y,ans[lx[i]].x, ans[lx[i]].y);        }    }    return  0;}

 

HDU 1507 Uncle Tom's Inherited Land*