hdu Uncle Tom's Inherited Land*

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)--(1,3)和(1,3)--(1,2),这两点之间就有两条边,因为这地方卡了我一上午······其实仔细想一下就能发现只要跳过相邻的节点建图就可以了,而相邻节点之间的下标和存在奇偶关系,于是根据下标和的奇偶性建图。
 
 
代码:
#include<stdio.h>
#include<string.h>

int map[105][105],hash[105][105],vis[105],pre[105],a[105][105];
struct node
{
  int x,y;
} id[55];
int cnt,n,m;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
int hungry(int u)
{
   int v;
   for(v=1;v<cnt;v++)
   {  
      
      if((id[v].x+id[v].y)%2==1)     //去掉下标和为奇数的点
             continue;
      if(map[u][v]&&vis[v]==0)
      {
         vis[v]=1;
         if(pre[v]==-1||hungry(pre[v]))
         {
            pre[v]=u;
            return 1;
         }
      }
   }
   return 0;
}
int main()
{  
   int i,j,k,t,x,y,ans;
   while(scanf("%d%d",&n,&m)!=EOF)
   {
      if(n==0&&m==0)
          break;
      scanf("%d",&k);
      memset(hash,0,sizeof(hash));
      for(i=0;i<k;i++)
      {
         scanf("%d%d",&x,&y);
         hash[x][y]=1;
      }
      cnt=1;
      for(i=1;i<=n;i++)
          for(j=1;j<=m;j++)
          {
            if(hash[i][j]==0)
            {
               a[i][j]=cnt;
               id[cnt].x=i;
               id[cnt].y=j;
               cnt++;
            }
          }
      memset(map,0,sizeof(map));
      for(i=1;i<=n;i++)
      {
         for(j=1;j<=m;j++)
         {
           if(hash[i][j]==0)
           { 
             for(t=0;t<4;t++)
             {
               x=i+dx[t];
               y=j+dy[t];
               if(x>=1&&x<=n&&y>=1&&y<=m&&hash[x][y]==0)
               {
                  map[a[i][j]][a[x][y]]=1;
                  map[a[x][y]][a[i][j]]=1;
               }
             }
           }
         }
      }
      ans=0;
      memset(pre,-1,sizeof(pre));
      for(i=1;i<cnt;i++)
      {  
         if((id[i].x+id[i].y)%2==0)    //需要注意一下,因为前面匈牙利算法枚举每个v节点,而这里是枚举每个u节点,所以两个的奇偶性不同
             continue;
         memset(vis,0,sizeof(vis));
         if(hungry(i))
             ans++;
      }   
      printf("%d\n",ans);
      for(i=1;i<cnt;i++)
      {
        if(pre[i]>=0)
          printf("(%d,%d)--(%d,%d)\n",id[i].x,id[i].y,id[pre[i]].x,id[pre[i]].y);
      } 
      printf("\n");  
   }
   return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值