Battle ships(HDU-5093)
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
————————————————————————————————————————————
题意:有一个 n*m 的图,* 代表空白海域,# 代表冰山,o 代表浮冰,在海域上布置战船,要求战船不能放到浮冰、冰山上,每一行每一列只能布置一艘船,除非中间有冰山隔开,问最多能布置多少艘
思路:同一行列只能放置一艘船,很容易想到二分图的最大匹配,但由于冰山的存在使得每一行列有可能放置两艘船,因此可以将行列作为两个集合,每一行或每一列分别对应集合的一个点,遇到冰山时,将行列进行拆点建图,最后匈牙利算法跑一遍即可。
Source Program
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define PI acos(-1.0)
#define E 1e-6
#define MOD 1000003
#define INF 0x3f3f3f3f
#define N 1001
#define LL long long
using namespace std;
int n,m;
int G[N][N];
char str[N][N];
int x[N][N],y[N][N];
int link[N];
bool vis[N];
int cntX,cntY;
bool dfs(int u){
for(int i=1;i<cntY;i++){
if(G[u][i]&&!vis[i]){
vis[i]=true;
if(link[i]==-1 || dfs(link[i])){
link[i]=u;
return true;
}
}
}
return false;
}
int hungary(){
int res=0;
memset(link,-1,sizeof(link));
for(int i=1;i<cntX;i++){
memset(vis,0,sizeof(vis));
if(dfs(i))
res++;
}
return res;
}
int main (){
int t;
scanf("%d",&t);
while(t--){
memset(G,0,sizeof(G));
memset(x,0,sizeof(x));
memset(y,0,sizeof(y));
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
scanf("%s",str[i]);
//处理行
cntX=1;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(str[i][j]=='*')
x[i][j]=cntX;
if(str[i][j]=='#')
cntX++;
}
cntX++;
}
//处理列
cntY=1;
for(int j=0;j<m;j++){
for(int i=0;i<n;i++){
if(str[i][j]=='*')
y[i][j]=cntY;
if(str[i][j]=='#')
cntY++;
}
cntY++;
}
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
if(str[i][j]=='*')
G[x[i][j]][y[i][j]]=1;
printf("%d\n",hungary());
}
return 0;
}
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- JVM系列之:JIT中的Virtual Call接口
- 重新构建711的Android项目(二),架构的选择与实现
- Android的配置文件操作封装,摒弃SharedPreference操作配置漫天乱飞
- java实现PBOC的TLV格式解析,超简单的解析(全互联网最简单)
- 链表总计
- Spring整合MongoDb
- 正确使用Qt多线程
- Spring与hibernate与mybatis
- scala 模式匹配的几个模式
- Spring Security 认证
- Spring Security 简单配置用户存储
- 我的小工具-nodejs串口转TCP调试通信
- Spring Web Folw配置
- Spring Aop支持
- 贪心思想