天天写算法之Uncle Tom's Inherited Land*

这个题目的特别行在于如何区分两个集合,做到二分。

自己按照以前的格式写了一个,很容易理解,每次都找四个方向。代码如下,很容易理解。

#include<iostream>
#include<string.h>
#include<cstdio>
#define MAX 105
using namespace std;
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int Map[MAX][MAX];
int Vis[MAX][MAX];
int n, m ,k;
struct Node{
    int x ;
    int y ;
};
Node nodes[MAX][MAX];

bool dfs(Node a)
{
    Node temp ;
    for(int i = 0 ; i <4 ; i ++ )
    {
        temp.x=dir[i][0]+a.x;
        temp.y=dir[i][1]+a.y;
        if(temp.x>=1&&temp.x<=n&&temp.y>=1&&temp.y<=m)
        {
         if(!Vis[temp.x][temp.y]&&Map[temp.x][temp.y])
         {
             Vis[temp.x][temp.y]=1 ;
             if(!nodes[temp.x][temp.y].x||dfs(nodes[temp.x][temp.y]))//一开始这个地方写成了dfs(temp)了
             {
                 nodes[temp.x][temp.y]=a ;
                 return true ;
             }
         }
        }
    }
    return false ;
}
int hungary(){
    int ans= 0 ;
    Node temp ;
    for(int i = 1 ; i<=n ; i ++)
    {
          for(int j =1 ; j <= m ; j ++)
          {
              if(((i+j)&1)&&Map[i][j])
              {
                    memset(Vis,0,sizeof(Vis));
                    temp.x = i ; temp.y = j;
                    if(dfs(temp))
                        ans+=1;
              }
          }
    }
    return ans ;
}

int main(){
    int a, b ,i,j;
  while(~scanf("%d%d",&n,&m)&&m+n!=0)
  {
      scanf("%d",&k);
      memset(Map,1,sizeof(Map));
      while(k--)
      {
          scanf("%d%d",&a,&b);
          Map[a][b]=0;
      }
      memset(nodes,0,sizeof(nodes));

      int ans = hungary();

      printf("%d\n",ans);
      for(i =  1 ; i <= n ; i ++)
      {
          for(j = 1 ; j <= m ; j++)
          {
              if(nodes[i][j].x)
              {
                  printf("(%d,%d)--(%d,%d)\n",nodes[i][j].x,nodes[i][j].y,i,j);//这个答案不唯一,可能和样例给出的答案不一致,很正常,输出的顺序也没有要求。
              }
          }
      }
      printf("\n");
  }

}

Uncle Tom's Inherited Land

02-26

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.)nnYour 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.nnYour uncle asked your help to determine the largest number of properties he could sell (the remaining squares will become recreational parks).nnnnnInputnnInput 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.nnnOutputnnFor each test case in the input your program should produce one line of output, containing an integer value representing the maximum number of properties which can be sold.nnnSample Inputnn4 4n6n1 1n1 4n2 2n4 1n4 2n4 4n4 3n4n4 2n3 2n2 2n3 1n0 0nnnSample Outputnn4n3n

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试