Hdu 5093 Battle ships【思维+二分图匹配】

Battle ships

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


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表示碎冰,#表示冰块。

其中o和#不能放置船只,*可以,我们想要放置尽可能多的船只。

现在有一个规则,一行和一列只能放置一只船只,除非两只船只之间可以有一个或者多个#.


思路:


我们一行一行的去刷,能够刷在一起的,就只能放置一只船只。

我们再一列一列的去刷,能够刷在一起的,就只能放置一只船只。


那么我们此时行列编号求一个最大匹配即可。

对于重叠部分的一行一列,只能选择一只船只放置,所以直接最大二分匹配就是ans.



Ac代码:


#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
vector<int>mp[100000];
int match[100000];
int vis[30000];
char a[66][66];
int row[66][66];
int col[66][66];
int find(int u){
    for(int i=0;i<mp[u].size();i++){
        int v=mp[u][i];
        if(vis[v]==0){
            vis[v]=1;
            if(match[v]==-1||find(match[v])){
                match[v]=u;
                return 1;
            }
        }
    }
    return 0;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int colnum=1;
        int rownum=1;
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=90000;i++)mp[i].clear();
        for(int i=0;i<n;i++)scanf("%s",a[i]);
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(a[i][j]=='*')
                {
                    while(j+1<m&&a[i][j+1]=='o'||a[i][j+1]=='*')
                    {
                        row[i][j]=rownum;
                        j++;
                    }
                    row[i][j]=rownum;
                    rownum++;
                }
            }
        }
        for(int i=0;i<m;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(a[j][i]=='*')
                {
                    while(j+1<n&&a[j+1][i]=='o'||a[j+1][i]=='*')
                    {
                        col[j][i]=colnum;
                        j++;
                    }
                    col[j][i]=colnum;
                    colnum++;
                }
            }
        }
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(a[i][j]=='*')
                mp[row[i][j]].push_back(col[i][j]);
            }
        }
        int output=0;
        memset(match,-1,sizeof(match));
        for(int i=1;i<=rownum;i++)
        {
            memset(vis,0,sizeof(vis));
            if(find(i))output++;
        }
        printf("%d\n",output);
    }
}











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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值