HDU 5093 Battle ships maximum binary matching

Source: Internet
Author: 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 a excellent Navy commander, who is responsible of a tough mission Ly.

Your Fleet Unfortunately encountered an enemy fleet near the South Pole where the geographical conditions is negative for Both sides. The floating ice and iceberg blocks battleships move which leads to this unexpected engagement highly dangerous, unpredict Able and incontrollable.

But, fortunately, as a experienced navy commander, you is able to take opportunity-embattle the ships to maximize the Utility of cannons on the battleships before the engagement.

The target is a arrange as many battleships as you can in the map. However, there is three rules so which you cannot does that arbitrary:

A battleship cannot lay on floating ice
A battleship cannot be placed in an iceberg

The battleships cannot is arranged in the same row or column, and unless one or more icebergs is in the middle of them.

Inputthere is only one of the integer T (0<t<12) at the beginning line, which means following T test cases.

For each test case, integers m and n (1 <= m, n <=) is 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 the ' # ', ' * ', ' o ', that symbolize iceber G, ordinary sea and floating ice.
Outputfor each case, output just one line, contains a single integer which represents the maximal possible number of Battl Eships can be arranged.
Sample Input
4*oooo###**#*ooo*4 4#****#****#*ooo#

Sample Output
35


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


Test instructions: Select as many * points as possible, require that there must be a # barrier between the selected points, whether the person cannot be in the same row or the same column.


Practice:

First, the vertical * and O are connected together, and numbered.

And then there's the same thing sideways.

After the case number:

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

Here is the vertical number, the same number as a piece
1 2 4 6
1 0 0 0
1 3 0 7
1 3 5 7

Here is the landscape number
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


Each of the vertical blocks is then considered to be each point on the left side of the binary graph,

Then each of the horizontal pieces is considered to be the right of each point of the dichotomy,


The asterisk must correspond to a vertical block number, and a horizontal block number.

Connect the two numbers that correspond to each asterisk.

If a match is made, the two blocks are occupied, and the two blocks can no longer be connected by any other asterisk points.


So you need to know how many * can be put, only to know that up to a few sides can be selected. That is, to find the maximum two-point match.


#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++)//Check the left node X with the node on the right {if (!visit[i]&&match[x][i]) {visit[i]=1;// Mark checked points if (mark[i]==-1| | DFS (Mark[i])//mark If there is no male, directly to a husband, if there is a husband, search his husband there is no like, there are words, the woman with a new male;; {//| | The front is not running behind; mark[i]=x            ;//Modify the matching relationship return 1; }}} return 0;}    int Hungary () {memset (mark,-1,sizeof (Mark)); int maxx=0,j; for (j=1;j<=n;j++)//To do a partial vertex by traversing {memset (visit,0,sizeof (visit));S (j)) maxx++;} Return Maxx;} Char mp[60][60];int cdh[60][60];int cds[60][60];int Main ()//Note mark[m]=n mark[j] = i{int maxx; int Nn,mm;int t;scanf (    "%d", &t); while (t--)//k with {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++)//left vertical {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 with {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++)//left vertical {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;//mark the current matching relationship}max=hungary ();  printf ("%d\n", max); */} return 0;}







HDU 5093 Battle ships maximum binary 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.