• [图论][二分图最大匹配]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/
  • 相关阅读:
    jq ajax之beforesend(XHR)
    WPF string,color,brush之间的转换
    wpf后台设置颜色(背景色,前景色)
    VMWare之——宿主机与虚拟机互相ping通,宿主机ping通另一台机器的虚拟机
    动态SQL的执行,注:exec sp_executesql 其实可以实现参数查询和输出参数的
    【SQLSERVER】动态游标的实现
    减小Delphi 2010/delphi XE编译出来的文件大小
    正确理解 SqlConnection 的连接池机制[转]
    关于VS2005中C#代码用F12转到定义时,总是显示从元数据的问题
    通过cmd命令安装、卸载、启动和停止Windows Service(InstallUtil.exe)
  • 原文地址:https://www.cnblogs.com/lllxq/p/9005411.html
Copyright © 2020-2023  润新知