HDOJ 5093 Battle ships 二分图匹配
二分图匹配:
分别按行和列把图展开。hungary二分图匹配。
。。。
例子:
4 4
*ooo
o###
**#*
ooo*
按行展开。 。。 。
*ooo
o#oo
oo#o
ooo#
**#o
ooo*
ooo*
再按列展开。。。 7 * 8
*ooooooo
oooooooo
oooooooo
oooooooo
*o*ooooo
ooooooo*
ooooooo* 匹配结果3
Battle ships
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 106 Accepted Submission(s): 53
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.
2
4 4
*ooo
o###
**#*
ooo*
4 4
#***
*#**
**#*
ooo#
3
5
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; const int maxn=2600; char mp[55][55];
char hang[maxn][maxn];
char lie[maxn][maxn]; int n,m;
int nn,mm; void getHang()
{
nn=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(mp[i][j]=='o')
{
hang[nn][j]='o';
}
else if(mp[i][j]=='*')
{
hang[nn][j]='*';
}
else if(mp[i][j]=='#')
{
hang[nn][j]='#';
for(int k=j+1;k<m;k++)
{
hang[nn][k]='o';
}
if(j!=m-1)
{
nn++;
for(int k=0;k<=j;k++)
{
hang[nn][k]='o';
}
}
}
}
nn++;
}
/*********************debug***********************
cout<<" debug hang \n";
for(int i=0;i<nn;i++)
{
printf("%s\n",hang[i]);
}
*********************debug***********************/
} void getLie()
{
mm=0;
for(int i=0;i<m;i++)
{
for(int j=0;j<nn;j++)
{
if(hang[j][i]=='*')
{
lie[j][mm]='*';
}
else if(hang[j][i]=='o')
{
lie[j][mm]='o';
}
else if(hang[j][i]=='#')
{
for(int k=j;k<nn;k++)
{
lie[k][mm]='o';
}
if(j!=nn-1)
{
mm++;
for(int k=0;k<=j;k++)
{
lie[k][mm]='o';
}
}
}
}
mm++;
}
/**************debug lie*******************
cout<<"debug lie\n";
cout<<nn<<" * "<<mm<<endl;
for(int i=0;i<nn;i++)
{
for(int j=0;j<mm;j++)
{
printf("%c",lie[i][j]);
if(j==mm-1) putchar(10);
}
}
**************debug lie*******************/
} struct Edge
{
int to,next;
}edge[maxn*maxn]; int Adj[maxn],Size; void init()
{
memset(Adj,-1,sizeof(Adj)); Size=0;
} void add_edge(int u,int v)
{
edge[Size].to=v; edge[Size].next=Adj[u]; Adj[u]=Size++;
} int linker[maxn];
bool used[maxn]; bool dfs(int u)
{
for(int i=Adj[u];~i;i=edge[i].next)
{
int v=edge[i].to;
if(!used[v])
{
used[v]=true;
if(linker[v]==-1||dfs(linker[v]))
{
linker[v]=u;
return true;
}
}
}
return false;
} int hungary()
{
int res=0;
memset(linker,-1,sizeof(linker));
for(int u=0;u<nn;u++)
{
memset(used,false,sizeof(used));
if(dfs(u)) res++;
}
return res;
} int main()
{
int T_T;
scanf("%d",&T_T);
while(T_T--)
{
memset(mp,0,sizeof(mp));
memset(hang,0,sizeof(hang));
memset(lie,0,sizeof(lie));
init(); scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
scanf("%s",mp[i]); getHang();
getLie(); /// build graph for(int i=0;i<nn;i++)
{
for(int j=0;j<mm;j++)
{
if(lie[i][j]=='*')
{
add_edge(i,j);
}
}
} printf("%d\n",hungary());
}
return 0;
}
HDOJ 5093 Battle ships 二分图匹配的更多相关文章
- hdoj 5093 Battle ships 【二分图最大匹配】
题目:pid=5093" target="_blank">hdoj 5093 Battle ships 题意:给你一个n*m的图,图中有冰山 '# ',浮冰 'o' ...
- HDU 5093 Battle ships(二分图最大匹配)
题意:一个m行n列的图由#.*.o三种符号组成,分别代表冰山.海域.浮冰,问最多可放的炮舰数(要求满足以下条件) 1.炮舰只可放在海域处 2.两个炮舰不能放在同一行或同一列(除非中间隔着一个或多个冰山 ...
- Battle ships(二分图,建图,好题)
Battle ships Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Tot ...
- hdu 5093 Battle ships
二分图匹配 #include<cstdio> #include<cstring> #include<iostream> #include<cmath> ...
- hdu 5093 Battle ships (二分图)
二分图最大匹配问题 遇到冰山就把行列拆成两个部分.每个部分x也好,y也好只能匹配一次 图画得比较草,将就着看 横着扫一遍,竖着扫一遍,得到编号 一个位置就对应一个(xi,yi)就是X集到Y集的一条边, ...
- hdu 5093 Battle ships(二分图最大匹配)
题意: M*N的矩阵,每个格子上是三个之一:*.o.#. (1 <= m, n <= 50) *:海洋,战船可以停在上面. o:浮冰,战船 ...
- Hdu5093 Battle ships 二分图
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission( ...
- hdu 5093 Battle ships 匈牙利 很巧妙的建图思路
//这题逼我把匈牙利学了 之前一直很勤快敲网络流 而且不以为耻反以为荣 解:首先按行扫描编号,如果在同一块中(即可以相互攻击),那么将其标为相同的数组,对列也做同样的操作. 然后扫描整张图,如果行编号 ...
- hdu 5093 放置战舰 二分图匹配
http://acm.hdu.edu.cn/showproblem.php?pid=5093 给定一个MxN大小的图,有3种点,冰山.浮冰.海.现在希望能在图中放置尽可能多的船.船的四个方向上不能有其 ...
随机推荐
- SQL 去除小数点后无效 0 的方法
select convert(float,10.0000) 就是这么简单
- JavaScript基础(一)
JavaScript一.什么是JavaScript?脚本描述语言,网页交互特效,说白了,就是实现HTML实现不了的效果.(JavaScript是一种基于对象.事件驱动的简单脚本语言,嵌入在HTML文档 ...
- java.io.FileNotFoundException: class path resource [META-INF/xfire/services.xml] cannot be opened because it does not exist
解决办法: maven创建项目时: META-INF目录下面新建一个xfire文件夹,把services.xml文件放到这个文件夹里,再将整个META-INF拷贝到WEB-INF中 clean一下工程 ...
- Windows Server 2003 SP2 R2 企业版/标准版/32与64位 CD-KEY
微软发布Windows Server 2003 R2版的目的是希望透过它填补Windows Server 2003 SP1和Longhorn Server之间的产品发布时间间隔.所以Windows S ...
- 前端/html5效果收藏
H5应用 9款漂亮的H5效果 8款漂亮的H5效果 36漂亮的button效果 颜色RGB表 省市二级联动
- (二)Angularjs - 入门(2)
AngularJS AJAX AngularJS提供了$http控制,可以用来获取服务器端的数据.服务器通过一个数据库的读取操作来获取需要的数据.注意AngularJS需要JSON格式的数据.一旦数据 ...
- JavaScript 显示弹出窗口(二)
window. open ( sURL , sName , sFeatures , bReplace ) sURL:可选项,被加载页面的html sName:可选项,指定打开的窗口的名字 _media ...
- Windows Server 2012从Evaluation版转成正式版
步骤 运行->CMD(管理员)->输入DISM /online /Get-CurrentEdition 看你的Edition ID是什么,如果是Evaluation的话,例如Standar ...
- jsp之间url传值出现中文乱码
示例: T1.jsp http://localhost:8080/test/Test.action?site=北京 T2.jsp ..... <%@ page language="ja ...
- 关于@font-face的一些问题
@font-face 能够加载服务器端的字体文件,让客户端显示客户端所没有安装的字体.[微软的IE 5已经是开始支持这个属性,但是只支持微软自有的.eot (Embedded Open Type) 格 ...