Robert is a famous engineer. One day he was given a task by his boss. The background of the task was the following:



Given a map consisting of square blocks. There were three kinds of blocks: Wall, Grass, and Empty. His boss wanted to place as many robots as possible in the map. Each robot held a laser weapon which could shoot to four directions (north, east, south, west)
simultaneously. A robot had to stay at the block where it was initially placed all the time and to keep firing all the time. The laser beams certainly could pass the grid of Grass, but could not pass the grid of Wall. A robot could only be placed in an Empty
block. Surely the boss would not want to see one robot hurting another. In other words, two robots must not be placed in one line (horizontally or vertically) unless there is a Wall between them.



Now that you are such a smart programmer and one of Robert's best friends, He is asking you to help him solving this problem. That is, given the description of a map, compute the maximum number of robots that can be placed in the map.



Input




The first line contains an integer T (<= 11) which is the number of test cases. 



For each test case, the first line contains two integers m and n (1<= m, n <=50) which are the row and column sizes of the map. Then m lines follow, each contains n characters of '#', '*', or 'o' which represent Wall, Grass, and Empty, respectively.

Output



For each test case, first output the case number in one line, in the format: "Case :id" where id is the test case number, counting from 1. In the second line just output the maximum number of robots that can be placed in that map.

Sample Input

2

4 4

o***

*###

oo#o

***o

4 4

#ooo

o#oo

oo#o

***#

Sample Output

Case :1

3

Case :2

5

题意:机器人能攻击跟它所在同一行跟列的全部东西,仅仅有'o'才干放机器人,'#'表示墙壁,能挡住机器人的攻击(意味着墙壁之间能放机器人),要你求出n*m的矩阵上能放多少机器人

思路:最大独立集。可惜眼下没有不论什么算法能求出最大独立集。

那么我们换一个思路,之前做过POJ3041,能够类似地做这道题,可是本题中的墙壁为我们的标记提供了难度。那么我么能够用xs数组看做X集合(表示每行中的'o'的位置。假设不相容则标记为同一个数),同理做一个列的ys数组!

相应下来。在每个'o'上连接两集合的点形成边,我们发现符合题目的每条边之间不能有公共点,所以就转化为了最小边覆盖的问题,刚好就是最大匹配!

AC代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int N = 55;
const int maxn=2600; int link[maxn][maxn];
int xs[N][N],ys[N][N];
int col[maxn],vis[maxn];
int mx,my; int n,m; int match(int x)
{
int i;
for(i=1;i<=my;i++){
if(link[x][i]&&!vis[i])
{
vis[i]=1;
if(!col[i]||match(col[i]))
{
col[i]=x;
return 1;
}
}
}
return 0;
} char str[N][N]; int main()
{
#ifndef ONLINE_JUDGE
freopen("in.cpp","r",stdin);
freopen("out.cpp","w",stdout);
#endif // ONLINE_JUDGE
int t;
scanf("%d",&t);
int cas=1;
while(t--)
{
int cnt=1;
scanf("%d %d",&n,&m);
memset(xs,0,sizeof(xs));
memset(ys,0,sizeof(ys));
getchar();
for(int i=0;i<n;i++)
{
scanf("%s",str[i]);
for(int j=0;j<m;j++)
{
if(str[i][j]=='o')
{
xs[i][j]=cnt;
} else if(str[i][j]=='#')
cnt++;
}
cnt++;
}
int maxx=cnt;
mx=cnt;
cnt=1;
for(int j=0;j<m;j++)
{
for(int i=0;i<n;i++)
{
if(str[i][j]=='o')
ys[i][j]=cnt;
else if(str[i][j]=='#')
cnt++;
}
cnt++;
}
my=cnt;
memset(link,0,sizeof(link));
memset(col,0,sizeof(col));
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(str[i][j]=='o')
{
link[xs[i][j]][ys[i][j]]=1;
}
}
} int tot=0;
for(int i=1;i<=mx;i++)
{
memset(vis,0,sizeof(vis));
if(match(i))tot++;
}
printf("Case :%d\n",cas++);
printf("%d\n",tot);
}
return 0;
}

ZOJ 1654 Place the Robots(最大匹配)的更多相关文章

  1. ZOJ 1654 - Place the Robots (二分图最大匹配)

    题意:在一个m*n的地图上,有空地,草和墙,其中空地和草能穿透攻击光线,而墙不能.每个机器人能够上下左右攻击,问在地图上最多能放多少个不互相攻击的机器人. 这个题和HDU 1045 -  Fire N ...

  2. ZOJ 1654 Place the Robots(放置机器人)------最大独立集

    Place the Robots http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1654 Time Limit: 5 Sec ...

  3. ZOJ 1654 Place the Robots (二分匹配 )

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=654 Robert is a famous engineer. One ...

  4. ZOJ 1654 Place the Robots建图思维(分块思想)+二分匹配

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=654 AC一百道水题,不如AC一道难题来的舒服. 题意:一个n*m地图 ...

  5. ZOJ 1654 Place the Robots

    题目大意: 在空地上放置尽可能多机器人,机器人朝上下左右4个方向发射子弹,子弹能穿过草地,但不能穿过墙, 两个机器人之间的子弹要保证互不干扰,求所能放置的机器人的最大个数 每个机器人所在的位置确定了, ...

  6. [ACM_动态规划] ZOJ 1425 Crossed Matchings(交叉最大匹配 动态规划)

    Description There are two rows of positive integer numbers. We can draw one line segment between any ...

  7. ZOJ 1364 Machine Schedule(二分图最大匹配)

    题意 机器调度问题 有两个机器A,B A有n种工作模式0...n-1 B有m种工作模式0...m-1 然后又k个任务要做 每一个任务能够用A机器的模式i或b机器的模式j来完毕 机器開始都处于模式0 每 ...

  8. ZOJ 3316 Game 一般图最大匹配带花树

    一般图最大匹配带花树: 建图后,计算最大匹配数. 假设有一个联通块不是完美匹配,先手就能够走那个没被匹配到的点.后手不论怎么走,都必定走到一个被匹配的点上.先手就能够顺着这个交错路走下去,最后一定是后 ...

  9. ZOJ 1654 二分匹配基础题

    题意: 给你一副图, 有草地(*),空地(o)和墙(#),空地上可以放机器人, 机器人向上下左右4个方向开枪(枪不能穿墙),问你在所有机器人都不相互攻击的情况下能放的最多的机器人数. 思路:这是一类经 ...

随机推荐

  1. ListView(2)最简单的上拉刷新、下拉刷新代码

    效果 最简单的上拉刷新和下拉刷新,当listview滚动到底部时向上拉刷新数据.当listview滚动到最顶部时下拉刷新.       图1,上拉刷新 图2,下拉刷新 1.设置lisview 加载he ...

  2. 20小时掌握网站开发(免费精品htmlcss视频教程)

    自己最近研发了一套新的htmlcss教程,并进行了授课实施,视频教程百度云下载链接如下: 视频及案例源码下载地址 本套教程视频需要安装屏幕录像专家软件才能观看,屏幕录像专家下载地址如下: 屏幕录像专家 ...

  3. index seek和index scan 提高sql 效率

    index seek和index scan 提高sql 效率解释解释index seek和index scan:索引是一颗B树,index seek是查找从B树的根节点开始,一级一级找到目标行.ind ...

  4. servlet-后台获取form表单传的参数

    前台代码: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> & ...

  5. CNN结构:用于检测的CNN结构进化-分离式方法

    前言: 原文链接:基于CNN的目标检测发展过程       文章有大量修改,如有不适,请移步原文. 参考文章:图像的全局特征--用于目标检测 目标的检测和定位中一个很困难的问题是,如何从数以万计的候选 ...

  6. Swift进阶之内存模型和方法调度

    前言 Apple今年推出了Swift3.0,较2.3来说,3.0是一次重大的升级.关于这次更新,在这里都可以找到,最主要的还是提高了Swift的性能,优化了Swift API的设计(命名)规范. 前段 ...

  7. 范畴论-一个单子(Monad)说白了不过就是自函子范畴上的一个幺半群而已

    范畴即为结构:包含要素和转化. 范畴为高阶类型. 函子为高阶函数.函子的输入为态射.函子为建立在态射基础上的高阶函数.函子用于保持范畴间映射的结构.态射用于范畴内部的转换. 群为运算规则的约束. 自函 ...

  8. C# 检测字符串是否为数字

    long n; 1. ], ].All(char.IsDigit); //识别空字符时候 会认为是数字 string str0 = ""; string str1 = " ...

  9. EMC VNX5200/5400存储 新增LUN与Hosts映射操作

    EMC VNX5200/5400 1.创建RAID  Groups 1.1        进入EMC VNX5200/5400主界面,依次选择Storage——Storage Pools——RAID ...

  10. day37-2元类,单例模式

    目录 元类 造类的第一种形式 class做了什么事 控制元类产生的类 控制元类产生的对象 实例化类 加上元类后类的属性查找顺序 元类控制模版 单例模式 1. 使用类方法的特性 2. 使用装饰器 3. ...