zoukankan      html  css  js  c++  java
  • HDU 1507 Uncle Tom's Inherited Land*

    Uncle Tom's Inherited Land*

    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)
     
    #include<cstdio>
    #include<cstring>
    #include<vector>
    #include<algorithm>
    #define pb push_back
    using namespace std;
    const int d[4][2]={1,0,0,1,-1,0,0,-1};
    int n,m,k,link[100005],vis[100005];
    bool mp[105][105];
    bool dfs(int cur)
    {
        for(int i=0;i<4;i++)
        {
            int x=cur/m+d[i][0],y=cur%m+d[i][1];
            if(x<0||y<0||x>=n||y>=m)continue;
            int v=x*m+y;
            if(!vis[v]&&!mp[x][y])
            {
                vis[v]=1;
                if(link[v]==-1||dfs(link[v]))
                {
                    link[v]=cur;
                    return 1;
                }
            }
        }
        return 0;
    }
    int match()
    {
        int ans=0;
        memset(link,-1,sizeof(link));
        for(int i=0;i<n*m;i++)
        {
            int x=i/m,y=i%m;
            if((x+y)&1&&!mp[x][y])
            {
                memset(vis,0,sizeof(vis));
                if(dfs(i))ans++;
            }
        }
        return ans;
    }
    int main()
    {
        while(scanf("%d%d",&n,&m),n|m)
        {
            scanf("%d",&k);
            memset(mp,0,sizeof(mp));
            while(k--)
            {
                int x,y;
                scanf("%d%d",&x,&y);
                mp[--x][--y]=1;
            }
            printf("%d
    ",match());
            for(int i=0;i<n*m;i++)
                    if(link[i]!=-1)
                        printf("(%d,%d)--(%d,%d)
    ",link[i]/m+1,link[i]%m+1,i/m+1,i%m+1);
        }
        return 0;
    }
  • 相关阅读:
    P2216-[HAOI2007]理想的正方形
    P2157-[SDOI2009]学校食堂
    Leetcode-5176 Number of Valid Words for Each Puzzle(猜字谜)
    Leetcode-5175 Can Make Palindrome from Substring(构建回文串检测)
    Leetcode-5174 Diet Plan Performance(健身计划评估)
    Task6.PyTorch理解更多神经网络优化方法
    Task5.PyTorch实现L1,L2正则化以及Dropout
    Task4.用PyTorch实现多层网络
    Task3.PyTorch实现Logistic regression
    Task2.设立计算图并自动计算
  • 原文地址:https://www.cnblogs.com/homura/p/5875451.html
Copyright © 2011-2022 走看看