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. WebApi里面路由机制的原理以及路由匹配的过程

    1.WebApi服务启动之后,会执行全局配置文件Global.asax.cs的 protected void Application_Start(){GlobalConfiguration.Confi ...

  2. ThinkPHP5中的助手函数

    load_trait:快速导入Traits,PHP5.5以上无需调用 /** * 快速导入Traits PHP5.5以上无需调用 * @param string    $class trait库 *  ...

  3. Android Studio and Gradle安装心得

    安装基于Eclipse 的ADT一段时间,感觉确实有很多功能不足,通过网上资料,决定改向AS. AS下载了最新的2.3版本,它不分64位与32位,网上说有单独版是瞎扯蛋.只要启动不同的EXE就行了. ...

  4. php数据库批量删除

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...

  5. Matlab移植到Eigen用到的词条

    同型矩阵运算满足加法交换律.结合律:并存在单位元.逆元.和0元,为同型矩阵对加法的交换环. Eigen的简单运算参考:http://blog.163.com/jiaqiang_wang/blog/st ...

  6. Vue双向数据绑定实现原理

    https://zendq1998.github.io/2018/04/12/vue%E5%8F%8C%E5%90%91%E6%95%B0%E6%8D%AE%E7%BB%91%E5%AE%9A%E5% ...

  7. 模拟人的手指在UI上滑动时3D模型跟随着移动(Unity)

    问题: 怎么让当手指滑动的同时对应的模型发生旋转 解决办法: 1:通过控制摄像机或者模型来实现效果 2:通过获取鼠标移动时X轴Y轴的偏移量来确定模型的旋转角度 3:为了不让人感觉到突兀,建议使用Mat ...

  8. GFS分布式文件系统理论个人总结

    GlusterFS 两种模式 可以通过TCP/IP和RDMA高速网络互连,客户端可通过原生Gluster协议访问数据 没有GlusterFS客户端的可以通过NFS/CIFS标准协议通过存储网关访问数据 ...

  9. 【第二课】kaggle案例分析二

    Evernote Export 推荐系统比赛(常见比赛) 推荐系统分类 最能变现的机器学习应用 基于应用领域分类:电子商务推荐,社交好友推荐,搜索引擎推荐,信息内容推荐等 **基于设计思想:**基于协 ...

  10. eas更改用户组织范围和业务组织范围

    表: T_PM_OrgRangeIncludeSubOrg 10 20 30 分别代表 业务组织 行政组织 以及管辖组织.查行政组织,