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

    Fire Net

    Time Limit: 2 Seconds      Memory Limit: 65536 KB

    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.

    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.

    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: Zhejiang University Local Contest 2001

     1 //2014-03-16 19:29:03     Accepted    1002    C++    0    172    姜伯约
     2 /*
     3 
     4     题意:
     5         最多可在棋盘上放多少个不冲突的棋(无障碍时不能同行同列)
     6     
     7     二分匹配:
     8         二分匹配的经典题,也可以用搜索做。
     9         用二分匹配做的难点是建图,具体细节要好好体会。 
    10         
    11      
    12 
    13 */
    14 #include<stdio.h>
    15 #include<string.h>
    16 char c[10][10],mapc[10][10],mapr[10][10];
    17 int g[10][10];
    18 int vis[10];
    19 int match[10];
    20 int n,N,M;
    21 void build()
    22 { 
    23     memset(mapc,0,sizeof(mapc));
    24     memset(mapr,0,sizeof(mapr));
    25     memset(g,0,sizeof(g));
    26     N=M=0;
    27     for(int i=0;i<n;i++)
    28         for(int j=0;j<n;j++)
    29            if(c[i][j]=='X')
    30                mapr[i][j]=mapc[i][j]=-1;
    31     int cnt=0;
    32     for(int i=0;i<n;i++)
    33         for(int j=0;j<n;j++){
    34             while(mapr[i][j]==-1 && j<n) j++;
    35             cnt++;
    36             while(mapr[i][j]!=-1 && j<n){
    37                 mapr[i][j]=cnt;
    38                 if(cnt>N) N=cnt;
    39                 j++;
    40             }
    41         }
    42     cnt=0;
    43     for(int j=0;j<n;j++)
    44         for(int i=0;i<n;i++){
    45             while(mapc[i][j]==-1 && i<n) i++;
    46             cnt++;
    47             while(mapc[i][j]!=-1 && i<n){
    48                 mapc[i][j]=cnt;
    49                 if(cnt>M) M=cnt;
    50                 i++;
    51             }
    52         }
    53     for(int i=0;i<n;i++)
    54         for(int j=0;j<n;j++)
    55             if(mapr[i][j]!=-1 && mapc[i][j]!=-1)
    56                 g[mapr[i][j]-1][mapc[i][j]-1]=1;
    57 }
    58 int dfs(int x)
    59 {
    60     for(int i=0;i<M;i++){
    61         if(!vis[i] && g[x][i]){
    62             vis[i]=1;
    63             if(match[i]==-1 || dfs(match[i])){
    64                 match[i]=x;
    65                 return 1;
    66             }
    67         }
    68     }
    69     return 0;
    70 }
    71 int hungary()
    72 {
    73     memset(match,-1,sizeof(match));
    74     int ans=0;
    75     for(int i=0;i<N;i++){
    76         memset(vis,0,sizeof(vis));
    77         if(dfs(i)) ans++;
    78     }
    79     return ans;
    80 }
    81 int main(void)
    82 {
    83     while(scanf("%d",&n),n)
    84     {
    85         for(int i=0;i<n;i++)
    86             scanf("%s",c[i]);
    87         build();
    88         printf("%d
    ",hungary());
    89     }
    90     return 0;
    91 }

     

  • 相关阅读:
    ul不加宽高
    获取元素的外部样式问题
    设置定时器、重启定时器要注意的问题
    php的var关键字
    抽象类(abstract class)和 接口(interface)
    __sleep和__wakeup
    类型约束
    TensorFlow 拾遗
    Datasets and Evaluation Metrics used in Recommendation System
    触龙——可解释推荐系统
  • 原文地址:https://www.cnblogs.com/GO-NO-1/p/3603712.html
Copyright © 2011-2022 走看看