zoukankan      html  css  js  c++  java
  • Muddy Fields

     Muddy Fields
    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
    Submit Status

    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

    经典构图
      1 #include <cstdio>
      2 #include <cmath>
      3 #include <queue>
      4 #include <string.h>
      5 #include <iostream>
      6 using namespace std;
      7 int head[60000],linkx[60000],vi[60000],q[60000];
      8 int dx[60000],dy[60000];
      9 struct Edge
     10 {
     11     int to,next;
     12     Edge() {};
     13     Edge(int t,int n):to(t),next(n) {}
     14 };
     15 Edge a[200000];
     16 int an=0,nn;
     17 void AddEdge(int u,int v)
     18 {
     19     a[an]=Edge(v,head[u]);
     20     head[u]=an++;
     21 }
     22 bool bfs()
     23 {
     24     int i,u,v,h,t;
     25     h=t=0;
     26     bool flag=0;
     27     memset(dx,-1,sizeof(dx));
     28     memset(dy,-1,sizeof(dy));
     29     for(i=0; i<=nn; i++)
     30         if(!vi[i])
     31             q[h++]=i,dx[i]=0;
     32     while(h!=t)
     33     {
     34         u=q[t++];
     35         for(i=head[u]; ~i; i=a[i].next)
     36         {
     37             if(dy[v=a[i].to]==-1)
     38             {
     39                 dy[v]=dx[u]+1;
     40                 if(linkx[v]==-1)flag=1;
     41                 else
     42                 {
     43                     dx[linkx[v]]=dy[v]+1;
     44                     q[h++]=linkx[v];
     45                 }
     46             }
     47         }
     48     }
     49     return flag;
     50 }
     51 bool dfs(int u)
     52 {
     53     int v,i;
     54     for(i=head[u]; ~i; i=a[i].next)
     55     {
     56         v=a[i].to;
     57         if(dy[v]==dx[u]+1)
     58         {
     59             dy[v]=-1;
     60             if(linkx[v]==-1||dfs(linkx[v]))
     61             {
     62                 linkx[v]=u;
     63                 return 1;
     64             }
     65         }
     66     }
     67     return 0;
     68 }
     69 int MaxMatch()
     70 {
     71     int ans=0;
     72     memset(vi,0,sizeof(vi));
     73     while(bfs())
     74         for(int i=0; i<=nn; i++)
     75             if(!vi[i]&&dfs(i))
     76                 vi[i]=1,ans++;
     77     return ans;
     78 }
     79 int main()
     80 {
     81     int i,j,m,n;
     82     int x[100][100];
     83     while(~scanf("%d%d",&n,&m))
     84     {
     85         memset(head,-1,sizeof(head));
     86         memset(linkx,-1,sizeof(linkx));
     87         an=0;
     88         getchar();
     89         for(i=1; i<=n; i++)
     90         {
     91             for(j=1; j<=m; j++)
     92             {
     93                 x[i][j]=getchar();
     94             }
     95             getchar();
     96         }
     97         int now='1';
     98         for(i=1; i<=n; i++)
     99         {
    100             j=1;
    101             while(j<=m)
    102             {
    103                 while(j<=m&&x[i][j]!='*')j++;
    104                 while(j<=m&&x[i][j]=='*')x[i][j++]=now;
    105                 now++;
    106             }
    107         }
    108         nn=now;
    109         now='1';
    110         for(i=1; i<=m; i++)
    111         {
    112             j=1;
    113             while(j<=n)
    114             {
    115                 while(j<=n&&x[j][i]=='.')j++;
    116                 while(j<=n&&x[j][i]!='.')AddEdge(now-'0',(int)(x[j++][i]-'0'));
    117                 now++;
    118             }
    119         }
    120         printf("%d
    ",MaxMatch());
    121     }
    122 }
    View Code
  • 相关阅读:
    WWF中关于External Data Exchange Service的错误
    通用异常处理框架(转载)
    追MM和23种设计模式(转载)
    编译:XAML与程序代码结合(转载)
    异步编程*(转载)
    出糗~又是工作流!
    由WorkflowOwnershipException 异常,联想出的部署犯案
    Nullable类型,C#溢出检查,typeof运算符和运算符的重载 分类: C# 20121028 19:50 648人阅读 评论(0) 收藏
    我的程序员之路(九)参加郑州微软MVP宣讲会后的一些思考
    C#结构的学习
  • 原文地址:https://www.cnblogs.com/ERKE/p/3859078.html
Copyright © 2011-2022 走看看