zoukankan      html  css  js  c++  java
  • 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): 2013    Accepted Submission(s): 830
    Special Judge


    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 4 6 1 1 1 4 2 2 4 1 4 2 4 4 4 3 4 4 2 3 2 2 2 3 1 0 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)
     
    Source
     
    这道题,怎么说呢,比较的抽象,就是将一个点作为一个匹配的对象来处理,比较容易想到最大匹配,至于这个点的坐标该如何来存放
    可以开四维的数组,貌似这样会到10e8+的数据规模 达到250M的样子,所以放弃这个念头...转而用结构体来定义自己的变量
    然后就是一个一如既往的匈牙利算法啦,但是在遍历所有的坐标时,虽然可以求出正确的匹配数,貌似在输出那个谁匹配谁的时候,又不好处理
    比如  你在 (1,2)这个点的时候,假如你和(1,3)匹配了,那么在(1,3)那个点时,又会有(1,3)匹配(1,2)这样就不好处里了...
    于是呼,你会发现这个地方的匹配有些奇特,那就是貌似他临近的点的坐标的和总和所在点呈现奇偶互异的状态,所以我们不妨这样搞,取这个点就不在取那个点
    就这样跳跃着,取点,然后就可以了....这样的话,一切就都ok了....
    代码:
     1 #include<cstring>
     2 #include<cstdio>
     3 #include<cstdlib>
     4 #define init(a) memset(a,0,sizeof(a))
     5 const int maxn=105;
     6 bool map[maxn][maxn];
     7 bool vis[maxn][maxn];
     8 struct node{
     9   int x,y;
    10 };
    11 node mat[maxn][maxn];
    12 int n,m;
    13 int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
    14 int match(node aa){
    15 
    16       node tem;
    17      if(!map[aa.x][aa.y]){
    18       for(int ll=0;ll<4;ll++)  //扫描它的四周...
    19       {
    20         tem=(node){aa.x+dir[ll][0],aa.y+dir[ll][1]};
    21         if(tem.y>0&&tem.x>0&&tem.x<=n&&tem.y<=m&&!map[tem.x][tem.y]&&!vis[tem.x][tem.y]){
    22              vis[tem.x][tem.y]=1;
    23              if(!mat[tem.x][tem.y].x||match(mat[tem.x][tem.y])){
    24               mat[tem.x][tem.y]=aa;
    25               return 1;
    26             }
    27           }
    28         }
    29        }
    30  return 0;
    31 }
    32 int main()
    33 {
    34     int k,x,y;
    35     //freopen("test.in","r",stdin);
    36     while(scanf("%d%d",&n,&m)!=EOF,n+m){
    37      scanf("%d",&k);
    38      init(map);
    39     while(k--){
    40          scanf("%d%d",&x,&y);
    41           map[x][y]=1;  //代表一个池塘
    42     }
    43     init(mat);
    44     int ans=0;
    45     for(int i=1;i<=n;i++){
    46    for(int j=1;j<=m;j++){
    47       if(((i+j)&1)){  //不娶相邻的点...取奇数或者偶数都一样,,
    48       init(vis);
    49       ans+=match((node){i,j});
    50       }
    51    }
    52    }
    53    printf("%d
    ",ans);
    54       for(int i=1;i<=n;i++){
    55    for(int j=1;j<=m;j++){
    56        if(mat[i][j].x){
    57            printf("(%d,%d)--(%d,%d)
    ",i,j,mat[i][j].x,mat[i][j].y);
    58        }
    59    }
    60    }
    61   }
    62   return 0;
    63 }
  • 相关阅读:
    01:求平均年龄
    09:与圆相关的计算
    08:温度表达转化
    07:计算多项式的值
    06:甲流疫情死亡率
    05:计算分数的浮点数值
    04:带余除法
    03:计算(a+b)/c的值
    02:计算(a+b)*c的值
    01:A+B问题
  • 原文地址:https://www.cnblogs.com/gongxijun/p/3929630.html
Copyright © 2011-2022 走看看