HDU - 5093(Battle ships)

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


 

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是浮冰

*ooo
o###
**#*
ooo*

将所有按行标号海

1000

0000

2203

0004

列也是规则一样的建图,是一种经典建图法。

#include <set>
#include <map>
#include <deque>
#include <stack>
#include <queue>
#include <time.h>
#include <vector>
#include <string>
#include <math.h>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <iomanip>
#include <iostream>
#include <algorithm>
#define PI acos(-1)
#define ll long long
#define LL long long
#define inf 0x3f3f3f3f
#define ull unsigned long long
using namespace std;
const int maxn  =550 ;
const int maxe  =90500 ;
bool inpath[maxe];
int match[maxe];
int head[maxe];
int edgeNum;
struct Edge{
    int to,next;
}edge[maxe];
int m,n;
void addEdge(int a,int b){
    edge[edgeNum].to=b;
    edge[edgeNum].next=head[a];
    head[a]=edgeNum++;
}
bool findpath(int k){
    // inpath[k]=true; //inpath储存的是增广路,本题中标记的应该是男生,
    for(int i=head[k];i!=-1;i=edge[i].next){
        int j=edge[i].to;
        if(!inpath[j]){
            inpath[j]=true; //所以在这里标记
            //如果没有匹配,直接匹配,
            //如果匹配了,,看看她的匹配能否找到新的匹配
            if(match[j]==-1||findpath(match[j])){
                match[j]=k;return true;
            }
        }
    }
    return false;
}
int y;
void hungary()
{
    int cnt=0;
    for(int i=1;i<=y;i++){
        memset(inpath,0,sizeof(inpath));
        //从每一个节点开始找增广路,增广路都要清空,
        //但是match不要清空,
        if(findpath(i)){
            cnt++;
        }
    }
    cout<<cnt<<endl;
}
void init()
{
    memset(inpath,false,sizeof(inpath));
    memset(match,-1,sizeof(match));
    memset(head,-1,sizeof(head));
    edgeNum=0;
}
char s[55][55];
int a[55][55],b[55][55];
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++) scanf("%s",s[i]+1);
        //  #山   *海      o冰
        init();
        int cnt=1;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(s[i][j]=='*') a[i][j]=cnt;
                else if(s[i][j]=='#')
                {
                    if(j!=1&&j!=m) cnt++;
                }
            }
            cnt++;
        }
        y=cnt;
        cnt=1;
        for(int j=1;j<=m;j++)
        {
            for(int i=1;i<=n;i++)
            {
                if(s[i][j]=='*') b[i][j]=cnt;
                else if(s[i][j]=='#')
                {
                    if(i!=1&&i!=n) cnt++;
                }
            }
            cnt++;
        }
        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]);
            }
        }
        hungary();
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值