hdu 5093 Battle ships(二分图匹配)

来源:互联网 发布:淘宝卖家退款流程 编辑:程序博客网 时间:2024/06/07 02:34

Battle ships

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



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
24 4*oooo###**#*ooo*4 4#****#****#*ooo#
 

Sample Output
35
 

Source
2014上海全国邀请赛——题目重现(感谢上海大学提供题目)
 

题意:在n*m的矩阵中,*代表可以放的船只,o和#代表不能放,同一行列除非间隔#,不然不能放两船只,问最多船只数量。

思路:若没有#号,很容易就可想到二分图匹配算法,在假了限制条件的基础上,只需要将行列重新定义,有#就分隔成不同行列就好

代码:
#include<bits/stdc++.h>using namespace std;const int MAXN=1505;//这个值要超过两边个数的较大者,因为有linkerint linker[MAXN];bool used[MAXN];vector<int>mp[MAXN];int uN=MAXN;char s[MAXN][MAXN];int x[MAXN][MAXN],y[MAXN][MAXN];bool dfs(int u){    for(int i=0;i<mp[u].size();i++)    {        if(!used[mp[u][i]])        {            used[mp[u][i]]=true;            if(linker[mp[u][i]]==-1||dfs(linker[mp[u][i]]))            {                linker[mp[u][i]]=u;                return true;            }        }    }    return false;}int hungary(){    int u;    int res=0;    memset(linker,-1,sizeof(linker));    for(u=0;u<uN;u++)    {        memset(used,false,sizeof(used));        if(dfs(u)) res++;    }    return res;}int main(){    int T;    scanf("%d",&T);    while(T--)    {        int n,m;        scanf("%d%d",&n,&m);        for(int i=1;i<=n;i++)            scanf("%s",s[i]+1);        for(int i=0;i<uN;i++)            mp[i].clear();        int len=0;        for(int i=1;i<=n;i++)        {            int flag=0;            len++;            for(int j=1;j<=m;j++)            {                if(s[i][j]=='*')                {                    if(flag)                        len++;                    flag=0;                    x[i][j]=len;                }                if(s[i][j]=='#')                {                    flag=1;                }            }        }        len=0;        for(int j=1;j<=n;j++)        {            int flag=0;            len++;            for(int i=1;i<=m;i++)            {                if(s[i][j]=='*')                {                    if(flag)                        len++;                    flag=0;                    y[i][j]=len;                }                if(s[i][j]=='#')                {                    flag=1;                }            }        }        for(int i=1;i<=n;i++)        for(int j=1;j<=m;j++)        if(s[i][j]=='*')        {            //printf("%d %d %d %d\n",i,j,x[i][j],y[i][j]);            mp[x[i][j]].push_back(y[i][j]);        }        printf("%d\n",hungary());    }    return 0;}


原创粉丝点击