HDU 5093 Battle ships [二分图匹配]

24 篇文章 0 订阅
1 篇文章 0 订阅

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

输入一个n×m的地图,图上 * 号表示空地,o表示浮冰,#表示冰山。每行每列只能放一艘船,有冰山隔开则可以多放。问最多能放多少艘船。


考虑若没有冰山,则只需给每一块空地对应的行结点和列结点连一条边,就是个二分图,求最大匹配即可。

现在多了冰山的条件,把冰山左右两边当做两行,冰山上下当做两列即可。先按行扫一遍给结点做行编号,然后按列扫描连边建图。


#include<iostream>
#include<cassert>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<string>
#include<iterator>
#include<cstdlib>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
using namespace std;
#define debug(x) cout<<"debug "<<x<<endl;
#define rep(i,f,t) for(int i = (f),_end_=(t); i <= _end_; ++i)
#define rep2(i,f,t) for(int i = (f),_end_=(t); i < _end_; ++i)
#define dep(i,f,t) for(int i = (f),_end_=(t); i >= _end_; --i)
#define dep2(i,f,t) for(int i = (f),_end_=(t); i > _end_; --i)
#define clr(c, x) memset(c, x, sizeof(c) )
typedef long long int64;
const int INF = 0x5f5f5f5f;
const double eps = 1e-8;


//*****************************************************
typedef vector<int> Vec;
Vec G[2501];
int mch[2501];
int n,m;
int cnt;
int mp[55][55];

void build()
{
    int id = 1;
    bool flg = false;
    rep(i,1,2500)G[i].clear();

    //按行扫描,做好行编号
    rep(i,1,n)
    {
        rep(j,1,m)
        {
            if(!mp[i][j])continue; //floating ice
            if(mp[i][j] > 0){ //ordinary sea
                mp[i][j] = id;
                flg = true; //次id已有使用
            }else if(flg){
                flg = false;
                ++id;
            }
        }
        if(flg){
            ++id;
            flg = false;
        }
    }
    //******************************************************
    //按列扫描,建图
    rep(j,1,m)
    {
        rep(i,1,n)
        {
            if(!mp[i][j])continue;
            if(mp[i][j] > 0){
                int r = mp[i][j];
                G[r].push_back(id);
                G[id].push_back(r);
                flg = true;
            }else if(flg){
                flg = false;
                ++id;
            }
        }
        if(flg){
            ++id;
            flg = 0;
        }
    }
    cnt = id - 1;
}
bool vis[2501];
bool dfs(int v)
{
    vis[v] = true;
    rep(i,0,G[v].size()-1)
    {
        int u = G[v][i], w = mch[u];
        if( w < 0 || (!vis[w] && dfs(w)) ){
            mch[v] = u;
            mch[u] = v;
            return true;
        }
    }
    return false;
}
int gao()
{
    clr(mch,-1);
    int res = 0;
    rep(i,1,cnt)
    {
        if(mch[i] < 0){
            clr(vis,0);
            if(dfs(i))++res;
        }
    }
    return res;
}
int main()
{
    int cas;
    scanf("%d",&cas);
    while(cas--)
    {
        scanf("%d%d",&n,&m);
        clr(mp,0);
        rep(i,1,n)rep(j,1,m)
        {
            char c;
            scanf(" %c",&c);
            if(c=='#')      mp[i][j] = -1; //iceberg
            else if(c=='*') mp[i][j] = 1; //ordnary sea   用数字代替,简化编码
        }
        build();
        int ans = gao();
        printf("%d\n",ans);
    }

    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值