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 过 ...
随机推荐
- Nginx-虚拟主机配置问题
Nginx-虚拟主机配置问题 标签(空格分隔): linux,php,nginx,larave 这两天突然想配置lnmp环境,学习下Nginx配置结果就遇到了下边的问题 Nginx: server下的 ...
- 微信小程序领取卡券
微信小程序领取卡券 标签(空格分隔): php 开发前需要准备的工作 1 小程序和公众号要有绑定 2 小程序和该公众号要绑定到同一个开发平台下 [https://open.weixin.qq.com/ ...
- MetaSploit攻击实例讲解------工具Meterpreter常用功能介绍(kali linux 2016.2(rolling))(详细)
不多说,直接上干货! 说在前面的话 注意啦:Meterpreter的命令非常之多,本篇博客下面给出了所有,大家可以去看看.给出了详细的中文 由于篇幅原因,我只使用如下较常用的命令. 这篇博客,利用下面 ...
- 如何配置MySQL?(三)
要进行mysql配置,首先要找到mysql的配置向导文件,这个配置向导文件就在我们安装目录下的一个bin的子目录下. 刚才我们是选择典型安装MySQL,一般windows是默认存储在C:\Progra ...
- iOS菜鸟成长笔记(2)——网易彩票练习
距离上一篇<第一个iOS应用>已经有一个多月了,今天来和大家一起学习和分享一下一个小练习<网易彩票> 首先我们向storyboard中拖入一个TabBarController和 ...
- MPP的进化 - 深入理解Batch和MPP优缺点
https://mp.weixin.qq.com/s/scXNfkpjktCZxBg3pYEUUA?utm_medium=hao.caibaojian.com&utm_source=hao.c ...
- vue的webpack打包
一个完整的项目离不开 开发环境 生产环境 测试环境 这三个环境 首先解释一下这三个环境的含义 开发环境:开发环境是程序猿们专门用于开发的服务器,配置可以比较随意,为了开发调试方便,一般打开全部错误报告 ...
- 一个icon的选中与不选中
页面的样式展示 1.页面中选中的状态 2.页面中未选中的状态 3.俩个icon代表的状态 页面的布局展示 <label> <i class="iconfont icon-d ...
- phpstorm10安装并汉化
一.下载phpstorm 下载地址:https://pan.baidu.com/s/1R64ZROVP1ljGbYfCwWjwxA 二.一直点击下一步安装即可 注意:第3步的时候选择一下支持的后缀 三 ...
- CRC校验原理及步骤
什么是CRC校验? CRC即循环冗余校验码:是数据通信领域中最常用的一种查错校验码,其特征是信息字段和校验字段的长度可以任意选定.循环冗余检查(CRC)是一种数据传输检错功能,对数据进行多项式计算,并 ...