hdu5093
来源:互联网 发布:linux -g 编辑:程序博客网 时间:2024/06/05 03:44
Battle ships
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1120 Accepted Submission(s): 408
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.
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.
24 4*oooo###**#*ooo*4 4#****#****#*ooo#
35
给你一张 n*m 的矩阵图形 ,‘ * ’ 表示 空地 , ‘ o ’ 表示浮冰 , ‘ # ’ 表示 冰山 , 你可以在空地上方战舰 ,不可以在有浮冰的地方放战舰, 但是同一行或同一列只能放一个战舰。若两个战舰想放在同一行或同一列上,那么它们之间必须要有冰山相隔。然后问你在这张图上最多可以放多少个战舰 ?
如果是没有冰山,那就是简单的二分图匹配了,行与列直接相连,但是加了冰山,一行可以放多个,一列也可以放多个,用二分图换匹配还能不能解呢?能!
划分区域,将每一行每一列划分区域,每一行相连的空地划分为同一个区域并编号,每一列划相连区域分为同于区域并编号,50*50的矩阵,区域不会超过1250个,我随便开了一两个1500*1500的 区域矩阵,一个表示行区域,一个表示列局区域;
就以样例 一为例:
* o o o 行集合(将 ’ * ‘标号) 1 o o o 列集合 1 o o o
o # # # o # # # o # # #
* * # * 2 2 # 3 1 2 # 3
o o o * o o o 4 o o o 3
由于这样代码实现有一点复杂,其实可以遇见#就给区域号加一,反正同一区域的编号是相同即可,详见代码建边时 ,将空地 * 的行区域号与列区域号连边即可;
ac代码:
#include <iostream>#include <stdio.h>#include <string.h>using namespace std;const int maxn=1500;int uN;int ma[maxn][maxn];int vis[maxn],linker[maxn];///二分图匹配bool dfs(int u){ for(int v=1;v<=uN;v++) { if(!vis[v]&&ma[u][v]) { vis[v]=1; if(linker[v]==-1||dfs(linker[v])) { linker[v]=u; return true; } } } return false;}int solve(){ int ans=0; memset(linker,-1,sizeof(linker)); for(int i=1;i<=uN;i++) { memset(vis,0,sizeof(vis)); if(dfs(i)) ans++; } return ans;}int n,m;int c[55][55],l[55][55];char str[55][55];void creat_ma()///建边{ int tot=1; uN=0; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(str[i][j]=='*') c[i][j]=tot; if(str[i][j]=='#') tot++; } tot++; } if(tot>uN) uN=tot; tot=1; for(int i=0;i<m;i++) { for(int j=0;j<n;j++) { if(str[j][i]=='*') l[j][i]=tot; if(str[j][i]=='#') tot++; } tot++; } if(uN<tot) uN=tot; memset(ma,0,sizeof(ma)); for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(str[i][j]=='*') ma[c[i][j]][l[i][j]]=1; } } for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(str[i][j]=='*') cout<<c[i][j]; else cout<<str[i][j]; } cout<<endl; } for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(str[i][j]=='*') cout<<l[i][j]; else cout<<str[i][j]; } cout<<endl; }}int main(){ int t; cin>>t; while(t--) { cin>>n>>m; for(int i=0;i<n;i++) cin>>str[i]; creat_ma(); cout<<solve()<<endl; } return 0;}/**24 4*oooo###**#*ooo*行区域编号1oooo###66#7ooo8列区域编号1oooo###13#8ooo8*/
- HDU5093
- hdu5093
- hdu5093
- 最大二分匹配hdu5093
- hdu5093(二分图)
- HDU5093 Battle ships (BZOJ4554)
- 二分多重匹配(HDU5093)
- HDU5093 Battle ships (二分图)
- hdu5093(二分图最大匹配)
- hdu5093 二分图最大匹配、
- 【匈牙利】HDU5093[Battle ships]题解
- hdu5093 二分图的最大匹配
- hdu5093 Battle ships 二分图匹配
- hdu5093 Battle ships 二分图匹配
- 二 工厂方法模式
- 编译原理:求First集与Follow集的方法
- qt项目发布
- LeetCode 57. Insert Interval/56. Merge Intervals
- SSH商城第一天的学习感受
- hdu5093
- nyoj 16 矩形嵌套 (DAG上的动态规划)
- 深入理解Java:注解(Annotation)自定义注解入门
- java.lang.SecurityException: Permission Denial
- 猴子选大王
- LoaderManager
- C++第四次上机作业
- 数组的剖析
- codeforces 462 d Appleman and Tree(树形dp)