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): 1091    Accepted Submission(s): 395


    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
     
    Source
     
    题意:给出一个地图,*为海水区,o为浮冰区,#为冰山   要在海水区放船,除非被冰山阻挡,否则每行或每列只能放一艘,问最多能放多少
    思路:如果不管冰山阻挡的约数条件,每一行或每一列只能被选择一次,就是显然的二分图行列匹配。有了约数条件,其实就是把每一行或每一列根据#来分段,再匹配就行了
    20160407群赛-补
    #include <bits/stdc++.h>
    using namespace std;
    const int N = 55;
    char Mat[N][N];
    int n, m;
    int cnt1, cnt2;
    int x[N][N], y[N][N], g[N * N][N * N];
    void init() {
        cnt1 = 0, cnt2 = 0;
        for(int i = 0; i < n; ++i) {
            int p = 0, j, f = 0;
            while(p < m) {
                f = 0;
                for(j = p; j < m; ++j) {
                        if(Mat[i][j] == '*') { f = 1; x[i][j] = cnt1; }
                        else if(Mat[i][j] == '#') break;
                }
                if(f)
                ++cnt1;
                p = j;
                ++p;
            }
        }
        for(int i = 0; i < m; ++i) {
            int p = 0, j, f = 0;
            while(p < n) {
                f = 0;
                for(j = p; j < n; ++j) {
                        if(Mat[j][i] == '*') { f = 1; y[j][i] = cnt2; }
                        else if(Mat[j][i] == '#') break;
                }
                if(f)
                ++cnt2;
                p = j;
                ++p;
            }
        }
    }
    void look(int a[][55]) {
        for(int i = 0; i < n; ++i) {
            for(int j = 0; j < m; ++j) printf("%d ", a[i][j]);
            puts("");
        }
    }
    void get() {
        memset(g, 0, sizeof g);
        for(int i = 0; i < n; ++i)
            for(int j = 0; j < m; ++j)
            if(Mat[i][j] == '*')
            g[ x[i][j] ][ y[i][j] ] = 1;
    }
    int linker[N * N];
    bool used[N * N];
    bool dfs(int u) {
        for(int v = 0; v < cnt2; ++v)
            if(g[u][v] && !used[v]) {
                used[v] = true;
                if(linker[v] == -1 || dfs(linker[v])) {
                    linker[v] = u;
                    return true;
                }
            }
        return false;
    }
    int hungary() {
        int res = 0;
        memset(linker, -1, sizeof linker);
        for(int u = 0; u < cnt1; ++u)
        {
            memset(used, false, sizeof used);
            if(dfs(u)) res++;
        }
        return res;
    }
    int main() {
        int _; scanf("%d", &_);
        while(_ --) {
            scanf("%d%d", &n, &m);
            for(int i = 0; i < n; ++i) scanf("%s", Mat[i]);
            init();
            get();
           // look(x);
            //look(y);
           printf("%d
    ", hungary());
        }
        return 0;
    }
    View Code
  • 相关阅读:
    文件上传案例_Socket_测试
    Linux的小整理(入门)
    full stack 第一天
    基础考题 试题
    shell语法
    网络管理
    图像类
    定时储存
    网络管理
    磁盘管理
  • 原文地址:https://www.cnblogs.com/orchidzjl/p/5370214.html
Copyright © 2011-2022 走看看