BZOJ1735Muddy Fields 泥濘的牧場

1735: [Usaco2005 jan]Muddy Fields 泥濘的牧場
Time Limit: 5 Sec Memory Limit: 64 MB
Submit: 244 Solved: 146
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.
大雨侵襲了奶牛們的牧場.牧場是一個R * C的矩形,其中1≤R,C≤50.大雨將沒有長草的土地弄得泥濘不堪,可是小心的奶牛們不想在喫草的時候弄髒她們的蹄子. 爲了防止她們的蹄子被弄髒,約翰決定在泥濘的牧場裏放置一些木板.每一塊木板的寬度爲1個單位,長度任意.每一個板必須放置在平行於牧場的泥地裏. 約翰想使用最少的木板覆蓋所有的泥地.一個木板可以重疊在另一個木板上,但是不能放在草地上.
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.
第1行:兩個整數R和C.
第2到R+1行:每行C個字符,其中“*’代表泥地,“.”代表草地.
Output
* Line 1: A single integer representing the number of boards FJ needs.
最少需要多少木板.
Sample Input
4 4
..
.*
*.
..*.
Sample Output
4
HINT
這裏寫圖片描述
Source
Gold
最小點覆蓋。。
二分圖匹配。。
附上本蒟蒻的代碼:

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int n,m,h[100001],part[100001],cnt=1,ans=0,a[51][51],b[51][51];
char s[51][51];
bool used[100001];
struct kx
{
    int to,next;
}edge[100001];

int read()
{
    int w=0,c=1; char ch=getchar();
    while (ch<'0' || ch>'9')
      {
        if (ch=='-') c=-1;
        ch=getchar();
      }
    while (ch>='0' && ch<='9')
      w=w*10+ch-'0',ch=getchar();
    return w*c;
}

void add(int u,int v)
{
    cnt++,edge[cnt].next=h[u],h[u]=cnt,edge[cnt].to=v;
}

bool find(int s)
{
    int k;
    for (k=h[s];k;k=edge[k].next)
      if (!used[edge[k].to])
        {
            used[edge[k].to]=true;
            if (!part[edge[k].to] || find(part[edge[k].to]))
              {
                part[edge[k].to]=s;
                return true;
              }
        }
    return false;
}

int main()
{
    int i,j,xi=0,yi=0;
    n=read(),m=read();
    for (i=0;i<n;i++)
      scanf("%s",&s[i]);
    for (i=0;i<n;i++)
      for (j=0;j<m;j++)
        if (s[i][j]=='*')
          {
            if (j>0 && s[i][j-1]=='*') a[i][j]=a[i][j-1];
            else a[i][j]=++yi;
            if (i>0 && s[i-1][j]=='*') b[i][j]=b[i-1][j];
            else b[i][j]=++xi;
          }
    for (i=0;i<n;i++)
      for (j=0;j<m;j++)
        if (s[i][j]=='*') 
          add(a[i][j],b[i][j]);
    n=yi;
    for (i=1;i<=n;i++)
      {
        memset(used,false,sizeof(used));
        if (find(i)) ans++;
      }
    printf("%d",ans);
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章