(匈牙利算法) hdu 4185

Oil Skimming

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 993    Accepted Submission(s): 422


Problem Description
Thanks to a certain "green" resources company, there is a new profitable industry of oil skimming. There are large slicks of crude oil floating in the Gulf of Mexico just waiting to be scooped up by enterprising oil barons. One such oil baron has a special plane that can skim the surface of the water collecting oil on the water's surface. However, each scoop covers a 10m by 20m rectangle (going either east/west or north/south). It also requires that the rectangle be completely covered in oil, otherwise the product is contaminated by pure ocean water and thus unprofitable! Given a map of an oil slick, the oil baron would like you to compute the maximum number of scoops that may be extracted. The map is an NxN grid where each cell represents a 10m square of water, and each cell is marked as either being covered in oil or pure water.
 

 

Input
The input starts with an integer K (1 <= K <= 100) indicating the number of cases. Each case starts with an integer N (1 <= N <= 600) indicating the size of the square grid. Each of the following N lines contains N characters that represent the cells of a row in the grid. A character of '#' represents an oily cell, and a character of '.' represents a pure water cell.
 

 

Output
For each case, one line should be produced, formatted exactly as follows: "Case X: M" where X is the case number (starting from 1) and M is the maximum number of scoops of oil that may be extracted.
 

 

Sample Input
1 6 ...... .##... .##... ....#. ....## ......
 

 

Sample Output
Case 1: 3
 

 

Source
 
难点就是建立图,01分割的棋盘,0一个集合,1一个集合,转化为求最大匹配就AC
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cmath>
using namespace std;
int tt,n,ans,g[610][610],nx,ny,mark[610],link[610];
char s[610][610];
struct node
{
      int x,y;
}a0[610],a1[610];
bool dfs(int t)
{
      for(int i=0;i<ny;i++)
      {
            if(mark[i]==-1&&g[t][i])
            {
                  mark[i]=1;
                  if(link[i]==-1||dfs(link[i]))
                  {
                        link[i]=t;
                        return true;
                  }
            }
      }
      return false;
}
int main()
{
      int cas=1;
      scanf("%d",&tt);
      while(tt--)
      {
          scanf("%d",&n);
          ans=0,nx=0,ny=0;
          memset(g,0,sizeof(g));
          getchar();
          for(int i=0;i<n;i++)
                  scanf("%s",s[i]);
          for(int i=0;i<n;i++)
                for(int j=0;j<n;j++)
                {
                      if(s[i][j]=='#')
                      {
                          if((i+j)%2==0)
                          {
                                a0[nx].x=i,a0[nx].y=j;
                                nx++;
                          }
                          else
                          {
                                a1[ny].x=i,a1[ny].y=j;
                                ny++;
                          }
                      }
                }
          memset(link,-1,sizeof(link));
          for(int i=0;i<nx;i++)
                  for(int j=0;j<ny;j++)
                  {
                        if(((a0[i].x==a1[j].x)&&abs(a0[i].y-a1[j].y)==1)||((a0[i].y==a1[j].y)&&abs(a0[i].x-a1[j].x)==1))
                              g[i][j]=1;
                  }
          for(int i=0;i<nx;i++)
          {
                memset(mark,-1,sizeof(mark));
                if(dfs(i))
                        ans++;
          }
          printf("Case %d: %d\n",cas,ans);
          cas++;
      }
      return 0;
}

  

posted @ 2015-01-25 19:38  心田定则  阅读(226)  评论(0编辑  收藏  举报