HDU 1507 Uncle Tom's Inherited Land*(二分图匹配+奇偶建图)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37943488/article/details/82315493

Uncle Tom's Inherited Land*

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4036    Accepted Submission(s): 1686
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)

题目大意:有一个n*m的棋盘,有几个格子不能放东西,问用1×2或2×1的牌能覆盖多少个格子,输出这些匹配的格子

奇偶建图,跑最大匹配。毒瘤题,要开大空间才能过

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=10010;
const int maxm=400010;
struct Node
{
    int to;
    int next;
}edge[maxm];
int cnt;
int n,m;
int head[1000010];
int map[1010][1010];
bool vis[maxn];
int match[1000010];
int dirx[]={0,1,0,-1};
int diry[]={1,0,-1,0};
void init()
{
    cnt=0;
    memset(head,-1,sizeof(head));
    memset(map,0,sizeof(map));
    return;
}
void add(int u,int v)
{
    edge[cnt].to=v;
    edge[cnt].next=head[u];
    head[u]=cnt++;
    return;
}
bool dfs(int node)
{
    for(int i=head[node];~i;i=edge[i].next)
    {
        int v=edge[i].to;
        if(!vis[v])
        {
            vis[v]=true;
            if(match[v]==-1||dfs(match[v]))
            {
                match[v]=node;
                return true;
            }
        }
    }
    return false;
}
int hungry()
{
    int ans=0;
    memset(match,-1,sizeof(match));
    for(int i=1;i<=n*m;i++)
    {
        memset(vis,false,sizeof(vis));
        if(dfs(i))
        {
            ans++;
        }
    }
    return ans;
}
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    while(~scanf("%d%d",&n,&m))
    {
        if(!n&&!m) break;
        init();
        int k;
        scanf("%d",&k);
        while(k--)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            map[u][v]=-1;
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(map[i][j]!=-1)
                {
                    map[i][j]=(i-1)*m+j;
                }
            }
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(map[i][j]!=-1)
                {
                    for(int k=0;k<4;k++)
                    {
                        int nx=i+dirx[k];
                        int ny=j+diry[k];
                        if(map[nx][ny]!=-1&&nx>=1&&nx<=n&&ny>=1&&ny<=m)
                        {
                            add(map[i][j],map[nx][ny]);
                        }
                    }
                }
            }
        }
        int ans=hungry();
        printf("%d\n",ans/2);
        for(int i=1;i<=n*m;i++)
        {
            if(match[i]==-1)
            {
                continue;
            }
            else
            {
                int x=i/m;
                if(i%m!=0) x+=1;
                int y=i%m;
                if(y==0) y=m;

                int a=match[i]/m;
                if(match[i]%m!=0) a+=1;
                int b=match[i]%m;
                if(b==0) b=m;
                if(map[x][y]!=-1&&map[a][b]!=-1)
                {
                    printf("(%d,%d)--(%d,%d)\n",x,y,a,b);
                    map[x][y]=map[a][b]=-1;
                }
            }
        }
        puts("");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37943488/article/details/82315493