zoukankan      html  css  js  c++  java
  • 二分多重匹配(HDU5093)

    Battle ships

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 39    Accepted Submission(s): 19


    Problem Description
    Dear contestant, now you are an excellent navy commander, who is responsible of a tough mission currently.
    Your fleet unfortunately encountered an enemy fleet near the South Pole where the geographical conditions are negative for both sides. The floating ice and iceberg blocks battleships move which leads to this unexpected engagement highly dangerous, unpredictable and incontrollable.
    But, fortunately, as an experienced navy commander, you are able to take opportunity to embattle the ships to maximize the utility of cannons on the battleships before the engagement.
    The target is, arrange as many battleships as you can in the map. However, there are three rules so that you cannot do that arbitrary:
    A battleship cannot lay on floating ice
    A battleship cannot be placed on an iceberg
    Two battleships cannot be arranged in the same row or column, unless one or more icebergs are in the middle of them.
    Input
    There is only one integer T (0<T<12) at the beginning line, which means following T test cases.
    For each test case, two integers m and n (1 <= m, n <= 50) are at the first line, represents the number of rows and columns of the battlefield map respectively. Following m lines contains n characters iteratively, each character belongs to one of ‘#’, ‘*’, ‘o’, that symbolize iceberg, ordinary sea and floating ice.
    Output
    For each case, output just one line, contains a single integer which represents the maximal possible number of battleships can be arranged. 

    Sample Input
    2 4 4 *ooo o### **#* ooo* 4 4 #*** *#** **#* ooo#
     

    Sample Output
    3 5
     
    题意:给出一个n行m列的图,*代表海域,o代表冰水,#代表冰山,要想在海域中放置船,保证船与船之间不能相互看到,之间只要有山就不能看到,问最多能放多少船。
    分析:对于样例:
    3 5
    *##**
    #****
    *#**#
    建图的时候按照行和列建立二部图,首先统计每行和每列的*联通个数

    #include"stdio.h"
    #include"string.h"
    #include"stdlib.h"
    #include"queue"
    #include"algorithm"
    #include"string.h"
    #include"string"
    #include"map"
    #define inf 0x3f3f3f3f
    #define M 3009
    using namespace std;
    char mp[55][55];
    int row[55],col[55];
    int l[M],r[M];
    int G[3000][3000],x[3000],y[3000],use[3000];
    int finde(int u,int n)
    {
        int i;
        for(i=1;i<=n;i++)
        {
            if(!use[i]&&G[u][i])
            {
                use[i]=1;
                if(y[i]==0||finde(y[i],n))
                {
                    y[i]=u;
                    x[u]=i;
                    return 1;
                }
            }
        }
        return 0;
    }
    int max_match(int cnt1,int cnt2)
    {
        memset(x,0,sizeof(x));
        memset(y,0,sizeof(y));
        int ans=0;
        for(int i=1;i<=cnt1;i++)
        {
            if(x[i]==0)
            {
                memset(use,0,sizeof(use));
                ans+=finde(i,cnt2);
            }
        }
        return ans;
    }
    int num[88],vis[66][66];
    struct node
    {
        int v;
        node(int vv)
        {
            v=vv;
        }
    };
    vector<node>edge[M];
    int used[66];
    int main()
    {
        int T,m,n,i,j;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d%d",&n,&m);
            for(i=0;i<n;i++)
                scanf("%s",mp[i]);
            int cnt1=0;
            for(i=0;i<n;i++)
            {
                int flag=0;
                row[i]=0;
                for(j=0;j<m;j++)
                {
                    if(mp[i][j]=='*')
                    {
                        if(!flag)
                        {
                            row[i]++;
                            l[++cnt1]=i;
                            flag=1;
                        }
                        edge[cnt1].push_back(j);//记录当前行和一个连续的*的列号
                    }
                    if(mp[i][j]=='#')
                        flag=0;
                }
            }
            int cnt2=0;
            for(j=0;j<m;j++)
            {
                col[j]=0;
                int flag=0;
                for(i=0;i<n;i++)
                {
                    if(mp[i][j]=='*')
                    {
                        if(!flag)
                        {
                            col[j]++;
                            r[++cnt2]=j;
                            flag=1;
                        }
                        vis[i][j]=cnt2;//记录第i行第j列应与右边相连的点的编号
                    }
                    if(mp[i][j]=='#')
                        flag=0;
                }
            }
            memset(G,0,sizeof(G));
            for(i=1;i<=cnt1;i++)
            {
                memset(used,0,sizeof(used));
                for(j=0;j<(int)edge[i].size();j++)
                    used[edge[i][j].v]=1;
                for(j=1;j<=cnt2;j++)
                {
                    if(mp[l[i]][r[j]]=='*'&&used[r[j]])
                    {
                        G[i][vis[l[i]][r[j]]]=1;
                    }
                }
            }
            int ans=max_match(cnt1,cnt2);
            printf("%d
    ",ans);
            for(i=1;i<=cnt1;i++)
                edge[i].clear();
        }
        return 0;
    }
    


  • 相关阅读:
    关于字符编码
    cocosbuilder中使用字体描边时,字符重叠,间距过小问题
    2dx关于js响应layer触摸消息的bug
    关于js-binding中Layer触摸事件的优化
    2dx解析cocosbuilder中使用layer时的缺陷
    关于 2dx v3.7 UIScale9Sprite的bug
    c++11 Chrono时间库
    cocos2d-x lua table数据存储
    cocos2d-x lua 使用ListView
    Oracle数据库impdp时ora-00439:未启用功能:Deferred Segment Creation
  • 原文地址:https://www.cnblogs.com/mypsq/p/4348122.html
Copyright © 2011-2022 走看看