Place the Robots



http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1654


Time Limit: 5 Seconds     
Memory Limit: 32768 KB


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

题目描写叙述:

Robert 是一个著名的project师。一天,他的老板给他分配了一个任务。

任务的背景是:给定一

个m×n 大小的地图。地图由方格组成。在地图中有3 种方格-墙、草地和空地。他的老板希望

能在地图中放置尽可能多的机器人。每一个机器人都配备了激光枪,能够同一时候向四个方向(上、下、

左、右)开枪。机器人一直待在最初始放置的方格处,不可移动,然后一直朝四个方向开枪。激

光枪发射出的激光能够穿透草地,但不能穿透墙壁。

机器人仅仅能放置在空地。当然,老板不希望

机器人互相攻击。也就是说,两个机器人不能放在同一行(水平或垂直),除非他们之间有一堵墙

格开。

给定一张地图,你的程序须要输出在该地图中能够放置的机器人的最大数目。

解题思路:

将每行相连接的空白快用一个序号表示,放在x集合中;每列相连的空白快用一个表示放在集合y中。(假设两个空白中间含有草地,那么他们与属于同一个序号中)

把每一个横向块看作二部图中顶点集合X 中的顶点,竖向块看作集合Y 中的顶点。若两个块有公共的空地(注意,每两个块最多有一个公共空地),则在它们之间连边。

所以问题转化为在二部图中找没有公共顶点的最大边集,这就是最大匹配问题。

更加具体能够參考:http://blog.csdn.net/u014141559/article/details/44409255

#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxnn=2505;
const int maxn=55;
int n,m;
int nx,ny;
char pic[maxn][maxn];
int xs[maxn][maxn],ys[maxn][maxn];
vector<int> lin[maxnn];
int from[maxnn];
int tot;
bool use[maxnn];
int flag;
bool match(int x)
{
for(int i=0;i<lin[x].size();i++)
{
if(!use[lin[x][i]]){
use[lin[x][i]]=true;
if(from[lin[x][i]]==-1 || match(from[lin[x][i]]))
{
from[lin[x][i]]=x;
return true;
}
}
}
return false;
}
int hungary()
{
tot=0;
memset(from,255,sizeof(from));
for(int i=1;i<=nx;i++)
{
memset(use,0,sizeof(use));
if(match(i))
++tot;
}
return tot;
}
int main()
{
int N;
cin>>N;
for(int k=1;k<=N;k++)
{
printf("Case :%d\n",k);
scanf("%d %d",&n,&m);
for(int i=0;i<n;i++) scanf("%s",pic[i]);
nx=ny=0;
memset(xs,0,sizeof(xs));
memset(ys,0,sizeof(ys));
for(int i=0;i<n;i++)
{
flag=0;
for(int j=0;j<n;j++)
{
if(pic[i][j]=='o')
{
if(!flag) ++nx;
flag=1;
xs[i][j]=nx;
}
else if(pic[i][j]=='#')
flag=0;
}
}
for(int j=0;j<n;j++)
{
flag=0;
for(int i=0;i<n;i++)
{
if(pic[i][j]=='o')
{
if(!flag) ++ny;
flag=1;
ys[i][j]=ny;
}
else if(pic[i][j]=='#')
flag=0;
}
}
for(int i=1;i<=nx;i++) lin[i].clear();
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(xs[i][j] && ys[i][j])
lin[xs[i][j]].push_back(ys[i][j]);
}
}
printf("%d\n",hungary());
}
return 0;
}

最新文章

  1. C#提供APP接口之JSON差异
  2. Kafka 分布式的,基于发布/订阅的消息系统
  3. UVA11806-Cheerleaders(容斥原理+二进制)
  4. SqlServer2000数据库字典--表结构.sql
  5. 025. asp.net中GridView的排序和过滤
  6. &lt;string&gt;和&lt;string.h&gt;的区别
  7. python常用字符串操作
  8. 关于SQL 系统自带存储过程的使用 (一)
  9. IOS使用FMDB封装的数据库增删改查操作
  10. 更改Jenkins的workspace目录
  11. JEECG 上传插件升级-标签
  12. [C++]PAT乙级1012.数字分类 (20/20)
  13. oracle12 安装
  14. HDU 1247 - Hat’s Words - [字典树水题]
  15. springMVC源码学习地址
  16. python 3.6 的 venv 模块
  17. java ffmpeg视频转码(自测通过)
  18. POJ1222 EXTENDED LIGHTS OUT 高斯消元 XOR方程组
  19. Redis学习(5)-Jedis(Java操作redis数据库技术)
  20. 第一个Django应用程序_part2

热门文章

  1. Android微信智能心跳方案 Android微信智能心跳方案
  2. hdu_1754,线段树单点更新,求区间最值
  3. hdoj--5562--Clarke and food(模拟)
  4. js 实现 水仙花数
  5. Car Talk1
  6. 在Ubuntu下使用命令删除目录
  7. 三分钟学会用SpringMVC搭建最小系统(超详细)_转载
  8. HDU I Hate It(线段树单节点更新,求区间最值)
  9. PHP 数组转字符串,字符串转数组
  10. poj 3613 Cow Relays(矩阵的图论意义)