zoukankan      html  css  js  c++  java
  • Fire Net (二分图匹配 匈牙利算法模板)

    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

    本题可以使用二分图匹配

    即缩点之后抽象成匹配问题

    裸题 

    ac代码

    #include<bits/stdc++.h>
    using namespace std;
    
    char mp[10][10];
    int vis1[10][10];//竖着匹配
    int vis2[10][10];//横着匹配
    
    
     const int maxn=10;
     int nx,ny;//x集合和y集合中顶点的个数
     int edge[maxn][maxn];//edge[i][j]为1表示ij可以匹配
     int cx[maxn],cy[maxn];//用来记录x集合中匹配的y元素是哪个!
     int visited[maxn];//用来记录该顶点是否被访问过!
     int path(int u)
     {
         int v;
         for(v=1;v<=ny;v++)
         {
             if(edge[u][v]&&!visited[v])
             {
                 visited[v]=1;
                if(cy[v]==-1||path(cy[v]))//如果y集合中的v元素没有匹配或者是v已经匹配,但是从cy[v]中能够找到一条增广路
                 {
                     cx[u]=v;
                     cy[v]=u;
                     return 1;
                 }
             }
         }
         return 0;
     }
     int maxmatch()
     {
         int res=0;
         memset(cx,0xff,sizeof(cx));//初始值为-1表示两个集合中都没有匹配的元素!
         memset(cy,0xff,sizeof(cy));
         for(int i=1;i<=nx;i++)
         {
             if(cx[i]==-1)
             {
                 memset(visited,0,sizeof(visited));
                 res+=path(i);
             }
         }
         return res;
     }
    
    
    int main()
    {
        //freopen("in.txt","r",stdin);
        int n;
        while(~scanf("%d",&n))
        {
            if(n==0) break;
            memset(edge,0,sizeof(edge));
            memset(vis1,0,sizeof(vis1));
            memset(vis2,0,sizeof(vis2));
            for(int i=1; i<=n; i++)
            {
                scanf("%s",mp[i]+1);
            }
            int cnt1=0;
            int cnt2=0;
           // cout<<"d"<<endl;
            for(int i=1; i<=n; i++) //纵向缩点
            {
                for(int j=1; j<=n; j++)
                {
                    if(vis1[i][j]==0&&mp[i][j]=='.')
                    {
                        cnt1++;
                        vis1[i][j]=cnt1;
                        int tmp=i;
                        tmp++;
                        while(tmp<=n)
                        {
                            if(mp[tmp][j]=='.') vis1[tmp][j]=cnt1;
                            else break;
                            tmp++;
                        }
                    }
                }
            }
            //cout<<"d"<<endl;
            for(int i=1; i<=n; i++)
            {
                for(int j=1; j<=n; j++)
                {
                    if(vis2[i][j]==0&&mp[i][j]=='.')
                    {
                        cnt2++;
                        vis2[i][j]=cnt2;
                        int tmp=j;
                        tmp++;
                        while(tmp<=n)
                        {
                            if(mp[i][tmp]=='.') vis2[i][tmp]=cnt2;
                            else break;
                            tmp++;
                        }
                    }
                }
            }
            //cout<<"d"<<endl;
            nx=cnt2;
            ny=cnt1;
            //cout<<nx<<" "<<ny<<endl;
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=n;j++)
                {
                    if(mp[i][j]=='.')
                    {
                        edge[vis2[i][j]][vis1[i][j]]=1;
                        //cout<<"cao:"<<i<<j<<" "<<vis2[i][j]<<" "<<vis1[i][j]<<endl;
                    }
                }
            }
            printf("%d
    ",maxmatch());
        }
    }
  • 相关阅读:
    微信小程序保存图片功能实现
    小程序清除缓存功能如何实现
    小程序自定义函数—数字千位转换
    小程序身份证号检测函数
    小程序 的 textarea 组件 层级问题如何解决
    Markdown 语法背一下咯
    跨域了解一下?
    sort命令的k选项大讨论【转】
    Shell脚本中实现切换用户并执行命令操作【转】
    Ansible Tower系列 四(使用tower执行一个命令)【转】
  • 原文地址:https://www.cnblogs.com/caowenbo/p/11852266.html
Copyright © 2011-2022 走看看