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;
}

ZOJ 1654 Place the Robots(放置机器人)------最大独立集的更多相关文章

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

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

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

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

  3. ZOJ 1654 Place the Robots(最大匹配)

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

  4. ZOJ 1654 Place the Robots

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

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

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

  6. ZOJ 1654 二分匹配基础题

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

  7. 洛谷 - P5030 - 长脖子鹿放置 - 二分图最大独立集

    https://www.luogu.org/problemnew/show/P5030 写的第一道黑色题,图建对了. 隐约觉得互相攻击要连边,规定从奇数行流向偶数行. 二分图最大独立集=二分图顶点总数 ...

  8. AcWing 406. 放置机器人

    大型补档计划 题目链接 预处理每个列.行连续块. 每个每个列行只能在一个位置匹配,否则冲突. 符合二分图性质,跑匈牙利即可. 点数最坏情况 \(N * M\) (墙空地相间分布),边数最坏情况 \(N ...

  9. ACM -二分图题目小结

    暂时只包括与最大匹配相关的问题. 求最大独立集,最小路径覆盖等等大多数题目都可以转化为求最大匹配用匈牙利算法解决. 1.最大匹配(边集) 此类问题最直接,直接用匈牙利算法即可. HDU 2063  过 ...

随机推荐

  1. 如何在github的README.md中添加图片

    如何在github的README.md中添加图片 总结: 链接引用:![Image text](图片的链接地址) 简介: 1.在github上的仓库建立一个存放图片的文件夹,文件夹名字随意.如:img ...

  2. idea报错。Error:Failed to load project configuration: cannot parse xml file E:\project\.idea\workspace.xml: Error on line 1: 前言中不允许有内容。

    因为电脑卡死强制重启电脑后打开idea,进行junit单元测试报错: idea报错.Error:Failed to load project configuration: cannot parse x ...

  3. SpringMVC学习一:SpringMVC的配置

    SpringMVC的配置主要分为两部分: 1.xml文件配置 2.注解的配置 SpringMVC配置的步骤如下: 1.在将SpringMVC的jar包导入到web项目中后,先配置web.xml 文件. ...

  4. coedforces #481Div(3)(ABCDEFG)

    A. Remove Duplicates Petya has an array aconsisting of nintegers. He wants to remove duplicate (equa ...

  5. 一篇文章教会你理解Scrapy网络爬虫框架的工作原理和数据采集过程

    今天小编给大家详细的讲解一下Scrapy爬虫框架,希望对大家的学习有帮助. 1.Scrapy爬虫框架 Scrapy是一个使用Python编程语言编写的爬虫框架,任何人都可以根据自己的需求进行修改,并且 ...

  6. Python组织文件 实践:将带有美国风格日期的文件改名为欧洲风格日期

    描述:假设有这样一个任务,你需要将文件名中含有美国风格日期(MM-DD-YYYY)的部分更换为欧洲风格日期(DD-MM-YYYY),并且需要你处理的文件多达上千个 分析:检查当前工作目录的所有文件名, ...

  7. 一种基于RBAC模型的动态访问控制改进方法

    本发明涉及一种基于RBAC模型的动态访问控制改进方法,属于访问控制领域.对原有RBAC模型进行了权限的改进和约束条件的改进,具体为将权限分为静态权限和动态权限,其中静态权限是非工作流的权限,动态权限是 ...

  8. Win8.1应用开发之文件操作

    在操作文件之前,先相应用的应用功能声明进行设定.用户通过C#(非UI)对win8.1上的文件进行訪问,仅仅能局限于图片,音乐,视频和文档四个目录. 而通过文件选取器则能訪问到整个系统的文件. (一)应 ...

  9. js插件---强大的图片裁剪Cropper

    js插件---强大的图片裁剪Cropper 一.总结 一句话总结:官网或者github里面的文档或者demo才是真的详细 使用的话找到图片裁剪后的base64数据,然后这个数据可下载可传递到服务器 1 ...

  10. hdoj--1342--Lotto(dfs)

    Lotto Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm ...