Battle ships HDU - 5093(二分图最大匹配)

在极地恶劣条件下,海军指挥官面临战舰布局的挑战,需避开冰山和浮冰,遵循特定规则安排尽可能多的战舰。通过构建二分图模型,采用网络流算法寻找最大匹配,实现最优布局。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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)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

看出了是二分图的模型,在建图上出现了点问题,后来发现改正,把连续的行和连续的列抽象成点,如果有公共点,则行和列连线,找一个最大匹配,这个匹配我是用网络流来实现的,代码就比较的长了。
关键是如何建边。我的方法是给每个点做一个个标记是属于哪个行点和列点。然后建行和列的边。
代码:

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn=5505;
const int maxx=20010;
int edge;
int to[maxx],flow[maxx],nex[maxx];
int head[maxn];

void addEdge(int v,int u,int cap)
{
    to[edge]=u,flow[edge]=cap,nex[edge]=head[v],head[v]=edge++;
    to[edge]=v,flow[edge]=0,nex[edge]=head[u],head[u]=edge++;
}
int vis[maxn];
bool bfs(int s,int e)
{
    queue<int> que;
    memset(vis,-1,sizeof(vis));
    que.push(s);
    vis[s]=0;
    while(!que.empty())
    {
        int u=que.front();
        que.pop();
        for(int i=head[u];~i;i=nex[i])
        {
            int v=to[i];
            if(vis[v]==-1&&flow[i])
            {
                vis[v]=vis[u]+1;
                if(v==e)
                    return true;
                que.push(v);
            }

        }
    }
    return false;
}
int dfs(int s,int t,int f)
{
    if(s==t||!f)
        return f;
    int r=0;
    for(int i=head[s];~i;i=nex[i])
    {
        int v=to[i];
        if(vis[v]==vis[s]+1&&flow[i])
        {
            int d=dfs(v,t,min(f,flow[i]));
            if(d>0)
            {
                flow[i]-=d;
                flow[i^1]+=d;
                r+=d;
                f-=d;
                if(!f)
                    break;
            }
        }
    }
    if(!r)
        vis[s]=INF;
    return r;
}
int maxFlow(int s ,int e)
{
    int ans=0;
    while(bfs(s,e))
        ans+=dfs(s,e,INF);
    return ans;
}

void init()
{
    memset(head,-1,sizeof(head));
    edge=0;
}
char s[55][55];
int t,n,m;
int a[55][55];
int b[55][55];
int main()
{

    cin>>t;
    while(t--)
    {
        init();
        cin>>n>>m;
        //cout<<"0 0: "<<get(0,0)<<endl;
        for(int i=1;i<=n;i++)
            scanf("%s",s[i]+1);

        int cur=0;
        for(int i=1;i<=n;i++)
        {
            int now=0;
            bool sign=false;
            while(now<=m)
            {
                if(s[i][now]=='*')
                {
                    if(!sign)
                    {
                        cur++;
                        sign=true;
                        addEdge(0,cur,1);

                    }
                    a[i][now]=cur;//这个点的左边和cur相连
                }
                else
                    if(s[i][now]=='#')
                    {
                        if(sign)
                            sign=false;
                    }
                now++;
            }
        }
        int e=n*m*2+1;
        for(int i=1;i<=m;i++)
        {
            int now=0;
            bool sign=false;
            while(now<=n)
            {
                if(s[now][i]=='*')
                {
                    if(!sign)
                    {
                        cur++;
                        sign=true;
                        addEdge(cur,e,1);
                    }
                    b[now][i]=cur;这个点的右边和cur相连
                }
                else
                    if(s[now][i]=='#')
                    {
                        if(sign)
                            sign=false;
                    }
                now++;
            }
        }
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
                if(s[i][j]=='*')addEdge(a[i][j],b[i][j],1);//就这样连边了
        cout<<maxFlow(0,e)<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值