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)
    Total Submission(s): 17669    Accepted Submission(s): 10749


    Problem 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
     
    Source
     
     
     
     1 /*
     2     本题大意:给出一个n * n的矩阵,让你在这个矩阵中放置尽可能多的炮台。
     3     放置炮台的规则为,任意两个炮台都不在同一行同一列,除非他们之间有wall。
     4     
     5     本题思路:这题很明显回溯可以做,但是下面我们用二分匹配讲解一下这个题,
     6     很明显我们知道如果一个点被选取,那么和他在同一行或者同一列的中间不存
     7     在wall的所有点都不能被选取,所以我们就可以考虑如何限制?我们可以把
     8     这个图按照行缩点,再按照列缩点,那么我们就可以知道,对于缩点之后的行
     9     标号i和列标号j,如果i -> j之间存在一条边,那就说明在方格(i, j)上放了
    10     一个点,那么i行j列不能再放其它其它点,那么我们可以想到对得到的缩点进行
    11     建立二分图,行和列分别为二部图的两部分,然后依据原图建边,寻找到最大匹配
    12     即为答案。
    13 */
    14 #include <cstdio>
    15 #include <cstring>
    16 #include <algorithm>
    17 using namespace std;
    18 
    19 const int maxn = 20 + 5;
    20 bool used[maxn];//表示是否在交错路中
    21 int linker[maxn];//存储匹配点
    22 int g[maxn][maxn];//存缩点之后的图
    23 char str[maxn][maxn];
    24 int x[maxn][maxn], cntx;//行点集
    25 int y[maxn][maxn], cnty;//列点集
    26 
    27 bool dfs(int u) {
    28     for(int v = 1; v <= cnty; v ++) {
    29         if(g[u][v] && !used[v]) {
    30             used[v] = true;
    31             if(linker[v] == -1 || dfs(linker[v])) {
    32                 linker[v] = u;
    33                 return true;
    34             }
    35         }
    36     }
    37     return false;
    38 }
    39 
    40 int hungary() {
    41     int res = 0;
    42     memset(linker, -1, sizeof linker);
    43     for(int u = 1; u <= cntx; u ++) {
    44         memset(used, false, sizeof used);
    45         if(dfs(u)) res ++;
    46     }
    47     return res;
    48 }
    49 
    50 int main() {
    51     int n;
    52     while(scanf("%d", &n) && n) {
    53         memset(x, 0, sizeof x);
    54         memset(y, 0, sizeof y);
    55         memset(g, false, sizeof g);
    56         for(int i = 0; i < n; i ++) {
    57             scanf("%s", str[i]);
    58         }
    59         cntx = 1;
    60 
    61         //对行缩点
    62         for(int i = 0; i < n; i ++) {
    63             for(int j = 0; j < n; j ++) {
    64                 if(str[i][j] == '.') x[i][j] = cntx;
    65                 if(str[i][j] == 'X') cntx ++;
    66             }
    67             cntx ++;
    68         }
    69 
    70         //对列缩点
    71         cnty = 1;
    72         for(int j = 0; j < n; j ++) {
    73             for(int i = 0; i < n; i ++) {
    74                 if(str[i][j] == '.') y[i][j] = cnty;
    75                 if(str[i][j] == 'X') cnty ++;
    76             }
    77             cnty ++;
    78         }
    79         for(int i = 0; i < n; i ++) {
    80             for(int j = 0; j < n; j ++) {
    81                 if(str[i][j] == '.') g[x[i][j]][y[i][j]] = true;
    82             }
    83         }
    84         printf("%d
    ", hungary());
    85     }
    86     return 0;
    87 }
  • 相关阅读:
    计算机进制转换
    十进制二进制转换
    windows XP 下怎样查看系统运行时间总计?
    “编译器错误信息: CS0016: 未能写入输出文件”解决方法
    用ntsd关进程
    Hosts文件的位置
    开机显示client mac addr...... 错误的解决办法 .
    无法嵌入互操作类型“Microsoft.Office.Interop.Excel.ApplicationClass”。请改用适用的接口 .
    【转】XP远程桌面连接2008提示:远程计算机需要网络级别身份验证,而您的计算机不支持该验证 .
    关于Access的郁闷二三事。。。
  • 原文地址:https://www.cnblogs.com/bianjunting/p/11411147.html
Copyright © 2011-2022 走看看