(匈牙利算法) hdu 5093

Battle ships

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


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
 

 关键在于建图

横向和纵向的连分块分别++tot,连边,然后直接匈牙利算法

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<cstdlib>
using namespace std;
int n,m,tot;
char s[51][51];
int dx[51][51],dy[51][51],xxn,mp[2510][2510],yyn,link[2510],mark[2510];
bool dfs(int x)
{
    for(int i=1;i<=yyn;i++)
    {
        if(mp[x][i]&&mark[i]==-1)
        {
            mark[i]=1;
            if(link[i]==-1||dfs(link[i]))
            {
                link[i]=x;
                return true;
            }
        }
    }
    return false;
}
int main()
{
    int tt;
    scanf("%d",&tt);
    while(tt--)
    {
        scanf("%d%d",&n,&m);
        bool flag;
        tot=0;
        memset(mp,0,sizeof(mp));
        memset(link,-1,sizeof(link));
        memset(dx,0,sizeof(dx));
        memset(dy,0,sizeof(dy));
        for(int i=0;i<n;i++)
            scanf("%s",s[i]);
        for(int i=0;i<n;i++)
        {
            flag=false;
            for(int j=0;j<m;j++)
            {
                if(s[i][j]=='*')
                {
                    if(!flag)
                        dx[i][j]=++tot,flag=true;
                    else
                        dx[i][j]=tot;
                }
                else if(s[i][j]=='#')
                    flag=false;
            }
        }
        xxn=tot;
        tot=0;
        for(int j=0;j<m;j++)
        {
            flag=false;
            for(int i=0;i<n;i++)
            {
                if(s[i][j]=='*')
                {
                    if(!flag)
                    {
                        dy[i][j]=++tot;
                        flag=true;
                    }
                    else
                        dy[i][j]=tot;
                }
                else if(s[i][j]=='#')
                    flag=false;
            }
        }
        yyn=tot;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                int xx,yy;
                xx=dx[i][j],yy=dy[i][j];
                if(xx&&yy)
                    mp[xx][yy]=1;
            }
        }
        int ans=0;
        for(int i=1;i<=xxn;i++)
        {
            memset(mark,-1,sizeof(mark));
            if(dfs(i))
                ans++;
        }
        printf("%d
",ans);
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/water-full/p/4460070.html