HDU 5093 battle ships (two-part graph + maximum matching)

Source: Internet
Author: User
Battle ships Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/65536 K (Java/Others)
Total submission (s): 172 accepted submission (s): 84


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 your 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
 


Question: give you a matrix of N * M. '*' indicates the open space, 'O' indicates the ice float, '#' indicates the iceberg, and you

A warship can be placed on top of an open space, but only one warship can be placed on one row or column. If two warships want to be placed in the same line or in the same

Columns must be separated by iceberg. Then, how many warships can you put on this graph?

Idea: The maximum matching problem of a two-part graph. the rows and columns are treated as a set, and each row and each column are considered as corresponding.

Point of the set. If the row or column contains '#', split the row or column. Take the example as an example:


* OOO row set (marked by '*') 1OOO column set 1OOO

O ### o ###

** # * 22 #3 23 #4

Ooo * ooo4 ooo4


Then, you can see the position of the source image '*' to create the image.

 

Then, the maximum matching result is the answer.


#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int N = 55*30;const int maxn = 55;int a[maxn][maxn],b[maxn][maxn],match[N],n,m,p,q;bool con[N][N],vis[N];char ch[maxn][maxn];void initial(){    memset(a,0,sizeof(a));    memset(b,0,sizeof(b));    memset(con,0,sizeof(con));    memset(match,-1,sizeof(match));}void input(){    scanf("%d %d",&n,&m);    for(int i=0; i<n; i++)  scanf("%s",ch[i]);}void creat_row(){    p=1;    bool flag;    for(int i=0; i<n; i++)    {        flag=0;        for(int j=0; j<m; j++)        {               if(ch[i][j]=='*')               {                   a[i][j]=p;                   flag=1;               }               if(ch[i][j]=='#')               {                   p++;                   flag=0;               }        }        if(flag)  p++;    }}void creat_column(){    q=1;    bool flag;    for(int j=0; j<m; j++)    {        flag=0;        for(int i=0; i<n; i++)        {               if(ch[i][j]=='*')               {                   b[i][j]=q;                   flag=1;               }               if(ch[i][j]=='#')               {                   q++;                   flag=0;               }        }        if(flag)  q++;    }}bool dfs(int x){    for(int i=1;i<q;i++)    {         if(!vis[i] && con[x][i])         {              vis[i]=1;              if(match[i]==-1 || dfs(match[i]))              {                  match[i]=x;                  return true;              }         }    }    return false;}void solve(){    creat_row();    creat_column();    for(int i=0;i<n;i++)        for(int j=0;j<m;j++)             if(ch[i][j]=='*')                 con[a[i][j]][b[i][j]]=1;    int cnt=0;    for(int i=1;i<p;i++)    {        memset(vis,0,sizeof(vis));        if(dfs(i))  cnt++;    }    printf("%d\n",cnt);}int main(){    int T;    scanf("%d",&T);    while(T--)    {        initial();        input();        solve();    }    return 0;}


HDU 5093 battle ships (two-part graph + maximum matching)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.