zoukankan      html  css  js  c++  java
  • HDU 1045 Fire Net (二分匹配)

    Fire Net

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

    Description
    Suppose that we have a square city with straight streets. A map of a city is a square board with n rows and n columns, each representing a street or a piece of wall. 

    A blockhouse is a small castle that has four openings through which to shoot. The four openings are facing North, East, South, and West, respectively. There will be one machine gun shooting through each opening. 

    Here we assume that a bullet is so powerful that it can run across any distance and destroy a blockhouse on its way. On the other hand, a wall is so strongly built that can stop the bullets. 

    The goal is to place as many blockhouses in a city as possible so that no two can destroy each other. A configuration of blockhouses is legal provided that no two blockhouses are on the same horizontal row or vertical column in a map unless there is at least one wall separating them. In this problem we will consider small square cities (at most 4x4) that contain walls through which bullets cannot run through. 

    The following image shows five pictures of the same board. The first picture is the empty board, the second and third pictures show legal configurations, and the fourth and fifth pictures show illegal configurations. For this board, the maximum number of blockhouses in a legal configuration is 5; the second picture shows one way to do it, but there are several other ways. 



    Your task is to write a program that, given a description of a map, calculates the maximum number of blockhouses that can be placed in the city in a legal configuration. 
     
    Input
    The input file contains one or more map descriptions, followed by a line containing the number 0 that signals the end of the file. Each map description begins with a line containing a positive integer n that is the size of the city; n will be at most 4. The next n lines each describe one row of the map, with a '.' indicating an open space and an uppercase 'X' indicating a wall. There are no spaces in the input file. 
     
    Output
    For each test case, output one line containing the maximum number of blockhouses that can be placed in the city in a legal configuration.
     
    Sample Input
    4
    .X..
    ....
    XX..
    ....
    2
    XX
    .X
    3
    .X.
    X.X
    .X.
    3
    ...
    .XX
    .XX
    4
    ....
    ....
    ....
    ....
    0
     
    Sample Output
    5
    1
    5
    2
    4
    题意:放置尽可能多的炮台,使得没有两个在中间没有障碍的情况下位于同一行或同一列
    分析:可以用二分匹配来做。将每一行连着的空白格看做X里的点,将每一列连着的空白格看做Y里的点,如果这一行点与这个列点所表示的行和列有交点,就将他们连一条边,然后最大匹配就是答案
    当然,也可以dfs爆搜。
    #include<stdio.h>
    #include<string.h>
    #include<iostream>
    using namespace std;
    /* ***************************************************
    二分图匹配(匈牙利算法的DFS实现)
    INIT:G[][]两边定点划分的情况
    CALL:res=hungary();输出最大匹配数
    优点:适于稠密图,DFS找增广路快,实现简洁易于理解
    时间复杂度:O(VE);
    *************************************************** */
    const int MAXN = 10;
    int uN,vN;
    int G[MAXN][MAXN];
    int linker[MAXN];
    bool used[MAXN];
    
    bool dfs(int u)
    {
        int v;
        for(v=1;v<=vN;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;
        int u;
        memset(linker,-1,sizeof(linker));
        for(u=1;u<=uN;u++){
            memset(used,false,sizeof(used));
            if(dfs(u)) res++;
        }
        return res;
    }
    
    char mp[MAXN][MAXN];
    int row[MAXN][MAXN],col[MAXN][MAXN];
    
    int main()
    {
        int n;
        while(scanf("%d",&n)==1&&n){
            for(int i=0;i<n;i++) scanf("%s",mp[i]);
            int tol1=0,tol2=0;
            memset(G,0,sizeof(G));
            memset(row,-1,sizeof(row));
            memset(col,-1,sizeof(col));
            for(int i=0;i<n;i++){
                for(int j=0;j<n;j++){
                    if(mp[i][j]=='X') continue;
                    if(i==0||mp[i-1][j]=='X') row[i][j]=++tol1;
                    else row[i][j]=row[i-1][j];
                }
            }
            for(int j=0;j<n;j++){
                for(int i=0;i<n;i++){
                    if(mp[i][j]=='X') continue;
                    if(j==0||mp[i][j-1]=='X') col[i][j]=++tol2;
                    else col[i][j]=col[i][j-1];
                }
            }
            uN=tol1,vN=tol2;
            for(int i=0;i<n;i++){
                for(int j=0;j<n;j++){
                    if(mp[i][j]=='.'){
                        if(!G[row[i][j]][col[i][j]]) G[row[i][j]][col[i][j]]=1;
                    }
                }
            }
            printf("%d
    ",Hungary());
        }
        return 0;
    }
  • 相关阅读:
    对于捐赠承诺和劳务捐赠,不予以确认,但应在会计报表附注中披露
    R语言代写线性混合效应模型Linear Mixed-Effects Models的部分折叠Gibbs采样
    matlab代写MCMC贝叶斯方法用于加筋复合板的冲击载荷识别
    R语言代写dplyr-高效的数据变换与整理工具
    GIS代写遥感数据可视化评估:印度河流域上部的积雪面积变化
    R语言代写向量自回归模型(VAR)及其实现
    r语言代写实现似然的I(2)协整VAR模型弱外生性推理
    python代写缺失值处理案例分析:泰坦尼克数据
    Python代写高性能计算库——Numba
    matlab递归神经网络RNN实现:桨距控制控制风力发电机组研究
  • 原文地址:https://www.cnblogs.com/wangdongkai/p/5635708.html
Copyright © 2011-2022 走看看