题干:

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代表冰水,#代表冰山,要想在海域中放置船,保证船与船之间不能相互看到,之间只要有山就不能看到,问最多能放多少船。

(也就是冰水和冰山都不能放船,但是冰山就能隔开两个船,冰水就不行)
将一片最多只能放一个船的连续网格叫做‘块’。
以样例一为例
首先只考虑行,将每个块标号:将答案存入x [ ] [ ]
 1000
 0000
 2203
 0004
再此只考虑列,将每个块标号:将答案存入y [ ][ ]
 1000
 0000
 2304
 0004

解题报告:

建图,注意那个line数组那些,不要开数组开小了,因为totx和toty都可能比较大。

其实是这个题的进化版本:【BZOJ - 1059】矩阵游戏(二分图匹配,建图,最小边覆盖)。

不同点在于,他那个题不需要缩点,就相当于你这个题中每一行和每一列就是自己一个连通块,中间没有东西把他们分割开来,也就是说如果没有 ' # ' ,那么就退化成bzoj这个题了。

这题也是这样,二分图中的边代表原图中的每一个点(这里的点指的是连通块的某一个交点),当选择了某一条边,也就说明连接的左右两个点已经被占用了(代表对应的行连通块和列连通块被使用了)

思考方式和这个题也很像【HDU - 1281 】

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
char maze[555][555];
int visx[555][555],visy[555][555];
int n,m;
int totx,toty;
bool line[2500][2500];
int nxt[2505];
bool used[2555];
bool find(int x) {for(int i = 1; i<=toty; i++) {if(line[x][i] && used[i]==0) {used[i]=1;if(nxt[i] == -1 || find(nxt[i])) {nxt[i] = x;return 1;}}}return 0;
}
int match() {int sum = 0;memset(nxt,-1,sizeof nxt);for(int i = 1; i<=totx; i++) {memset(used,0,sizeof used);if(find(i)) sum++;}return sum;
}
int main()
{int t;cin>>t;while(t--) {scanf("%d%d",&n,&m);memset(visx,0,sizeof visx);memset(visy,0,sizeof visy);memset(line,0,sizeof line);totx=toty=0;for(int i = 1; i<=n; i++) scanf("%s",maze[i]+1);for(int i = 1; i<=n; i++) {for(int j = 1; j<=m; j++) {if(maze[i][j] == '*' || maze[i][j] == 'o') {if(maze[i][j-1]=='#' || j == 1) visx[i][j] = ++totx;else visx[i][j] = visx[i][j-1];if(maze[i-1][j]=='#' || i == 1)  visy[i][j] = ++toty;else visy[i][j] = visy[i-1][j];}}}for(int i = 1; i<=n; i++) {for(int j = 1; j<=m; j++) {if(maze[i][j] == '*') {line[visx[i][j]][visy[i][j]] = 1;}}}printf("%d\n",match());} return 0 ;
}

【 HDU - 5093】Battle ships(匈牙利算法,二分图匹配)相关推荐

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

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

  2. 【HDU - 5943】Kingdom of Obsession(数论,素数间隔结论,构造,思维,匈牙利算法,匹配问题)

    题干: There is a kindom of obsession, so people in this kingdom do things very strictly. They name the ...

  3. HDU 2444 The Accomodation of Students 二分图匹配

    HDU 2444 The Accomodation of Students 二分图匹配 题目来源: HDU 题意: 给出学生数n和关系数m,接下来给出m个关系. 要求将学生分成两部分,每一部分不能有互 ...

  4. HDU 2063 过山车 (二分图匹配之匈牙利算法)

    过山车 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  5. 【模板】匈牙利算法 二分图最大匹配题模板

    [任务] 给定一个二分图,用匈牙利算法求这个二分图的最大匹配数. [说明] 求最大匹配,那么我们希望每一个在左边的点都尽量找到右边的一个点和它匹配. 我们一次枚举左边的点x的所有出边指向的点y, 若y ...

  6. NOI图论算法:二分图匹配

    二分图匹配 算法竞赛入门经典训练指南+陈锋+ch5.5_二分图的匹配 https://www.bilibili.com/video/BV1j5411x7PU SWPU-ACM每周算法讲堂-匈牙利算法 ...

  7. 浅谈匈牙利算法(二分图最大匹配)

    前置知识 一张图是二分图,当且仅当它的点可以被分成两部分,而这张图上的所有边的两个端点,都分属不同的部分.我们称这两个点集,一个叫左部,一个叫右部.左部中的点叫左部点:右部中的点叫右部点. 一张图的一 ...

  8. 【HDU 1150】Machine Schedule(二分图匹配)

    机器的不同模式为点,对于每个job,建两条边 A机器需要的模式<->B机器需要的模式. 问题转化为最小点覆盖,然后用二分图的最小点覆盖==最大匹配,用匈牙利算法解. #include &l ...

  9. POJ-3041 匈牙利算法 二分图最大匹配

    踢以 给出多个点的坐标 有一种攻击 可以把一次干掉同一列的 或者干掉同一行的 求最少的攻击次数 肥西 由于问题是问选取最少的行和列干掉所有的陨石 可以把输入的r和c看成r和c之间有一条连边因为要实现干 ...

  10. 【Step1】【二分图匹配】poj 1274-The Perfect Stall

    题目链接 题目大意 输入数据中,第一行给出n,表示n个奶牛. 接下来n行,每行一个x,xi表示第i头奶牛可以选择x个谷仓中的一个进行匹配.接下来x个数,表示谷仓的编号(1~n之间) 一个谷仓也只能有一 ...

最新文章

  1. python装饰器-究竟什么是装饰器?python中的装饰器介绍
  2. Linux系统之创建逻辑卷
  3. linux系统管理试卷必修B卷,2013-2014Linux系统管理试卷
  4. linux查看网速工具,ubuntu查看网速的工具
  5. python每天定时9点执行_python定时器每天订时执行的实例方法
  6. Incorrect string value: '/xE7/xA8/x8B/xE5/xBA/x8F...' for column 'course' at row 1
  7. 0.3:Before We Start
  8. 基于selenium的钓鱼工具:关于ReelPhish神器的使用
  9. DOTween的Sequence图例说明
  10. hive启动报错 java.net.URISyntaxException: Relative path in absolute URI: ${system:java.io.tmpdir%7D/$%7B
  11. mysql查询季度数据统计_mysql按年度、季度、月度、周、日SQL统计查询代码
  12. 刘晓燕核心词汇趣讲笔记-第十七课
  13. 蚂蚁课堂视频笔记思维导图-3期 四、源码分析
  14. 手写数学公式自动识别工具、表格自动识别
  15. 计算机网络基础 习题,计算机网络基础练习题集.pdf
  16. 阿里字体图标库iconfont的使用详解
  17. 移动硬盘上安装Ubuntu18.04系统
  18. elasticsearch query里面的slop选项
  19. 2.12 二项式系数加法解 C实现
  20. 纯js制作图片轮播效果

热门文章

  1. ASP.NET教程11
  2. [密码学基础][每个信息安全博士生应该知道的52件事][Bristol52]42蒙哥马利乘法,哪里泄漏侧信道路吗?
  3. [密码学基础][每个信息安全博士生应该知道的52件事][Bristol Cryptography][第19篇]Shamir密钥交换场景
  4. 华为云服务器安装win10系统,云服务器可以安装win10吗
  5. IDEA快捷键的使用成就手速之旅(要想手速变得快,快捷练习必须刚)
  6. 548B. Mike and Fun
  7. RT-Thread使用ENV生成工程时自己添加的文件被清掉的解决方法
  8. python sql语句生成_python Django 生成sql语句
  9. 台式电脑如何截屏_买台式电脑如何避免成为被宰羔羊(一)
  10. 状态模式和策略模式的区别