• [图论][二分图最大匹配]Fire Net


    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
     
    思路:超级巧妙啊,先对原图分别进行横向和纵向缩点(横向/纵向连续的一条空地等价与一个可放一间house的空地),那么在所有这些横向缩成点中能放house的点是那些能找到纵向缩成点与之相交的点(想象一下,因为这些缩成点原先是一个条状的),所以就是一个横向缩成点与纵向缩成点之间的二分图的最大匹配问题了(这个建模过程实在是太巧妙了)
    AC代码:
    #include <iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    
    int n;
    char Map[5][5];
    int num_x,num_y;
    int belong_numx[5][5],belong_numy[5][5];
    int Map_[50][50];
    int vis[50];
    int used[50];
    
    bool match(int x){//匈牙利算法
      for(int i=1;i<=num_y;i++){
        if(!vis[i]&&Map_[x][i]){
            vis[i]=1;
            if(!used[i]||match(used[i])){
                used[i]=x;
                return true;
            }
        }
      }
      return false;
    }
    
    int main()
    {
        while(scanf("%d",&n)!=EOF&&n){
            for(int i=1;i<=n;i++){
                for(int j=1;j<=n;j++){
                    scanf(" %c",&Map[i][j]);
                }
            }
            num_x=0,num_y=0;
            for(int i=1;i<=n;i++){
                for(int j=1;j<=n;j++){
                    if(Map[i][j]=='.'){
                        if(j==1||Map[i][j-1]=='X') num_x++;
                        belong_numx[i][j]=num_x;
                    }
                }
            }
            for(int j=1;j<=n;j++){
                for(int i=1;i<=n;i++){
                    if(Map[i][j]=='.'){
                        if(i==1||Map[i-1][j]=='X') num_y++;
                        belong_numy[i][j]=num_y;
                    }
                }
            }
            memset(Map_,0,sizeof(Map_));
            for(int i=1;i<=n;i++){
                for(int j=1;j<=n;j++){
                    if(Map[i][j]=='.') Map_[belong_numx[i][j]][belong_numy[i][j]]=1;//相交的缩成点之间能够进行匹配
                }
            }
            int ans=0;
            memset(used,0,sizeof(used));
            for(int i=1;i<=num_x;i++){
                memset(vis,0,sizeof(vis));
                if(match(i)) ans++;
            }
            printf("%d
    ",ans);
        }
        return 0;
    }
    转载请注明出处:https://www.cnblogs.com/lllxq/
  • 相关阅读:
    LINUX监控一:监控命令
    Kettle并行
    KETTLE集群搭建
    Solr报错Index locked for write for core '***'. Solr now longer supports forceful unlocking via 'unlockOnStartup'
    Solr json,xml等文件数据导入(添加索引)linux下操作
    python对solr进行查询、插入操作(GETPOST)
    Solr-5.3.1 dataimport 导入mysql数据
    解决MySQL数据导入报错Got a packet bigger than‘max_allowed_packet’bytes
    解决防火墙限制远程连接MySQL(导致错误10060可能之一)
    gensim加载word2vec训练结果(bin文件)并进行相似度实验
  • 原文地址:https://www.cnblogs.com/lllxq/p/9005411.html
Copyright © 2020-2023  润新知