poj 2226

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
Hint
OUTPUT DETAILS: 

Boards 1, 2, 3 and 4 are placed as follows: 
1. 2. 
. 333 
444. 
..2. 
Board 2 overlaps boards 3 and 4.


题目大意:给出一个矩阵,现在需要在*号上铺地板,地板是宽度为1,长度不限的,现在要求输出最少的地板块数,并且只能在 * 上铺。
解题思路:经典的建图问题。可以把矩阵横行进行编号,竖列进行编号,相邻的 * 使用一个编号。在进行二分匹配,求出最小覆盖数,即最大匹配数。
编号如下
横行编号
1.2. 
.333 
444. 
..2.
数列编号:
1 .  4 .
.  3 4 5
2 3 4 .
.  .  4 .
对于该问题可一这样理解,每个 * 都可以用横行的板覆盖或者竖列的板覆盖,为覆盖完全并且达到最少,只用把这些板进行匹配,将横板作为一个集合,竖板作为一个集合,让所有编号都用上,则可以保证所有的 * 都被覆盖,而二分匹配则是将这些板最小化使用。
代码:
#include<stdio.h>
#include<string.h>
int a[1500][1500];
int b[1500];
int v[1500];
int x,y;
int fun(int u)
{
    int i;
    for(i=1;i<=x;i++)
    {
      if(!b[i]&&a[u][i])
      {
        b[i]=1;
if(!v[i]||fun(v[i]))
{
v[i]=u;
return 1;
}
  }
}
return 0;
}
int main()
{
int i,j,k,m,n;
char s[60][60];
int e0[60][60];
int e1[60][60];
while(scanf("%d%d",&m,&n)!=EOF)
{
for(i=0;i<m;i++)
 scanf("%s",s[i]);
memset(e0,0,sizeof(e0));
memset(e1,0,sizeof(e1));
x=0; 
        for(i=0;i<m;i++)
{
   if(s[i][0]=='*')
      e0[i][0]=++x;
for(j=1;j<n;j++)
{
if(s[i][j]=='*'&&s[i][j-1]!='*')
 e0[i][j]=++x;
else if(s[i][j]=='*'&&s[i][j-1]=='*')
 e0[i][j]=x;
}
}
y=0;
for(i=0;i<n;i++)
{
   if(s[0][i]=='*')
     e1[0][i]=++y;
for(j=1;j<m;j++)
{
   if(s[j][i]=='*'&&s[j-1][i]!='*')
      e1[j][i]=++y;
   else if(s[j][i]=='*'&&s[j-1][i]=='*')
      e1[j][i]=y;
}

memset(a,0,sizeof(a));
memset(v,0,sizeof(v)); 
for(i=0;i<m;i++)
for(j=0;j<n;j++)
   if(s[i][j]=='*')
  a[e0[i][j]][e1[i][j]]=1;
k=0;
if(x<y)
x=y;
for(i=1;i<=x;i++)
{
memset(b,0,sizeof(b));
if(fun(i))
 k++;  
}
printf("%d\n",k);
}
return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值