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);
        }
    }
  • 相关阅读:
    常见邮件服务器(接收服务器和发送邮件服务器)地址
    Linux下搭建SVN服务器(CentOS)
    macBook下更新python
    画画练习20180627
    如何用Photoshop画一个发光金币(unity游戏素材教程)
    Python+VSCode+Git 学习总结
    如何在MFC DLL中向C#类发送消息
    MFC信号量使用指南
    回归cnBlogs
    Web自动化测试框架Watir(基于Ruby)
  • 原文地址:https://www.cnblogs.com/mfys/p/7614951.html
Copyright © 2011-2022 走看看