zoukankan      html  css  js  c++  java
  • 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): 203 Accepted Submission(s): 132
     
    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
    South America 2002 - Practice
     
    Recommend
    LL
     
    /*
    题意:给你一个n*m的方格矩阵,然后有k个方格内有鱼塘,给出你k个鱼塘的坐标,剩下的方格用1*2的小方格填满,问你最多能填多少个
    
    初步思路:对剩下的小方格进行二分匹配,只有两个相邻的小方格才存在联系,能填满的最大数,就是二分的最大匹配数;
    #超内存  因为开标记数组的时候把鱼塘也考虑进去了,所以开了一个10000*10000的数组,太大
    
    改进:总共空格子最多有50个,只需要开一个50*50的数组就可以
    */
    #include<bits/stdc++.h>
    #define N 110
    using namespace std;
    int mapn[N][N];
    int vis[N][N];
    int vis2[N][N];
    int n,m,k;
    int x,y;
    int dir[4][2]={{1,0},{-1,0},{0,-1},{0,1}};
    struct node{
        int x,y;
        node(){}
        node(int a,int b){
            x=a;
            y=b;
        }
    };
    vector<node>point;
    /***********************二分匹配模板**************************/
    const int MAXN=110;
    int uN,vN;  //u,v数目
    int g[55][55];//编号是0~n-1的 
    int linker[MAXN];//记录匹配点i的匹配点是谁
    bool used[MAXN];
    bool dfs(int u)//回溯看能不能通过分手来进行匹配
    {
        int v;
        for(v=0;v<point.size();v++)
            if(g[u][v]&&!used[v])
            //如果有这条边,并且这条边没有用过
            {
                used[v]=true;
                if(linker[v]==-1||dfs(linker[v]))//如果这个点没有匹配过,并且能找到匹配点,那么就可以以这个边作为匹配点
                {
                    linker[v]=u;
                    return true;
                }    
            }  
        return false;
    }    
    int hungary()//返回最大匹配数
    {
        int res=0;
        int u;
        memset(linker,-1,sizeof(linker));
        for(u=0;u<point.size();u++)
        {
            memset(used,0,sizeof(used));
            if(dfs(u))//如果这个点有匹配点 
                res++;
        } 
        return res;   
    }
    /***********************二分匹配模板**************************/
    void init(){
        memset(g,0,sizeof g);
        memset(mapn,0,sizeof mapn);
        memset(vis,0,sizeof vis);
        memset(vis2,0,sizeof vis2);
        point.clear();
    }
    bool ok(int x,int y){
        if(x<1||x>n||y<1||y>m||mapn[x][y]) return true;
        return false;
    }
    void match(){//将所有的小方格进行编号
        
        for(int i=1;i<=n;i++){//将所有的空方格存进vector中
            for(int j=1;j<=m;j++){
                //cout<<mapn[i][j]<<" ";
                if(mapn[i][j]==0)
                {
                    point.push_back(node(i,j));
                    vis2[i][j]=point.size()-1;//记录这是第几个
                    //cout<<i<<" "<<j<<endl;
                }    
            }
            //cout<<endl;
        }
        //cout<<"point.size()="<<point.size()<<endl;
        for(int i=0;i<point.size();i++){
            if(vis[point[i].x][point[i].y]==0){
                vis[point[i].x][point[i].y]=1;
                for(int k=0;k<4;k++){
                    int fx=point[i].x+dir[k][0];
                    int fy=point[i].y+dir[k][1];
                    if(ok(fx,fy)) continue;
                    g[i][vis2[fx][fy]]=1;
                    vis[fx][fy]=1;
                }
            }
        }
        // for(int i=0;i<point.size();i++){
            // for(int j=0;j<point.size();j++){
                // cout<<g[i][j]<<" ";
            // }
            // cout<<endl;
        // }
    }
    int main(){
        //freopen("in.txt","r",stdin);
        while(scanf("%d%d",&n,&m)!=EOF&&(n+m)){
            init();//初始化
            scanf("%d",&k);
            while(k--){
                scanf("%d%d",&x,&y);
                mapn[x][y]=1;
            }//标记鱼塘的位置
            // for(int i=1;i<=n;i++){
                // for(int j=1;j<=m;j++){
                    // cout<<mapn[i][j]<<" ";
                // }
                // cout<<endl;
            // }
            match();//进行空格的匹配
            int cur=hungary();
            printf("%d
    ",cur);
            for(int i=0;i<point.size();i++){
                if(linker[i]!=-1){
                    printf("(%d,%d)--(%d,%d)
    ",point[i].x,point[i].y,point[linker[i]].x,point[linker[i]].y);
                }
            }
            printf("
    ");
        }
        return 0;
    }
  • 相关阅读:
    git的相关命令
    vue资料链接
    自定义小程序底部菜单
    物联网开发日记四:原理图2
    物联网开发日记三:原理图1
    物联网开发日记二:设计系统结构
    物联网开发日记一:准备工作
    mybatis不使用@Param有人报错有人却不报错问题
    最简单的js包装对象、ajax请求
    java文字转拼音、首字母缩写
  • 原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/6202270.html
Copyright © 2011-2022 走看看