Battle ships

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1007    Accepted Submission(s): 353

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
 

题解:题意就是让找在这个矩阵中可以放的船最大数目;其中船之间可以有冰山,船间没冰山时船不能在同行同列;这样需要建图,先扫描行,如果遇到冰山t++;下一个数字可以继续匹配,下一行了t++,同样,再扫描列;得到二分匹配的x分部和y分部;

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
#define mem(x,y) memset(x,y,sizeof(x))
typedef long long LL;
#define SI(x) scanf("%d",&x)
#define SL(x) scanf("%lld",&x)
#define T_T while(T--)
#define F(i,x) for(i=0;i<x;i++)
#define PR(x) printf("%d",x)
#define PL(x) printf("%lld",x)
#define p_ printf(" ")
const int MAXN=1010;
char s[MAXN][MAXN];
int mp[MAXN][MAXN],vis[MAXN],link[MAXN],x[110][110],y[110][110];
int m,n,t1,t2;
int getmpxy(int rl,int a[][110]){//扫描行和列得到二分匹配的x部和y部
int t=0,i,j;
if(rl)F(i,m){
F(j,n){
if(s[i][j]=='*')a[i][j]=t;
if(s[i][j]=='#')t++;
}
t++;
}
else F(j,n){
F(i,m){
if(s[i][j]=='*')a[i][j]=t;
if(s[i][j]=='#')t++;
}
t++;
}
return t;
}
void getmp(){
mem(x,0);mem(y,0);
t1=getmpxy(1,x);
t2=getmpxy(0,y);
int i,j;
F(i,m){
F(j,n){
if(s[i][j]=='*'){
// printf("%d %d\n",x[i][j],y[i][j]);
mp[x[i][j]][y[i][j]]=1;
}
}
}
}
bool dfs(int x){
int i,j;
F(i,t2){
if(!vis[i]&&mp[x][i]){
vis[i]=1;
if(link[i]==-1||dfs(link[i])){//||
link[i]=x;
return true;
}
}
}
return false;
}
int km(){
mem(link,-1);
int i,j;
int ans=0;
F(i,t1){
mem(vis,0);
if(dfs(i))ans++;
}
return ans;
}
int main(){
int T;
SI(T);
T_T{
SI(m);SI(n);
int i,j;
mem(mp,0);
F(i,m)
scanf("%s",s[i]);
getmp();
PR(km());
puts("");
}
return 0;
}

  

Battle ships(二分图,建图,好题)的更多相关文章

  1. HDOJ 5093 Battle ships 二分图匹配

    二分图匹配: 分别按行和列把图展开.hungary二分图匹配. ... 例子: 4 4 *ooo o### **#* ooo* 按行展开. .. . *ooo o#oo oo#o ooo# **#o ...

  2. 最大流任务调度——hdu3572二分图建图

    很简单的任务调度模板题 把一个工作完成一天的量当做是边 /* 任务调度问题最大流 因为两个任务之间是没有关系的,两天之间也是没有关系的 所以抽象成二分图 任务i在天数[si,ei]之间都连一条双向边, ...

  3. poj 3281 Dining 网络流-最大流-建图的题

    题意很简单:JOHN是一个农场主养了一些奶牛,神奇的是这些个奶牛有不同的品味,只喜欢吃某些食物,喝某些饮料,傻傻的John做了很多食物和饮料,但她不知道可以最多喂饱多少牛,(喂饱当然是有吃有喝才会饱) ...

  4. joj 2453 candy 网络流建图的题

    Problem D: Candy As a teacher of a kindergarten, you have many things to do during a day, one of whi ...

  5. POJ 2195 一人一房 最小费用流 建图 水题

    Going Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 21010   Accepted: 10614 Desc ...

  6. 二分图建图,并查集求联通——二维等价性传递 cf1012B好题!

    /* 模拟二分图:每个点作为一条边,连接的是一列和一行(抽象成一个点,列在左,行在右) 由题意得 a-b相连,a-c相连,b-d相连,那么d-c就不用再相连了 等价于把二分图变成联通的需要再加多少边 ...

  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(二分图最大匹配)

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

  9. HDU 1045 Fire Net 二分图建图

    HDU 1045 题意: 在一个n*n地图中,有许多可以挡住子弹的墙,问最多可以放几个炮台,使得炮台不会相互损害.炮台会向四面发射子弹. 思路: 把行列分开做,先处理行,把同一行中相互联通的点缩成一个 ...

随机推荐

  1. Hadoop MultipleOutputs 结果输出到多个文件夹 出现数据不全,部分文件为空

    如题:出现下图中的情况(设置reduceNum=5) 感觉很奇怪,排除了很久,终于发现是一个第二次犯的错误:丢了这句 this.mOutputs.close(); 加上这句,一切恢复正常!

  2. Python之三层菜单

    三层菜单,根据用户所选数字,进入子菜单.一级一级呈现. menu = { 'Beijing': { "ChaoYang": { "CBD": ['CICC', ...

  3. [LeetCode]题解(python):121-Best Time to Buy and Sell Stock

    题目来源: https://leetcode.com/problems/best-time-to-buy-and-sell-stock/ 题意分析: 给定一个数组,代表array[i] 代表第i天的价 ...

  4. PHP-语法(www.w3school.com.cn/php)

    写在前面: 假设系统里已安装PHP软件 PHP是一种脚本语言,执行PHP脚本后向浏览器返回纯HTML语言(即后台将.php文件的执行结果以纯HTML的形式返回到前端) ---------------- ...

  5. FPGA知识大梳理(四)FPGA中的复位系统大汇总

    本文整合特权(吴厚航)和coyoo(王敏志)两位大神的博文.我也很推崇这两位大神的书籍,特权的书籍要偏基础一下,大家不要一听我这么说就想买coyoo的.我还是那一句话,做技术就要step by ste ...

  6. (IOS)关于Xcode的架构(Architectures)设置

    首先来了解一下Architectures中几个参数的含义 ARMv6:ARM11内核用于iPhone2G和iPhone3G中的架构 ARMv7:modern ARM内核用于iPhone3GS和iPho ...

  7. contains 和 ele.compareDocumentPosition确定html节点间的关系

    ~~~ nodeA.contains(nodeB) //ie ,   nodeA.compareDocumentPosition(nodeB) //firefox opera 1.DOMElement ...

  8. activemq下activemq.bat不能启动

    今天下载了一个apache-activemq-5.5.0-bin.rar解压缩后双击/bin目录下的activemq.bat批处理文件发现启动窗口一闪而过无法启动,最后找到原因是因为在环境变量-系统变 ...

  9. [转载]Android 知识图谱

    from: http://blog.csdn.net/xyz_lmn/article/details/41411355

  10. Java程序猿JavaScript学习笔记(2——复制和继承财产)

    计划和完成在这个例子中,音符的以下序列: Java程序猿的JavaScript学习笔记(1--理念) Java程序猿的JavaScript学习笔记(2--属性复制和继承) Java程序猿的JavaSc ...