• HDU 5093 Battle ships [二分图匹配]


    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表示浮冰,#表示冰山。每行每列只能放一艘船,有冰山隔开则可以多放。问最多能放多少艘船。


    考虑若没有冰山,则只需给每一块空地对应的行结点和列结点连一条边,就是个二分图,求最大匹配即可。

    现在多了冰山的条件,把冰山左右两边当做两行,冰山上下当做两列即可。先按行扫一遍给结点做行编号,然后按列扫描连边建图。


    #include<iostream>
    #include<cassert>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    #include<string>
    #include<iterator>
    #include<cstdlib>
    #include<vector>
    #include<stack>
    #include<queue>
    #include<map>
    #include<set>
    using namespace std;
    #define debug(x) cout<<"debug "<<x<<endl;
    #define rep(i,f,t) for(int i = (f),_end_=(t); i <= _end_; ++i)
    #define rep2(i,f,t) for(int i = (f),_end_=(t); i < _end_; ++i)
    #define dep(i,f,t) for(int i = (f),_end_=(t); i >= _end_; --i)
    #define dep2(i,f,t) for(int i = (f),_end_=(t); i > _end_; --i)
    #define clr(c, x) memset(c, x, sizeof(c) )
    typedef long long int64;
    const int INF = 0x5f5f5f5f;
    const double eps = 1e-8;
    
    
    //*****************************************************
    typedef vector<int> Vec;
    Vec G[2501];
    int mch[2501];
    int n,m;
    int cnt;
    int mp[55][55];
    
    void build()
    {
        int id = 1;
        bool flg = false;
        rep(i,1,2500)G[i].clear();
    
        //按行扫描,做好行编号
        rep(i,1,n)
        {
            rep(j,1,m)
            {
                if(!mp[i][j])continue; //floating ice
                if(mp[i][j] > 0){ //ordinary sea
                    mp[i][j] = id;
                    flg = true; //次id已有使用
                }else if(flg){
                    flg = false;
                    ++id;
                }
            }
            if(flg){
                ++id;
                flg = false;
            }
        }
        //******************************************************
        //按列扫描,建图
        rep(j,1,m)
        {
            rep(i,1,n)
            {
                if(!mp[i][j])continue;
                if(mp[i][j] > 0){
                    int r = mp[i][j];
                    G[r].push_back(id);
                    G[id].push_back(r);
                    flg = true;
                }else if(flg){
                    flg = false;
                    ++id;
                }
            }
            if(flg){
                ++id;
                flg = 0;
            }
        }
        cnt = id - 1;
    }
    bool vis[2501];
    bool dfs(int v)
    {
        vis[v] = true;
        rep(i,0,G[v].size()-1)
        {
            int u = G[v][i], w = mch[u];
            if( w < 0 || (!vis[w] && dfs(w)) ){
                mch[v] = u;
                mch[u] = v;
                return true;
            }
        }
        return false;
    }
    int gao()
    {
        clr(mch,-1);
        int res = 0;
        rep(i,1,cnt)
        {
            if(mch[i] < 0){
                clr(vis,0);
                if(dfs(i))++res;
            }
        }
        return res;
    }
    int main()
    {
        int cas;
        scanf("%d",&cas);
        while(cas--)
        {
            scanf("%d%d",&n,&m);
            clr(mp,0);
            rep(i,1,n)rep(j,1,m)
            {
                char c;
                scanf(" %c",&c);
                if(c=='#')      mp[i][j] = -1; //iceberg
                else if(c=='*') mp[i][j] = 1; //ordnary sea   用数字代替,简化编码
            }
            build();
            int ans = gao();
            printf("%d
    ",ans);
        }
    
        return 0;
    }
    




    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    Vue 获取dom元素中的自定义属性值
    php文件包含
    php数组运算符
    管理一组事物的状态
    php数据类型转换
    php函数
    PHP循环while do while循环
    php的流程控制 if elseif swich case for循环
    php的工作原理
    python算数、逻辑运算,位运算
  • 原文地址:https://www.cnblogs.com/DSChan/p/4861988.html
Copyright © 2020-2023  润新知