Fire Net ——二分匹配

Fire Net

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12315    Accepted Submission(s): 7438


 

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

这道题以前做搜索专题的时候出现过,没想到还能用二分匹配做 ,建图的时候确实有点困难,卡了我好久,一直不是很明白它是怎么建图的,不过,好歹我看了好久的博客,也算是有点了解了,现在我就把思路讲的清晰点,希望能给那些,被其他博客一句缩点搞得晕头转向的人一些帮助。这道题是将二分图分为行列两部分,如果可以放点,说明该点不是墙,同时还是行列交叉的地方,,此时我们如果提前设置好了一个行集合以及一个列集合就可以知道,这点同时还是行集合与列集合的交点,因此这道题就抽象为了求行集合与列集合的最大匹配了,那么,怎么求行集合与列集合呢?这就是大部分博客里面说的缩点,其实就是在遍历每一点的时候,把多个点当做行集合里面的一个点,因为我们知道每一行只能有一个点,除非中间有墙的存在,所以如果一行里面没有墙,那么这一行就算是一点,其实也就是这一行的元素的值都一样,因此看做是一个集合里面的点,所以,按照这个道理,我们就可以依次得到行列两个集合的点,然后我们在用匈牙利算法,求得最大匹配就是答案了,详情看代码。

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
char s[10][10];
int row[10][10],col[10][10];
int e[10][10];
int match[10];
bool book[10];
int p1,p2;
bool dfs(int u)
{
    for(int i=1; i<=p2; i++)
    {
        if(!book[i]&&e[u][i])
        {
            book[i]=true;
            if(match[i]==0||dfs(match[i]))
            {
                match[i]=u;
                return true;
            }
        }
    }
    return false;
}
int main()
{
    int n;
    while(~scanf("%d",&n)&&n)
    {
        mem(match,0);
        mem(e,0);
        mem(col,0);
        mem(row,0);
        for(int i=1; i<=n; i++)
            scanf("%s",s[i]+1);
        p1=0,p2=0;//获得顶点的数量
        //获得行集合的编号
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n; j++)
            {
                while(s[i][j]=='X'&&j<=n)//列在变
                {
                    j++;
                }
                p1++;
                while(s[i][j]!='X'&&j<=n)
                {
                    row[i][j]=p1;
                    j++;
                }
            }
        }
        //获得列集合的编号
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n; j++)
            {
                while(s[j][i]=='X'&&j<=n)//行在变
                {
                    j++;
                }
                p2++;
                while(s[j][i]!='X'&&j<=n)
                {
                    col[j][i]=p2;
                    j++;
                }
            }
        }
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n; j++)
            {
                if(s[i][j]!='X')
                {
                    e[row[i][j]][col[i][j]]=1;
                }
            }
        }
        int ans=0;
        for(int i=1; i<=p1; i++)
        {
            mem(book,0);
            if(dfs(i))ans++;
        }
        printf("%d\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40510246/article/details/81271422