Battle ships(二分图,建图,好题)
Battle ships
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1007 Accepted Submission(s): 353
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.
4 4
*ooo
o###
**#*
ooo*
4 4
#***
*#**
**#*
ooo#
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(二分图,建图,好题)的更多相关文章
- HDOJ 5093 Battle ships 二分图匹配
二分图匹配: 分别按行和列把图展开.hungary二分图匹配. ... 例子: 4 4 *ooo o### **#* ooo* 按行展开. .. . *ooo o#oo oo#o ooo# **#o ...
- 最大流任务调度——hdu3572二分图建图
很简单的任务调度模板题 把一个工作完成一天的量当做是边 /* 任务调度问题最大流 因为两个任务之间是没有关系的,两天之间也是没有关系的 所以抽象成二分图 任务i在天数[si,ei]之间都连一条双向边, ...
- poj 3281 Dining 网络流-最大流-建图的题
题意很简单:JOHN是一个农场主养了一些奶牛,神奇的是这些个奶牛有不同的品味,只喜欢吃某些食物,喝某些饮料,傻傻的John做了很多食物和饮料,但她不知道可以最多喂饱多少牛,(喂饱当然是有吃有喝才会饱) ...
- joj 2453 candy 网络流建图的题
Problem D: Candy As a teacher of a kindergarten, you have many things to do during a day, one of whi ...
- POJ 2195 一人一房 最小费用流 建图 水题
Going Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 21010 Accepted: 10614 Desc ...
- 二分图建图,并查集求联通——二维等价性传递 cf1012B好题!
/* 模拟二分图:每个点作为一条边,连接的是一列和一行(抽象成一个点,列在左,行在右) 由题意得 a-b相连,a-c相连,b-d相连,那么d-c就不用再相连了 等价于把二分图变成联通的需要再加多少边 ...
- Hdu5093 Battle ships 二分图
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission( ...
- HDU 5093 Battle ships(二分图最大匹配)
题意:一个m行n列的图由#.*.o三种符号组成,分别代表冰山.海域.浮冰,问最多可放的炮舰数(要求满足以下条件) 1.炮舰只可放在海域处 2.两个炮舰不能放在同一行或同一列(除非中间隔着一个或多个冰山 ...
- HDU 1045 Fire Net 二分图建图
HDU 1045 题意: 在一个n*n地图中,有许多可以挡住子弹的墙,问最多可以放几个炮台,使得炮台不会相互损害.炮台会向四面发射子弹. 思路: 把行列分开做,先处理行,把同一行中相互联通的点缩成一个 ...
随机推荐
- Google Maps 学习笔记(二)地图天气预报服务 2014.06.04
地图天气预报服务:一,获取天气预报信息:二,解析天气预报信息:三,在地图上加载天气预报信息: Yahoo!提供的天气预报服务采用流行的RSS输出结果,接口地址如下: http://weather.ya ...
- IOS使用pch预编译文件
首先新建一个pch文件,然后要修改这个项目的Build Setting中的Prefix Header 修改为 $(SRCROOT)/项目名称/预编译文件名: 一般pch文件的用处: 1.导入框架,如: ...
- JavaSE学习总结第27天_反射 & 设计模式 & JDK5、7、8新特性
27.01 反射_类的加载概述和加载时机 类的加载:当程序要使用某个类时,如果该类还未被加载到内存中,则系统会通过加载,连接,初始化三步来实现对这个类进行初始化. 加载:就是指将class文件读 ...
- pthread_wrap.h
#ifndef _PTHREAD_WRAP_H#define _PTHREAD_WRAP_H#include <pthread.h> class hm_pthread_mutex{publ ...
- MVC中关于JSON的处理
jquery中提交数据 $.getJSON("/TopicUpdate/UpdateInformation", { "bookId": bookid } ...
- centos6.5 升级python 到 python 2.7.11 安装 pip
1.首先官方下载源码,然后安装(./configure,make all,make install,make clean,make distclean) 注意:需要先安装zlib-devel,open ...
- 交换右ctrl和capslock
记得几年前开始使用Emacs,因为使用ctrl键太频繁了,所以上网查了下解决方案,看到这篇文章 .把capslock和左ctrl交换,简直要泪流满面啊!立马照着做,从此在所有使用的电脑上都对此进行了设 ...
- Android手机安全软件的恶意程序检测靠谱吗--LBE安全大师、腾讯手机管家、360手机卫士恶意软件检测方法研究
转载请注明出处,谢谢. Android系统开放,各大论坛活跃,应用程序分发渠道广泛,这也就为恶意软件的传播提供了良好的环境.好在手机上安装了安全软件,是否能有效的检测出恶意软件呢?下边针对LBE安全大 ...
- 网络爬虫返回json处理数据
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于JavaScript(Standard ECMA-262 3rd Edition - Decembe ...
- ACM_HDU 1231 最大连续子序列 (dp)_代码分析
Problem Description 给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ Ni, Ni+1, ..., Nj },其中 1 <= i < ...