• 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): 1724    Accepted Submission(s): 719
    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
     
    Recommend
    LL   |   We have carefully selected several similar problems for you:  1498 1533 1083 3360 1530 
     

    好几天没更新了= =百度之星打击了几天

    题意:

    在n*m的盘子里放2*1的块,最多放多少块,输出放的位置。黑色的区域不能放。

    开始思路还是比较清晰的,用离散化坐标在进行二分匹配在输出结果,不过写完后没WA.因为离散化+构图的代码挺多,不想改,就参考别人的思路,于是用了别人的 不离散化的做法,效率有点低,有能力可以离散化一下应该会快很多。

    这个代码优点是比较短,而且好理解。

     1 //453MS    552K    1957 B    C++    
     2 #include<stdio.h>
     3 #include<string.h>
     4 #include<vector>
     5 #define N 105
     6 using namespace std;
     7 vector<int>V[N*N];
     8 int match[N*N];
     9 int vis[N*N];
    10 int g[N][N];
    11 int n,m;
    12 int dfs(int u)
    13 {
    14     for(int i=0;i<V[u].size();i++){
    15         int v=V[u][i];
    16         if(!vis[v]){
    17             vis[v]=1;
    18             if(match[v]==-1 || dfs(match[v])){
    19                 match[v]=u;
    20                 return 1;
    21             }            
    22         }
    23     }
    24     return 0;
    25 }
    26 int hungary()
    27 {
    28     int ret=0;
    29     memset(match,-1,sizeof(match));
    30     for(int i=1;i<=n*m;i++){
    31         memset(vis,0,sizeof(vis));
    32         ret+=dfs(i);
    33     }
    34     return ret;
    35 }
    36 int main(void)
    37 {
    38     int k,a,b;
    39     while(scanf("%d%d",&n,&m)!=EOF && (n+m))
    40     {
    41         scanf("%d",&k);
    42         for(int i=0;i<=n*m;i++) V[i].clear();
    43         memset(g,0,sizeof(g));
    44         while(k--){
    45             scanf("%d%d",&a,&b);
    46             g[a][b]=1;
    47         }
    48         for(int i=1;i<=n;i++)
    49             for(int j=1;j<=m;j++){
    50                 if(g[i][j]) continue;
    51                 if(i+1<=n && !g[i+1][j]){
    52                     V[(i-1)*m+j].push_back(i*m+j);
    53                     V[i*m+j].push_back((i-1)*m+j);
    54                 }
    55                 if(j+1<=m && !g[i][j+1]){
    56                     V[(i-1)*m+j].push_back((i-1)*m+j+1);
    57                     V[(i-1)*m+j+1].push_back((i-1)*m+j);
    58                 }
    59             }
    60         int ans=hungary()/2;
    61         printf("%d
    ",ans);
    62         memset(vis,0,sizeof(vis));
    63         for(int i=1;i<=n*m;i++){
    64             if(match[i]!=-1 && !vis[i] && !vis[match[i]]){
    65                 int y1=(i%m)==0?m:(i%m);
    66                 int x1=(i-y1)/m+1;
    67                 int y2=(match[i]%m)==0?m:(match[i]%m);
    68                 int x2=(match[i]-y2)/m+1;
    69                 vis[i]=vis[match[i]]=1;
    70                 printf("(%d,%d)--(%d,%d)
    ",x1,y1,x2,y2);                     
    71             }
    72         }
    73         printf("
    ");
    74     }
    75     return 0;
    76 } 
  • 相关阅读:
    将01字符串转换成数字的办法
    Codeforces Round #180 (Div. 2) AB
    CPU制作过程『转』
    向VECTOR的头部添加元素
    母版页中js操作问题
    操作粘贴板
    XML和关系数据使用XML和数据集类
    XML和关系数据用XML加载数据集
    XPath和XSL转换向XML应用XSL转换
    XML和关系数据从XSD架构创建数据集映射
  • 原文地址:https://www.cnblogs.com/GO-NO-1/p/3756946.html
Copyright © 2020-2023  润新知