hdu5093

Battle ships

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


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
 

Source

给你一张 n*m 的矩阵图形  ,‘ * ’ 表示 空地 , ‘ o ’ 表示浮冰  , ‘ # ’ 表示 冰山  , 你可以在空地上方战舰 ,不可以在有浮冰的地方放战舰, 但是同一行或同一列只能放一个战舰。若两个战舰想放在同一行或同一列上,那么它们之间必须要有冰山相隔。然后问你在这张图上最多可以放多少个战舰 ?

如果是没有冰山,那就是简单的二分图匹配了,行与列直接相连,但是加了冰山,一行可以放多个,一列也可以放多个,用二分图换匹配还能不能解呢?能!

划分区域,将每一行每一列划分区域,每一行相连的空地划分为同一个区域并编号,每一列划相连区域分为同于区域并编号,50*50的矩阵,区域不会超过1250个,我随便开了一两个1500*1500的 区域矩阵,一个表示行区域,一个表示列局区域;

就以样例 一为例:


* o o o            行集合(将 ’ * ‘标号)          1 o o o                   列集合        1 o o o

o # # #                                                  o # # #                                      o # # #

* * #  *                                                    2 2 # 3                                     1 2 # 3

o o o *                                                   o o o 4                                      o o o 3

由于这样代码实现有一点复杂,其实可以遇见#就给区域号加一,反正同一区域的编号是相同即可,详见代码

建边时 ,将空地 * 的行区域号与列区域号连边即可;



ac代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
const int maxn=1500;
int uN;
int ma[maxn][maxn];
int vis[maxn],linker[maxn];
///二分图匹配
bool dfs(int u)
{
    for(int v=1;v<=uN;v++)
    {
        if(!vis[v]&&ma[u][v])
        {
            vis[v]=1;
            if(linker[v]==-1||dfs(linker[v]))
            {
                linker[v]=u;
                return true;
            }
        }
    }
    return false;
}
int solve()
{
    int ans=0;
    memset(linker,-1,sizeof(linker));
    for(int i=1;i<=uN;i++)
    {
        memset(vis,0,sizeof(vis));
        if(dfs(i))
            ans++;
    }
    return ans;
}
int n,m;
int c[55][55],l[55][55];
char str[55][55];
void creat_ma()///建边
{
    int tot=1;
    uN=0;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(str[i][j]=='*')
                c[i][j]=tot;
            if(str[i][j]=='#')
                tot++;
        }
        tot++;
    }
    if(tot>uN)
        uN=tot;
    tot=1;
    for(int i=0;i<m;i++)
    {
        for(int j=0;j<n;j++)
        {
            if(str[j][i]=='*')
                l[j][i]=tot;
            if(str[j][i]=='#')
                tot++;
        }
        tot++;
    }
    if(uN<tot)
        uN=tot;
    memset(ma,0,sizeof(ma));
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(str[i][j]=='*')
            ma[c[i][j]][l[i][j]]=1;

        }
    }
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(str[i][j]=='*')
                cout<<c[i][j];
            else
                cout<<str[i][j];

        }
        cout<<endl;
    }
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(str[i][j]=='*')
                cout<<l[i][j];
            else
                cout<<str[i][j];

        }
        cout<<endl;
    }
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        cin>>n>>m;
        for(int i=0;i<n;i++)
            cin>>str[i];
        creat_ma();
        cout<<solve()<<endl;
    }
    return 0;
}

/**
2
4 4
*ooo
o###
**#*
ooo*
行区域编号

1ooo
o###
66#7
ooo8

列区域编号

1ooo
o###
13#8
ooo8

*/




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值