二分图匹配:

分别按行和列把图展开。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

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
 
Source
 

#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 二分图匹配的更多相关文章

  1. hdoj 5093 Battle ships 【二分图最大匹配】

    题目:pid=5093" target="_blank">hdoj 5093 Battle ships 题意:给你一个n*m的图,图中有冰山 '# ',浮冰 'o' ...

  2. HDU 5093 Battle ships(二分图最大匹配)

    题意:一个m行n列的图由#.*.o三种符号组成,分别代表冰山.海域.浮冰,问最多可放的炮舰数(要求满足以下条件) 1.炮舰只可放在海域处 2.两个炮舰不能放在同一行或同一列(除非中间隔着一个或多个冰山 ...

  3. Battle ships(二分图,建图,好题)

    Battle ships Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tot ...

  4. hdu 5093 Battle ships

    二分图匹配 #include<cstdio> #include<cstring> #include<iostream> #include<cmath> ...

  5. hdu 5093 Battle ships (二分图)

    二分图最大匹配问题 遇到冰山就把行列拆成两个部分.每个部分x也好,y也好只能匹配一次 图画得比较草,将就着看 横着扫一遍,竖着扫一遍,得到编号 一个位置就对应一个(xi,yi)就是X集到Y集的一条边, ...

  6. hdu 5093 Battle ships(二分图最大匹配)

    题意: M*N的矩阵,每个格子上是三个之一:*.o.#.                     (1 <= m, n <= 50) *:海洋,战船可以停在上面.      o:浮冰,战船 ...

  7. Hdu5093 Battle ships 二分图

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission( ...

  8. hdu 5093 Battle ships 匈牙利 很巧妙的建图思路

    //这题逼我把匈牙利学了 之前一直很勤快敲网络流 而且不以为耻反以为荣 解:首先按行扫描编号,如果在同一块中(即可以相互攻击),那么将其标为相同的数组,对列也做同样的操作. 然后扫描整张图,如果行编号 ...

  9. hdu 5093 放置战舰 二分图匹配

    http://acm.hdu.edu.cn/showproblem.php?pid=5093 给定一个MxN大小的图,有3种点,冰山.浮冰.海.现在希望能在图中放置尽可能多的船.船的四个方向上不能有其 ...

随机推荐

  1. php的mq客户端获取队列方法改造

    获取mq中消息然后处理失败重试机制: 下面的代码是php连接mq客户端的获取queue队列中的消息代码: public function createDurableSubscriber($queue, ...

  2. jquery 验证框架的问题 remote的

    1.dataType 类型:String 预期服务器返回的数据类型.如果不指定,jQuery 将自动根据 HTTP 包 MIME 信息来智能判断,比如 XML MIME 类型就被识别为 XML.在 1 ...

  3. IIS 7.5 配置伪静态

    IIS 7.5 配置伪静态_win服务器_脚本之家 win7下IIS的安装和配置 图文教程详细出处参考:http://www.jb51.net/article/29787.htm http://blo ...

  4. Memcached 深度分析

    Memcached是danga.com(运营LiveJournal的技术团队)开发的一套分布式内存对象缓存系统,用于在动态系统中减少数据库 负载,提升性能.关于这个东西,相信很多人都用过,本文意在通过 ...

  5. jquery的几种异步请求,ajax

    http://blog.csdn.net/a5489888/article/details/8523316

  6. mvc wcf 并发提示,存储Application,验证是否有用户在操作

    System.Web.HttpContext httpContext = System.Web.HttpContext.Current; Hashtable departmentSalary = (H ...

  7. iOS 网络与多线程--4.同步Post方式的网络请求

    通过Post请求方式,同步获取网络数据,一旦发送同步请求,程序将停止用户交互,直至服务器返回数据 在ViewController.m文件内的viewDidLoad函数添加一下测试代码 - (void) ...

  8. PHP Date/Time 函数

    Runtime 配置 Date/Time 函数的行为受到 php.ini 中设置的影响: 名称 描述 默认 PHP 版本 date.timezone 规定默认时区(所有的 Date/Time 函数使用 ...

  9. QT 5.1.1 for Android 开发环境搭建与配置【Windows 7】

    前言:本人操作系统为Windows7 64位,用的是32位的安装包,32位系统没有验证. 一.首先下载以下安装包,如果提供的链接失效请自行下载: (1) Android SDK (Windows 32 ...

  10. telnet简单操作 模拟请求

    telnet简单操作   模拟请求 一: 二: 三: 按照以上操作即可!