HDU 1045 Fire Net(建模,用二分匹配)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/liar771/article/details/54341547

参考kuangbin教主的解法,链接http://www.cnblogs.com/kuangbin/archive/2011/08/09/2132830.html。

但是感觉他的建模有点问题.

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. 
InputThe 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.
OutputFor each test case, output one line containing the maximum number of blockhouses that can be placed in the city in a legal configuration. 

题意,输入n和n*n的地图,"."表示路径(可以放置炮台),“X”表示墙壁

炮台可以攻击所在一列一排,墙面可以挡住炮台子弹。每个放置的炮台不能被其他炮台攻击到。

每个点有两个属性,攻击到的横排和攻击到的竖排。

可以将地图转化两组数据,第一组,连续的横排路径看作一个点(一个炮塔攻击到的横排),第二组,连续的竖列路径看作一个点。

一组和二组的数据组合可以构成一个个炮台放置点。然后就构造出一个二分图,问题转换为求二分图最大匹配,用匈牙利算法解决。

非常好的题,有三种解法,用贪心做。dfs暴力搜。也可以用二分匹配,涉及二分图构造和最大匹配。

二分构造和匹配:

#include<bits/stdc++.h>
using namespace std;
const int u=10,v=10;
int n,mpx[u][v],mpy[u][v],link[u],x,y;
bool use[u][v],g[u][v];
bool connect(int z)
{
    for(int  i=1; i<=y; i++)
    {
        if(g[z][i]&&!use[z][i])
        {
            use[z][i]=true;
            if(link[i]==0||connect(link[i]))
            {
                link[i]=z;
                return true;
            }
        }
    }
    return false;
}
int dfs()
{
    int ans=0;
    for(int i=1; i<=x; i++)
    {
        memset(use,0,sizeof(use));
        if(connect(i))ans++;
    }
    return ans;
}
int main()
{
    while(~scanf("%d",&n)&&n)
    {
        memset(mpx,0,sizeof(mpx));
        memset(mpy,0,sizeof(mpy));
        memset(g,0,sizeof(g));
        memset(link,0,sizeof(link));
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)
            {
                char te;
                cin>>te;
                if(te=='X')
                    mpx[i][j]=mpy[i][j]=-1;
            }
        x=y=0;
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)
            {
                while(mpx[i][j]==-1&&j<=n)
                    j++;
                if(mpx[i][j]!=-1&&j<=n)
                {
                    x++;
                    while(mpx[i][j]!=-1&&j<=n)
                        mpx[i][j]=x,j++;
                }
            }
        for(int j=1; j<=n; j++)
            for(int i=1; i<=n; i++)
            {
                while(mpy[i][j]==-1&&i<=n)
                    i++;
                if(mpy[i][j]!=-1&&i<=n)
                {
                    y++;
                    while(mpy[i][j]!=-1&&i<=n)
                        mpy[i][j]=y,i++;
                }
            }
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)
            {
                if(mpx[i][j]!=-1&&mpy[i][j]!=-1)
                    g[mpx[i][j]][mpy[i][j]]=1;
            }
//        for(int i=1; i<=n; i++)
//        {
//            printf("\n");
//            for(int j=1; j<=n; j++)
//                printf("%d ",mpx[i][j]);
//        }
//        printf("\n");
//        for(int i=1; i<=n; i++)
//        {
//            printf("\n");
//            for(int j=1; j<=n; j++)
//                printf("%d ",mpy[i][j]);
//        }
//        printf("\n");
//        for(int i=1; i<=x; i++)
//        {
//            printf("\n");
//            for(int j=1; j<=y; j++)
//                printf("%d ",g[i][j]);
//        }
//        printf("\n");
        printf("%d\n",dfs());
    }
}




猜你喜欢

转载自blog.csdn.net/liar771/article/details/54341547