zoukankan      html  css  js  c++  java
  • poj2226更改行列匹配建图

    Muddy Fields
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 10961   Accepted: 4071

    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
    

    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.

    Source

     

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<vector>
    using namespace std;
    const int N=58;
    char mp[N][N];
    int used[N*N],head[N*N],tot,n,m;
    struct node
    {
        int to,next;
    } e[100861];
    bool vis[N*N];
    int aa[N][N],bb[N][N],a,b;
    void add(int u,int v)
    {
        e[tot].to=v;
        e[tot].next=head[u];
        head[u]=tot++;
    }
    bool findx(int u)
    {
        for(int i=head[u]; ~i; i=e[i].next)
        {
            int v=e[i].to;
            if(vis[v]) continue;
            vis[v]=1;
            if(!used[v]||findx(used[v]))
            {
                used[v]=u;
                return true;
            }
        }
        return false;
    }
    int main()
    {
        while(scanf("%d%d",&n,&m)!=EOF)
        {
            int maxmatch=0;
            tot=0;
            a=b=1;
            memset(head,-1,sizeof(head));
            memset(aa,0,sizeof(aa));
            memset(bb,0,sizeof(bb));
            for(int i=1; i<=n; ++i) scanf("%s",mp[i]+1);
            for(int i=1; i<=n; ++i) for(int j=1; j<=m; ++j)
                {
                    bool c=0;
                    while(mp[i][j]=='*'&&j<=m) c=1,aa[i][j++]=a;
                    if(c) ++a;
                }
            for(int i=1; i<=m; ++i) for(int j=1; j<=n; ++j)
                {
                    bool c=0;
                    while(mp[j][i]=='*'&&j<=n) c=1,bb[j++][i]=b;
                    if(c) ++b;
                }
            for(int i=1; i<=n; ++i) for(int j=1; j<=m; ++j) if(aa[i][j]) add(aa[i][j],bb[i][j]);
            memset(used,0,sizeof(used));
            for(int i=1; i<a; ++i)
            {
                memset(vis,0,sizeof(vis));
                if(findx(i)) ++maxmatch;
            }
            printf("%d
    ",maxmatch);
        }
    }
  • 相关阅读:
    DataGridView 设置行不可见时,与货币管理器的位置关联的行不能设置为不可见
    DataGridView 冻结列后出现 无法添加该列,原因是它被冻结并被置于未冻结的列之后
    sql 2000 查询中增加序号列,自动增加列
    SQL 语法大全
    清除vs2003vs2008起始页最近打开项目
    ALTER TABLE 修改表时 因为有一个或多个对象访问此列
    UNIX上C++程序设计守则(信号和线程)(上)
    Thread Cancel 指南
    [C++再学习系列] 深入new/delete:New的3种形态
    设计模式学习(六):重构与模式,推荐书籍(完)
  • 原文地址:https://www.cnblogs.com/mfys/p/7614951.html
Copyright © 2011-2022 走看看