hdu5093

来源:互联网 发布:linux -g 编辑:程序博客网 时间:2024/06/05 03:44

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

Sample Output
35
 

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

给你一张 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;}/**24 4*oooo###**#*ooo*行区域编号1oooo###66#7ooo8列区域编号1oooo###13#8ooo8*/




0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 孕29周胎儿臀位怎么办 刚怀孕有囊肿该怎么办 食杏中毒怎么办吃什么 猫吃了扁桃仁怎么办 夏天来了??点狐臭怎么办? 我有一小点狐臭怎么办 淘客网站被微信屏蔽怎么办 微信老是屏蔽网站怎么办 华帝售后没人理怎么办 新开的网店没生意怎么办 淘小二介入买家举证不全怎么办 新股申购中签后钱不够怎么办 买的东西收不到怎么办? 拼多多按错收货怎么办 团购招生做到一半不如意怎么办 网购付款后卖家没有货怎么办 淘宝退货商家不同意退款怎么办 淘宝确认收货后卖家拒绝退款怎么办 淘宝卖家不同意退款怎么办 淘宝仅退款卖家不处理怎么办 淘宝申请退款卖家不处理怎么办 快递没收到货要退款怎么办 发票给了不给钱怎么办 付款后不给发票怎么办 供货商不给发票怎么办 刚生过孩子太胖买衣服怎么办 黑色牛仔裤洗的发白怎么办 蘑菇街手机丢了怎么办 黑衣服上全是白毛毛怎么办 支付宝注销了钱怎么办 网上买东西手机号写错了怎么办 二类工资卡过万怎么办 淘宝发货地址写错怎么办 淘宝发货地址写错了怎么办 淘宝不能代付了怎么办 支付宝付款码被盗刷怎么办 地方选举追究不到相关责任人怎么办 天正建筑画个直线找不到怎么办 Wi-Fi模块不支持多播怎么办? 魅族手机屏幕点不动怎么办 uc打开网页很慢怎么办