(匈牙利算法) hdu 5093

Battle ships

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

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

关键在于建图

横向和纵向的连分块分别++tot,连边,然后直接匈牙利算法

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<cstdlib>
using namespace std;
int n,m,tot;
char s[51][51];
int dx[51][51],dy[51][51],xxn,mp[2510][2510],yyn,link[2510],mark[2510];
bool dfs(int x)
{
    for(int i=1;i<=yyn;i++)
    {
        if(mp[x][i]&&mark[i]==-1)
        {
            mark[i]=1;
            if(link[i]==-1||dfs(link[i]))
            {
                link[i]=x;
                return true;
            }
        }
    }
    return false;
}
int main()
{
    int tt;
    scanf("%d",&tt);
    while(tt--)
    {
        scanf("%d%d",&n,&m);
        bool flag;
        tot=0;
        memset(mp,0,sizeof(mp));
        memset(link,-1,sizeof(link));
        memset(dx,0,sizeof(dx));
        memset(dy,0,sizeof(dy));
        for(int i=0;i<n;i++)
            scanf("%s",s[i]);
        for(int i=0;i<n;i++)
        {
            flag=false;
            for(int j=0;j<m;j++)
            {
                if(s[i][j]==‘*‘)
                {
                    if(!flag)
                        dx[i][j]=++tot,flag=true;
                    else
                        dx[i][j]=tot;
                }
                else if(s[i][j]==‘#‘)
                    flag=false;
            }
        }
        xxn=tot;
        tot=0;
        for(int j=0;j<m;j++)
        {
            flag=false;
            for(int i=0;i<n;i++)
            {
                if(s[i][j]==‘*‘)
                {
                    if(!flag)
                    {
                        dy[i][j]=++tot;
                        flag=true;
                    }
                    else
                        dy[i][j]=tot;
                }
                else if(s[i][j]==‘#‘)
                    flag=false;
            }
        }
        yyn=tot;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                int xx,yy;
                xx=dx[i][j],yy=dy[i][j];
                if(xx&&yy)
                    mp[xx][yy]=1;
            }
        }
        int ans=0;
        for(int i=1;i<=xxn;i++)
        {
            memset(mark,-1,sizeof(mark));
            if(dfs(i))
                ans++;
        }
        printf("%d\n",ans);
    }
    return 0;
}

  

时间: 2024-11-07 22:21:23

(匈牙利算法) hdu 5093的相关文章

(匈牙利算法) hdu 1281

棋盘游戏 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2757    Accepted Submission(s): 1612 Problem Description 小希和Gardon在玩一个游戏:对一个N*M的棋盘,在格子里放尽量多的一些国际象棋里面的“车”,并且使得他们不能互相攻击,这当然很简单,但是Gardon限制了只有某些格

(二分+匈牙利算法) hdu 2236

无题II Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1198    Accepted Submission(s): 535 Problem Description 这是一个简单的游戏,在一个n*n的矩阵中,找n个数使得这n个数都在不同的行和列里并且要求这n个数中的最大值和最小值的差值最小. Input 输入一个整数T表示T组数据.对

(tarjan+匈牙利算法) hdu 3861

F - The King’s Problem Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 3861 Description In the Kingdom of Silence, the king has a new problem. There are N cities in the kingdom and there are M d

(匈牙利算法) hdu 2119

#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<cmath> #include<algorithm> #include<cstdlib> using namespace std; int n,m,a[110][110],g[110][110],mark[110],link[110],ans; bool dfs(i

(匈牙利算法) hdu 4185

Oil Skimming Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 993    Accepted Submission(s): 422 Problem Description Thanks to a certain "green" resources company, there is a new profitable

(匈牙利算法) hdu 2063

过山车 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 12125    Accepted Submission(s): 5302 Problem Description RPG girls今天和大家一起去游乐场玩,终于可以坐上梦寐以求的过山车了.可是,过山车的每一排只有两个座位,而且还有条不成文的规矩,就是每个女生必须找个个男生做par

HDU 2063:过山车(二分匹配,匈牙利算法)

过山车 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 9745    Accepted Submission(s): 4294 Problem Description RPG girls今天和大家一起去游乐场玩,终于可以坐上梦寐以求的过山车了.可是,过山车的每一排只有两个座位,而且还有条不成文的规矩,就是每个女生必须找个个男生做par

HDU 1150:Machine Schedule(二分匹配,匈牙利算法)

Machine Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5371    Accepted Submission(s): 2658 Problem Description As we all know, machine scheduling is a very classical problem in compu

HDU 5943 Kingdom of Obsession 【二分图匹配 匈牙利算法】 (2016年中国大学生程序设计竞赛(杭州))

Kingdom of Obsession Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 49    Accepted Submission(s): 14 Problem Description There is a kindom of obsession, so people in this kingdom do things very