Battle ships
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 iceA battleship cannot be placed on an icebergTwo 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 Input24 4*oooo###**#*ooo*4 4#****#****#*ooo#Sample Output35题目大意:
给出一个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 #include9 #include 10 #include 11 #include 12 #include 13 #include 14 #include
15 #include 16 #include 17 #include 18 #include 19 #include 20 #include 21 #define MAXN 10000022 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 >T;57 while (T--)58 {59 int N,M;60 cin>>M>>N;61 //cout< < >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-178 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< <