HDU 5093 Battle ships [二分图匹配] [匈牙利算法]
来源:互联网 发布:菲拉格慕蓝色经典 知乎 编辑:程序博客网 时间:2024/06/05 01:02
Battle ships
Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u
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
24 4*oooo###**#*ooo*4 4#****#****#*ooo#
Sample Output
35
典型的二维矩阵转化为二分图匹配的问题。。
要放置尽量多的ships,但是同一行同一列除非有iceberg隔开只能放一只,并且ships只能放在普通海域上。
那么预处理的时候找出相连的“块”然后建立行和列的二分图,然后求最大匹配数即可。。
编译一次成功且1A。。。。
#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<vector>#include<queue>#include<stack>#include<map>#include<set>#include<string>#include<iomanip>#include<ctime>#include<climits>#include<cctype>#include<algorithm>#ifdef WIN32#define AUTO "%I64d"#else#define AUTO "%lld"#endifusing namespace std;#define smax(x,tmp) x=max((x),(tmp))#define smin(x,tmp) x=min((x),(tmp))#define maxx(x1,x2,x3) max(max(x1,x2),x3)#define minn(x1,x2,x3) min(min(x1,x2),x3)const int INF=0x3f3f3f3f;const int maxn = 52;struct Edge{ int to,next;}edge[maxn*maxn<<1];int head[maxn*maxn];int maxedge;inline void addedge(int u,int v){ edge[++maxedge] = (Edge) { v,head[u] }; head[u] = maxedge;}int n,m;int g[maxn][maxn],cpy[maxn][maxn];int row,col;int r[maxn][maxn],c[maxn][maxn];void init(){ scanf("%d%d",&m,&n); memset(head,-1,sizeof(head)); maxedge = -1; char ch; for(int i=1;i<=m;i++) for(int j=1;j<=n;j++) { ch = getchar(); while(ch^'#' && ch^'o' && ch^'*') ch = getchar(); if(ch=='#') cpy[i][j]=1; // iceberg if(ch=='*') cpy[i][j]=0; // sea if(ch=='o') cpy[i][j]=-1; // floating ice } memcpy(g,cpy,sizeof(cpy)); memset(r,0,sizeof(r)); memset(c,0,sizeof(c)); row=col=0; for(int i=1;i<=m;i++) for(int j=1;j<=n;j++) if(!g[i][j]) { if(j==1 || g[i][j-1]==1) row++; r[i][j]=row; } else if(j==1 || g[i][j-1]==1) g[i][j]=1; memcpy(g,cpy,sizeof(cpy)); for(int j=1;j<=n;j++) for(int i=1;i<=m;i++) if(!g[i][j]) { if(i==1 || g[i-1][j]==1) col++; c[i][j]=col; } else if(i==1 || g[i-1][j]==1) g[i][j]=1; for(int i=1;i<=m;i++) for(int j=1;j<=n;j++) if(!cpy[i][j]) addedge(r[i][j],c[i][j]);}int match[maxn*maxn];bool vis[maxn*maxn];bool dfs(int u){ for(int i=head[u];~i;i=edge[i].next) { int v = edge[i].to; if(vis[v]) continue; vis[v]=true; if(!match[v] || dfs(match[v])) { match[v] = u; return true; } } return false;}int hungary(){ int ret=0; memset(match,0,sizeof(match)); for(int i=1;i<=row;i++) { memset(vis,0,sizeof(vis)); if(dfs(i)) ret++; } return ret;}int main(){ int cas; scanf("%d",&cas); while(cas--) { init(); int ans = hungary(); printf("%d\n",ans); } return 0;}
- HDU 5093 Battle ships [二分图匹配] [匈牙利算法]
- hdu 5093 Battle ships 二分图匹配
- hdu 5093 Battle ships(二分图匹配)
- HDU 5093 Battle ships [二分图匹配]
- hdu 5093 Battle ships 二分匹配
- hdu 5093 Battle ships(二分匹配)
- hdu 5093 Battle ships 最大二分匹配
- HDU 5093 Battle ships(二分图最大匹配)
- HDU 5093 Battle ships(二分图匹配)
- HDU - 5093 Battle ships 初学 二分图匹配
- hdu 5093 Battle ships(二分图匹配)
- HDU 5093 Battle ships(二分图匹配)
- HDU 5093 Battle ships(二分图匹配)
- hdu 5093 Battle ships(二分图匹配)
- Hdu 5093 Battle ships【思维+二分图匹配】
- HDOJ 5093 Battle ships 二分图匹配
- 【二分匹配】 HDU 5093 Battle ships 经典二分题
- HDU 5093 Battle ships 二分图!
- Ajax网页实现异步更新----理解
- mockito简单教程
- 经典问题总结
- HDU5823(2016多校第八场)——color II (状压dp,独立集)
- 线段树 扫描线
- HDU 5093 Battle ships [二分图匹配] [匈牙利算法]
- hdu 2013
- 高德地图AMap_2DMap_V2.9.0_20160525版下,隐藏“高德地图”logo样式
- 判断一个单链表是否有环及环的链接点
- 定位 CLLocationManager
- Cannot construct Factory : com.opensymphony.module.sitemesh.factory.DefaultF
- CSS盒子模型-盒子模型应用
- Opencv中的3种线性滤波器
- POJ 2377 Bad Cowtractors【最大生成树,Prime算法】