HDU5093——Battle ships(最大二分匹配)(2014上海邀请赛重现)

Battle ships


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
2
4 4
*ooo
o###
**#*
ooo*
4 4
#***
*#**
**#*
ooo#
Sample Output
3
5

题目大意:

    给出一个n行m列的图,*代表海域,o代表冰水,#代表冰山,要想在海域中放置船,保证船与船之间不能相互看到,之间只要有山就不能看到,问最多能放多少船。

解题思路:

    比赛的时候没有想出来,看了别人的博客也是花了很长时间才想明白为什么是二分匹配(在这里谢谢这些大牛,感谢你们的blog,要不我现在还不会呢。。。)。

    姑且将一片最多只能放一个船的连续网格叫做‘块’。

    以样例一为例

    首先只考虑行,将每个块标号:将答案存入num1

    1000

    0000

    2203

    0004

    再此只考虑列,将每个块标号:将答案存入num2

    1000

    0000

    1203

    0003

    那么对于任意一个可以放船的二维坐标A(i,j),假设其放船,那么num1与num2中与A(i,j)对应num相同的坐标都不能再放船。相当于A所在的行块和列块实现了一一映射关系。

    所以将行块看做一个集合,列块看做一个集合。

    所求的最大放船数就是两个集合之间的最大匹配数。

    匈牙利模版解决问题。

Code:

 1 /*************************************************************************
 2     > File Name: shanghai_1004.cpp
 3     > Author: Enumz
 4     > Mail: 369372123@qq.com
 5     > Created Time: 2014年11月04日 星期二 01时28分18秒
 6  ************************************************************************/
 7 
 8 #include<iostream>
 9 #include<cstdio>
10 #include<cstdlib>
11 #include<string>
12 #include<cstring>
13 #include<list>
14 #include<queue>
15 #include<stack>
16 #include<set>
17 #include<algorithm>
18 #include<cmath>
19 #include<bitset>
20 #include<climits>
21 #define MAXN 100000
22 using namespace std;
23 bool edge[3000][3000];
24 char map[100][100];
25 int num1[100][100],num2[100][100],link[3000];
26 bool vis[3000];
27 int k1,k2,cnt;
28 bool dfs(int x)
29 {
30     for (int y=1; y<k2; y++)
31         if (edge[x][y]&&!vis[y])
32         {
33             vis[y]=1;
34             if (link[y]==0||dfs(link[y]))
35             {
36                 link[y]=x;
37                 return true;
38             }
39         }
40     return false;
41 }
42 void search()
43 {
44     //cout<<"1"<<endl;
45     memset(link,0,sizeof(link));
46     for (int x=1; x<k1; x++)
47     {
48         memset(vis,0,sizeof(vis));
49         if (dfs(x))
50             cnt++;
51     }
52 }
53 int main()
54 {
55     int T;
56     cin>>T;
57     while (T--)
58     {
59         int N,M;
60         cin>>M>>N;
61         //cout<<M<<N;
62         memset(edge,0,sizeof(edge));
63         memset(num1,0,sizeof(num1));
64         memset(num2,0,sizeof(num2));
65         for (int i=1; i<=M; i++)
66             for (int j=1; j<=N; j++)
67                 cin>>map[i][j];
68         k1=k2=1;
69         for (int i=1; i<=M; i++)
70         {
71             for (int j=1; j<=N; j++)
72             {
73                 if (map[i][j]=='#') k1++;
74                 if (map[i][j]=='*') num1[i][j]=k1;
75             }
76             k1++; //注意k累加的位置,放在for循环之后保证最后的块
77         }        //的编号为k-1
78         for (int i=1; i<=N; i++)
79         {
80             for (int j=1; j<=M; j++)
81             {
82                 if (map[j][i]=='#') k2++;
83                 if (map[j][i]=='*') num2[j][i]=k2;
84             }
85             k2++;
86         }
87         for (int i=1; i<=M; i++)
88             for (int j=1; j<=N; j++)
89                 edge[num1[i][j]][num2[i][j]]=1;
90         cnt=0;
91         search();
92         cout<<cnt<<endl;
93     }
94     return 0;
95 }
原文地址:https://www.cnblogs.com/Enumz/p/4072647.html