poj 2226 二分图最小点集覆盖


这道题难就难在建图。


Muddy Fields
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6055 Accepted: 2246

Description

Rain has pummeled the cows' field, a rectangular grid of R rows and C columns (1 <= R <= 50, 1 <= C <= 50). While good for the grass, the rain makes some patches of bare earth quite muddy. The cows, being meticulous grazers, don't want to get their hooves dirty while they eat. 

To prevent those muddy hooves, Farmer John will place a number of wooden boards over the muddy parts of the cows' field. Each of the boards is 1 unit wide, and can be any length long. Each board must be aligned parallel to one of the sides of the field. 

Farmer John wishes to minimize the number of boards needed to cover the muddy spots, some of which might require more than one board to cover. The boards may not cover any grass and deprive the cows of grazing area but they can overlap each other. 

Compute the minimum number of boards FJ requires to cover all the mud in the field.

Input

* Line 1: Two space-separated integers: R and C 

* Lines 2..R+1: Each line contains a string of C characters, with '*' representing a muddy patch, and '.' representing a grassy patch. No spaces are present.

Output

* Line 1: A single integer representing the number of boards FJ needs.

Sample Input

4 4
*.*.
.***
***.
..*.

Sample Output

4


#include <cstdio>
#include <string.h>
#include <vector>
#include <algorithm>
using namespace std;
int flag[1002] , visit[1002];
vector<int>aa[1002];
char s[55][55];
int a[1002][1002] , b[1002][1002];
int r,c;
void goutu(  )
{
     for( int icnt=1 , i=1 ; i<=r ; i++ )     
          for( int j=1 ; j<=c ; j++ ){
               if( s[i][j] == '*' )
                   a[i][j] = icnt;
               if( s[i][j] == '*' && s[i][j+1] != '*' )     
                   icnt++;
          }
     for( int jcnt=1 , i=1 ; i<=c ; i++ )     
          for( int j=1 ; j<=r ; j++ ){
               if( s[j][i] == '*' )
                   b[j][i] = jcnt;
               if( s[j][i] == '*' && s[j+1][i] != '*' )     
                   jcnt++;
          }
     for( int  i=1 ; i<=r ; i++ )     
          for( int j=1 ; j<=c ; j++ ){
               if( s[i][j] == '*' )
                   aa[a[i][j]].push_back( b[i][j] );
          }
}

int dfs( int x )
{
    for( int i=0 ; i<aa[x].size() ; i++ ){
         if( !visit[aa[x][i]] ){
             visit[aa[x][i]] = 1;
             if( !flag[aa[x][i]] || dfs(flag[aa[x][i]]) ){
                 flag[aa[x][i]] = x;
                 return true;    
             }
         }
    }
    return false;
}

int main( )
{
    while( ~scanf("%d%d",&r,&c) )    
    {
           memset( aa , 0 , sizeof(aa) );
           memset( b , 0 , sizeof(b) );
           memset( a , 0 , sizeof(a) );
           memset( flag , 0 , sizeof(flag) );
           memset( s , '0' , sizeof(s) );
           for( int i=1 ; i<=r ; i++ )
                scanf("%s",s[i]+1);
           goutu( );
           int sum = 0;
           for( int i=1 ; i<1002 ; i++ ){
                memset( visit , 0 , sizeof(visit) );
                if( dfs(i) )
                    sum++;
           }
           printf("%d\n",sum);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值