hdu 5093 Battle ships 最大二分匹配

來源:互聯網
上載者:User

標籤:二分匹配

Battle shipsTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 589    Accepted Submission(s): 233


Problem DescriptionDear 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.
 
InputThere 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. 
OutputFor 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


連結:http://acm.hdu.edu.cn/showproblem.php?pid=5093


題意:選儘可能多*號點,要求選得點之間必須有#阻隔,否者不能在同一行或者同一列。


做法:

先要把豎向*和O相連的 都連成一起,並編號。

然後是橫著的也一樣。

案例編號後:

2
4 4
*ooo
o###
**#*
ooo*

這裡是豎向編號,同編號為一塊
1 2 4 6
1 0 0 0
1 3 0 7
1 3 5 7

這裡是橫向編號
1 1 1 1
2 0 0 0
3 3 0 4
5 5 5 5
3
4 4
#***
*#**
**#*
ooo#
1 2 4 6
1 0 4 6
1 3 0 6
1 3 5 7
1 1 1 1
2 0 3 3
4 4 0 5
6 6 6 5
5


然後把豎向的每一塊 都看作二分圖左邊的每一個點,

然後把橫向的每一塊 都看作二分圖右邊的每一個點,


星號必會對應一個豎向的塊 編號,   和一個橫向的塊編號。

把每個星號對應的 兩個編號連線。

如果匹配了,就表示這兩個塊都被佔據了,這兩個塊就不能再有其他星號點被連了。


所以要知道最多有多少*可以放,就只用知道最多可以有幾條邊可以選。也就是求最大二分匹配了。


#include <stdio.h>#include <stdlib.h>#include <string.h>#include <limits.h>#include <malloc.h>#include <ctype.h>#include <math.h>#include <string>#include <iostream>#include <algorithm>using namespace std;#include <stack>#include <queue>#include <vector>#include <deque>#include <set>#include <map> #define N 3000int visit[N];int mark[N];int match[N][N];int n,m,k;int dfs(int x){    int i;//printf("%d\n",x);    for(i=1;i<=m;i++)//對左邊的節點x與右邊的節點進行逐一檢查    {        if(!visit[i]&&match[x][i])        {            visit[i]=1;//標記檢查過的點            if(mark[i]==-1||dfs(mark[i])) //mark如果沒男的,就直接給個老公,如果已經有老公,搜他老公有沒有喜歡的,有的話,該女配新男;;;             {//|| 前面過了 後面不運行;;                 mark[i]=x;//修改匹配關係                   return 1;            }        }    }    return 0;}int hungary (){    memset(mark,-1,sizeof(mark));int maxx=0,j;    for(j=1;j<=n;j++)//對做部分頂點逐個進行遍曆{memset(visit,0,sizeof(visit));if(dfs(j))maxx++;}return maxx;}char mp[60][60];int cdh[60][60];int cds[60][60];int main()//注意 mark[m]=n  mark[j] = i{    int maxx; int nn,mm;int t;scanf("%d",&t);    while(t--)   //k 個配    {scanf("%d%d",&nn,&mm);/*for(int i=0;i<nn;i++){for(int j=0;j<mm;j++){if((i+j)%2==1)mp[i][j]='#';elsemp[i][j]='*';}}*/for(int i=0;i<nn;i++){scanf("%s",mp[i]); }int sqi=0;for(int j=0;j<mm;j++)//左豎著{for(int i=0;i<nn;i++){if(mp[i][j]!='#'){if(i==0)cds[i][j]=++sqi;else if(mp[i-1][j]=='#')cds[i][j]=++sqi;elsecds[i][j]=sqi;} }}n=sqi;int hqi=0; for(int i=0;i<nn;i++) //you heng 著{for(int j=0;j<mm;j++){if(mp[i][j]!='#'){if(j==0)cdh[i][j]=++hqi;else if(mp[i][j-1]=='#')cdh[i][j]=++hqi;elsecdh[i][j]=hqi; } }}m=hqi;/*for(int i=0;i<nn;i++)//左豎著{for(int j=0;j<mm;j++){printf("%d ",cdh[i][j]);}puts("");}*/        memset(match,0,sizeof(match));for(int i=0;i<nn;i++){for(int j=0;j<mm;j++){if(mp[i][j]=='*'){int z=cds[i][j];int y=cdh[i][j]; match[z][y]=1;}}}maxx=hungary();        printf ("%d\n",maxx);/*scanf ("%d%d%d",&n,&m,&k)!=EOF        for(i=1;i<=k;i++)        {            int a,b;            scanf ("%d%d",&a,&b);            match[a][b]=1;//標記當前匹配關係        }max=hungary();        printf ("%d\n",max);*/    }    return 0;} 







hdu 5093 Battle ships 最大二分匹配

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.