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地图中,有许多可以挡住子弹的墙,问最多可以放几个炮台,使得炮台不会相互损害.炮台会向四面发射子弹. 思路: 把行列分开做,先处理行,把同一行中相互联通的点缩成一个 ...
随机推荐
- iOS 从app跳转到Safari、从app打开电话呼叫
1.从app跳转到Safari NSString* strIdentifier = @"http://www.ybyb.com"; BOOL isExsit = [[UIAppli ...
- git学习基础教程
分享一个git学习基础教程 http://pan.baidu.com/s/1o6ugkGE 具体在网盘里面的内容..需要的学习可以直接下.
- CSS复习第一天(简单规范)
1.有意义的html 优点:与表现性的页面相比,有意义的页面更容易后期的处理与维护. 便于浏览器与屏幕阅读器识别. 更加便于调试样式 ...
- linux基础命令整理
1 显示当前工作目录 pwd 2 查看目录下的所有内容 ls ls -l 以列方式查看 ls -a 显示所有 ...
- [LeetCode]题解(python):085-Maximal Rectangle
题目来源: https://leetcode.com/problems/maximal-rectangle/ 题意分析: 给定一个二维的二进制矩阵,也就是只包括0 和 1的,找出只包括1的最大的矩阵的 ...
- IOS 警告框 (UIAlertView)的使用方法
1.普通警告框 IOS的SDK中提供了一个方便的类库UIAlertView,配合着不同参数来使用此类可以做出大多数的警告框,如下代码是IOS最简单的警告框. UIAlertView *alert = ...
- 为什么Lisp没有流行起来
很久以前,这种语言站在计算机科学研究的前沿,特别是人工智能的研究方面.现在,它很少被用到,这一切并不是因为古老,类似古老的语言却被广泛应用.其他类似的古老的语言有??FORTRAN. COBOL. L ...
- Lotus Sametime
编辑 Lotus Sametime属于IBM旗下的Lotus软件,包括一个成熟的协作平台提供商. 外文名 Lotus Sametime 属 于 IBM旗下的Lotus软件 包 括 一个成熟 ...
- config.json ajenti
{ "users": { "root": { "configs": { ...
- android TDD平台插入双卡时,查看允许返回发送报告的选项,去掉勾选,不起作用
请在MultiSimPreferenceActivity.java 下修改 修改1: 函数 isChecked() private boolean isChecked(String prefe ...