【HDU】5093Battle ships(二分图最大匹配问题)

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2001    Accepted Submission(s): 726


 

Problem Description

Dear contestant, now you are an excellent navy commander, who is responsible of a tough mission currently.

Your fleet unfortunately encountered an enemy fleet near the South Pole where the geographical conditions are negative for both sides. The floating ice and iceberg blocks battleships move which leads to this unexpected engagement highly dangerous, unpredictable and incontrollable.

But, fortunately, as an experienced navy commander, you are able to take opportunity to embattle the ships to maximize the utility of cannons on the battleships before the engagement.

The target is, arrange as many battleships as you can in the map. However, there are three rules so that you cannot do that arbitrary:

A battleship cannot lay on floating ice
A battleship cannot be placed on an iceberg

Two battleships cannot be arranged in the same row or column, unless one or more icebergs are in the middle of them.

 

Input

There is only one integer T (0<T<12) at the beginning line, which means following T test cases.

For each test case, two integers m and n (1 <= m, n <= 50) are at the first line, represents the number of rows and columns of the battlefield map respectively. Following m lines contains n characters iteratively, each character belongs to one of ‘#’, ‘*’, ‘o’, that symbolize iceberg, ordinary sea and floating ice.

 

Output

For each case, output just one line, contains a single integer which represents the maximal possible number of battleships can be arranged.

 

Sample Input

 

2 4 4 *ooo o### **#* ooo* 4 4 #*** *#** **#* ooo#

 

Sample Output

 

3 5

 

Source

2014上海全国邀请赛——题目重现(感谢上海大学提供题目)

 

Recommend

hujie

题目大意:你现在是一个船长,很多船的船长,现在你有一份海面上的可以停船的地图,其中有的地方现在是冰川,有的地方是浮冰,有的地方还是普通海面,停船时,每一行每一列只能停一艘船,但是现在给出:若中间由冰川隔开,则可以停靠多艘船,

地图上:# 冰川   ------o 浮冰  ------ *普通海洋

思路:把每一行和每一列的可行位置作为二分图的两个点集,这样二分图的最大匹配数量就是最大停船数量

效果如图:

代码:

#include<cstring>
#include<cstdio>
#include<algorithm>
#define maxn 55
using namespace std;
int map[maxn*55][maxn*55];
char str[maxn][maxn];
int used[maxn*maxn],link[maxn*maxn];
int x[maxn][maxn],y[maxn][maxn];
int x1,y1;
int n,m;

void init()
{
    memset(map,0,sizeof(map));
    memset(x,0,sizeof(x));
    memset(y,0,sizeof(y));
}
void input()
{
    scanf("%d%d",&n,&m);
    for(int i=0; i<n; i++)
        scanf("%s",str[i]);
}
void creatx()
{
    x1=1;
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<m; j++)
        {
            if(str[i][j]=='*')
                x[i][j]=x1;
            if(str[i][j]=='#')
                x1++;
        }
        x1++;
    }
    return ;
}
void creaty()
{
    y1=1;
    for(int j=0; j<m; j++)
    {
        for(int i=0; i<n; i++)
        {
            if(str[i][j]=='*')
                y[i][j]=y1;
            if(str[i][j]=='#')
                y1++;
        }
        y1++;
    }
    return ;
}
void getmap()
{
    for(int i=0; i<n; i++)
        for(int j=0; j<m; j++)
            if(str[i][j]=='*')
                map[x[i][j]][y[i][j]]=1;
    return ;
}


bool dfs(int x)
{
    for(int i = 1; i < y1; ++i)
    {
        if(map[x][i] && !used[i])
        {
            used[i] = 1;
            if(link[i] == -1 || dfs(link[i]))
            {
                link[i] = x;
                return true;
            }
        }
    }
    return false;
}

int hungary()
{
    int ans = 0;
    memset(link, -1, sizeof(link));
    for(int j = 1; j < x1; ++j)
    {
        memset(used, 0, sizeof(used));
        if(dfs(j))
            ans++;
    }
    return ans;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        init();
        input();
        creatx();
        /*for(int i = 0; i < n; ++i)
        {
            for(int j = 0; j < m; ++j)
                printf("%d  ", x[i][j]);
            printf("\n");
        }*/
//printf("\n");
        creaty();
        /*for(int i = 0; i < n; ++i)
        {
            for(int j = 0; j < m; ++j)
                printf("%d  ", y[i][j]);
            printf("\n");
        }*/

        getmap();

        int ans=hungary();
        printf("%d\n",ans);
    }
}


猜你喜欢

转载自blog.csdn.net/wentong_Xu/article/details/86549452