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. nj04---事件回调函数

    一.回调函数 1.异步式读取文件 var fs=require('fs'); fs.readFile('file.txt','utf-8',function(err,data){ if(err){ c ...

  2. IIS访问站点,出现connection refused

    排查后,发现是因为使用了代理导致的. 需要设置 Don't use the proxy server for local addresses.

  3. BZOJ 1711 网络流

    思路: 神建图 源->饮料 边权为1 牛拆点 两个点之间的边权为1 饮料->左牛 边权为1 右牛->食品 边权为1 食品->汇边权为1 //By SiriusRen #incl ...

  4. JDK5新特性:可变参数方法

    JDK1.5增加可变参方法,其定义格式为: 访问修饰符 返回值类型 方法标识符(参数类型 参数标识符1,参数类型 参数标识符2,参数类型...参数标识符){} 如可能要定义一个求和功能的方法,但求和的 ...

  5. JavaScript中函数作为另一个函数的参数的时候它存在于哪个作用域

    一直对函数作为参数被传递进另外一个函数理解的不是很清除.先看下这段代码吧: function test(fn){ var bar = 1; fn(); } var bar = 99; test(fun ...

  6. NodeJS学习笔记 (31)定时器-timers

    https://github.com/chyingp/nodejs-learning-guide

  7. javascript ---(常用工具类的封装)

    1. type 类型判断 isString(o) { //是否字符串 return Object.prototype.toString.call(o).slice(8, -1) === 'String ...

  8. const 和 pointer

    一般的: const对pointer的修饰有两种: const type * p/type const * p:表示指针指向的变量的值不能改变,无论指针改变为指向哪一个变量 type * const ...

  9. caioj 1077 动态规划入门(非常规DP1:筷子)

    首先可以看出排序之后,最优解肯定是每一对都相邻才是最优的 那么我们就要找构成最优解的相邻组 设f[i][j]是前i个字符,k对的最小值 如果当前这个筷子不取的话,f[i][j] = f[i-1][j] ...

  10. 【Codeforces Round #423 (Div. 2) A】Restaurant Tables

    [Link]:http://codeforces.com/contest/828/problem/A [Description] 有n个组按照时间顺序来餐馆; 每个组由一个人或两个人组成; 每当有一个 ...