ZOJ 1654 Place the Robots(放置机器人)------最大独立集
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(放置机器人)------最大独立集的更多相关文章
- ZOJ 1654 - Place the Robots (二分图最大匹配)
题意:在一个m*n的地图上,有空地,草和墙,其中空地和草能穿透攻击光线,而墙不能.每个机器人能够上下左右攻击,问在地图上最多能放多少个不互相攻击的机器人. 这个题和HDU 1045 - Fire N ...
- ZOJ 1654 Place the Robots建图思维(分块思想)+二分匹配
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=654 AC一百道水题,不如AC一道难题来的舒服. 题意:一个n*m地图 ...
- 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 ...
- ZOJ 1654 Place the Robots
题目大意: 在空地上放置尽可能多机器人,机器人朝上下左右4个方向发射子弹,子弹能穿过草地,但不能穿过墙, 两个机器人之间的子弹要保证互不干扰,求所能放置的机器人的最大个数 每个机器人所在的位置确定了, ...
- ZOJ 1654 Place the Robots (二分匹配 )
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=654 Robert is a famous engineer. One ...
- ZOJ 1654 二分匹配基础题
题意: 给你一副图, 有草地(*),空地(o)和墙(#),空地上可以放机器人, 机器人向上下左右4个方向开枪(枪不能穿墙),问你在所有机器人都不相互攻击的情况下能放的最多的机器人数. 思路:这是一类经 ...
- 洛谷 - P5030 - 长脖子鹿放置 - 二分图最大独立集
https://www.luogu.org/problemnew/show/P5030 写的第一道黑色题,图建对了. 隐约觉得互相攻击要连边,规定从奇数行流向偶数行. 二分图最大独立集=二分图顶点总数 ...
- AcWing 406. 放置机器人
大型补档计划 题目链接 预处理每个列.行连续块. 每个每个列行只能在一个位置匹配,否则冲突. 符合二分图性质,跑匈牙利即可. 点数最坏情况 \(N * M\) (墙空地相间分布),边数最坏情况 \(N ...
- ACM -二分图题目小结
暂时只包括与最大匹配相关的问题. 求最大独立集,最小路径覆盖等等大多数题目都可以转化为求最大匹配用匈牙利算法解决. 1.最大匹配(边集) 此类问题最直接,直接用匈牙利算法即可. HDU 2063 过 ...
随机推荐
- less09 判断语句
less //.mixin (@a) when (lightness(@a) >= 50%) { //255/2=127.5 // background-color: black; //} // ...
- DDos游戏行业受攻击最多
游戏行业遭遇DDOS攻击现状. 游戏一直是最易遭受黑客攻击的行业,高居全年DDOS攻击的48%.大规模攻击居多,平均值均超过100Gbps,攻击峰值急速上升,同比2015年增加了137.1%,其中攻击 ...
- 知方可补不足~powerDesign为模型添加注释(让生成的SQL有注释)
事实上powerDesign本身就有这个功能,不需要我们修改它的生成器了,这种方法够简单! 一 打开表模型,选择column标签
- 使用tinyxml2库解析xml
tinyxml2简介 tinyxml2是c++编写的轻量级的xml解析器,而且是开放源代码的,在一些开源的游戏引擎中用的比较多.源码托管在github上. 源码地址:https://github.co ...
- Android 得到根Fragment
public Fragment getRootFragment() { PlayFragment xFragment = null; for (Fragment fragment : getFragm ...
- 【转载】AngularJS 用$sce服务来过滤HTML标签,解决无法正确显示后台传递的html标签
angular js的强大之处之一就是他的数据双向绑定这一牛B功能,我们会常常用到的两个东西就是ng-bind和针对form的ng-model.但在我们的项目当中会遇到这样的情况,后台返回的数据中带有 ...
- SQLServer中同义词Synonym的用法
以前一直认为SqlServer中的同义词(Synonym)没有什么用处,所以也一直没有去查它的语法格式.今天碰到一个问题,用Synonym来解决再好不过了.问题是这样子的,我的系统中用到了多个数据库, ...
- myisam和innodb的qubie
1.Myisam 支持锁表,innoDB 支持行锁. 2.innoDB 和 BDB 支持事务. 3.Myisam 与 innoDB 索引的区别: Myisam 无论是主键索引还是其他索引,索 ...
- word/excel/ppt 2 PDF
PHP 实现 word/excel/ppt 转换为 PDF 一般最常见的就是利用OpenOffice来转换,来看看实现的核心代码: class PDFConverter { private $com; ...
- 紫书 习题 10-2 UVa 808(建立坐标+找规律)
这次是我遇见过最迷的一次 我写的程序uDebug全过 和ac程序对拍也过,求出来的坐标是一模一样的,最后结果输出的方式也是一样的 交上去就是错的 迷 第一次遇到这种情况 大佬在哪里 #include& ...