HDU 5093 Battle ships [二分图匹配] [匈牙利算法]

Battle ships
Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u

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

典型的二维矩阵转化为二分图匹配的问题。。
要放置尽量多的ships,但是同一行同一列除非有iceberg隔开只能放一只,并且ships只能放在普通海域上。
那么预处理的时候找出相连的“块”然后建立行和列的二分图,然后求最大匹配数即可。。
编译一次成功且1A。。。。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<string>
#include<iomanip>
#include<ctime>
#include<climits>
#include<cctype>
#include<algorithm>
#ifdef WIN32
#define AUTO "%I64d"
#else
#define AUTO "%lld"
#endif
using namespace std;
#define smax(x,tmp) x=max((x),(tmp))
#define smin(x,tmp) x=min((x),(tmp))
#define maxx(x1,x2,x3) max(max(x1,x2),x3)
#define minn(x1,x2,x3) min(min(x1,x2),x3)
const int INF=0x3f3f3f3f;
const int maxn = 52;
struct Edge
{
    int to,next;
}edge[maxn*maxn<<1];
int head[maxn*maxn];
int maxedge;
inline void addedge(int u,int v)
{
    edge[++maxedge] = (Edge) { v,head[u] };
    head[u] = maxedge;
}
int n,m;
int g[maxn][maxn],cpy[maxn][maxn];
int row,col;
int r[maxn][maxn],c[maxn][maxn];
void init()
{
    scanf("%d%d",&m,&n);
    memset(head,-1,sizeof(head));
    maxedge = -1;
    char ch;
    for(int i=1;i<=m;i++)
        for(int j=1;j<=n;j++)
        {
            ch = getchar();
            while(ch^'#' && ch^'o' && ch^'*') ch = getchar();
            if(ch=='#') cpy[i][j]=1; // iceberg
            if(ch=='*') cpy[i][j]=0; // sea
            if(ch=='o') cpy[i][j]=-1; // floating ice
        }
    memcpy(g,cpy,sizeof(cpy));
    memset(r,0,sizeof(r));
    memset(c,0,sizeof(c));
    row=col=0;
    for(int i=1;i<=m;i++)
        for(int j=1;j<=n;j++)
            if(!g[i][j])
            {
                if(j==1 || g[i][j-1]==1) row++;
                r[i][j]=row;
            }
            else if(j==1 || g[i][j-1]==1) g[i][j]=1;
    memcpy(g,cpy,sizeof(cpy));
    for(int j=1;j<=n;j++)
        for(int i=1;i<=m;i++)
            if(!g[i][j])
            {
                if(i==1 || g[i-1][j]==1) col++;
                c[i][j]=col;
            }
            else if(i==1 || g[i-1][j]==1) g[i][j]=1;
    for(int i=1;i<=m;i++)
        for(int j=1;j<=n;j++)
            if(!cpy[i][j]) addedge(r[i][j],c[i][j]);
}
int match[maxn*maxn];
bool vis[maxn*maxn];
bool dfs(int u)
{
    for(int i=head[u];~i;i=edge[i].next)
    {
        int v = edge[i].to;
        if(vis[v]) continue;
        vis[v]=true;
        if(!match[v] || dfs(match[v]))
        {
            match[v] = u;
            return true;
        }
    }
    return false;
}
int hungary()
{
    int ret=0;
    memset(match,0,sizeof(match));
    for(int i=1;i<=row;i++)
    {
        memset(vis,0,sizeof(vis));
        if(dfs(i)) ret++;
    }
    return ret;
}
int main()
{
    int cas;
    scanf("%d",&cas);
    while(cas--)
    {
        init();
        int ans = hungary();
        printf("%d\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值