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 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
题意:机器人能攻击跟它所在同一行跟列的全部东西,仅仅有'o'才干放机器人,'#'表示墙壁,能挡住机器人的攻击(意味着墙壁之间能放机器人),要你求出n*m的矩阵上能放多少机器人
思路:最大独立集。可惜眼下没有不论什么算法能求出最大独立集。
那么我们换一个思路,之前做过POJ3041,能够类似地做这道题,可是本题中的墙壁为我们的标记提供了难度。那么我么能够用xs数组看做X集合(表示每行中的'o'的位置。假设不相容则标记为同一个数),同理做一个列的ys数组!
相应下来。在每个'o'上连接两集合的点形成边,我们发现符合题目的每条边之间不能有公共点,所以就转化为了最小边覆盖的问题,刚好就是最大匹配!
AC代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int N = 55;
const int maxn=2600; int link[maxn][maxn];
int xs[N][N],ys[N][N];
int col[maxn],vis[maxn];
int mx,my; int n,m; int match(int x)
{
int i;
for(i=1;i<=my;i++){
if(link[x][i]&&!vis[i])
{
vis[i]=1;
if(!col[i]||match(col[i]))
{
col[i]=x;
return 1;
}
}
}
return 0;
} char str[N][N]; int main()
{
#ifndef ONLINE_JUDGE
freopen("in.cpp","r",stdin);
freopen("out.cpp","w",stdout);
#endif // ONLINE_JUDGE
int t;
scanf("%d",&t);
int cas=1;
while(t--)
{
int cnt=1;
scanf("%d %d",&n,&m);
memset(xs,0,sizeof(xs));
memset(ys,0,sizeof(ys));
getchar();
for(int i=0;i<n;i++)
{
scanf("%s",str[i]);
for(int j=0;j<m;j++)
{
if(str[i][j]=='o')
{
xs[i][j]=cnt;
} else if(str[i][j]=='#')
cnt++;
}
cnt++;
}
int maxx=cnt;
mx=cnt;
cnt=1;
for(int j=0;j<m;j++)
{
for(int i=0;i<n;i++)
{
if(str[i][j]=='o')
ys[i][j]=cnt;
else if(str[i][j]=='#')
cnt++;
}
cnt++;
}
my=cnt;
memset(link,0,sizeof(link));
memset(col,0,sizeof(col));
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(str[i][j]=='o')
{
link[xs[i][j]][ys[i][j]]=1;
}
}
} int tot=0;
for(int i=1;i<=mx;i++)
{
memset(vis,0,sizeof(vis));
if(match(i))tot++;
}
printf("Case :%d\n",cas++);
printf("%d\n",tot);
}
return 0;
}
ZOJ 1654 Place the Robots(最大匹配)的更多相关文章
- ZOJ 1654 - Place the Robots (二分图最大匹配)
题意:在一个m*n的地图上,有空地,草和墙,其中空地和草能穿透攻击光线,而墙不能.每个机器人能够上下左右攻击,问在地图上最多能放多少个不互相攻击的机器人. 这个题和HDU 1045 - Fire N ...
- ZOJ 1654 Place the Robots(放置机器人)------最大独立集
Place the Robots http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1654 Time Limit: 5 Sec ...
- ZOJ 1654 Place the Robots (二分匹配 )
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=654 Robert is a famous engineer. One ...
- ZOJ 1654 Place the Robots建图思维(分块思想)+二分匹配
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=654 AC一百道水题,不如AC一道难题来的舒服. 题意:一个n*m地图 ...
- ZOJ 1654 Place the Robots
题目大意: 在空地上放置尽可能多机器人,机器人朝上下左右4个方向发射子弹,子弹能穿过草地,但不能穿过墙, 两个机器人之间的子弹要保证互不干扰,求所能放置的机器人的最大个数 每个机器人所在的位置确定了, ...
- [ACM_动态规划] ZOJ 1425 Crossed Matchings(交叉最大匹配 动态规划)
Description There are two rows of positive integer numbers. We can draw one line segment between any ...
- ZOJ 1364 Machine Schedule(二分图最大匹配)
题意 机器调度问题 有两个机器A,B A有n种工作模式0...n-1 B有m种工作模式0...m-1 然后又k个任务要做 每一个任务能够用A机器的模式i或b机器的模式j来完毕 机器開始都处于模式0 每 ...
- ZOJ 3316 Game 一般图最大匹配带花树
一般图最大匹配带花树: 建图后,计算最大匹配数. 假设有一个联通块不是完美匹配,先手就能够走那个没被匹配到的点.后手不论怎么走,都必定走到一个被匹配的点上.先手就能够顺着这个交错路走下去,最后一定是后 ...
- ZOJ 1654 二分匹配基础题
题意: 给你一副图, 有草地(*),空地(o)和墙(#),空地上可以放机器人, 机器人向上下左右4个方向开枪(枪不能穿墙),问你在所有机器人都不相互攻击的情况下能放的最多的机器人数. 思路:这是一类经 ...
随机推荐
- Java 网络IO编程(BIO、NIO、AIO)
本概念 BIO编程 传统的BIO编程 代码示例: public class Server { final static int PROT = 8765; public static void main ...
- 强迫症!一行代码拿到url特定query的值
简单的说一下背景,看到小伙伴给我发的项目中有一段获取当前url特定query值的代码,本着能写1行代码就不写5行代码的原则,我把这个获取方法给改了一下 之前的代码如下: const queryArr ...
- windows 装XP系统
笔记本型号:HPCQ40-506AX 1.在BIOS中更改启动顺序:将USB设为第一启动项2.插入装有PE系统的USB设备3.开机后一直按F124.到达选择系统界面,目前我的HPCQ40用其他系统进去 ...
- Md2All版本更新记录
Md2All版本更新记录 版本号:V2.8.2更新日期:2018-06-281:结合云图床,解决了Latex公式复制到公众号时有可能报“图片粘贴失败的问题”;2:结合云图床,解决了Latex公式复制到 ...
- 01--Java IO基础
一.java.io包概览 Java IO包主要可以分为如下4类: 基于字节操作的I/O接口:InputStream和OutputStream. 基于字符操作的I/O接口:Writer和Reader 基 ...
- SLAM: Inverse Depth Parametrization for Monocular SALM
首语: 此文实现客观的评测了使线性化的反转深度的效果.整篇只在表明反转可以线性化,解决距离增加带来的增长问题,有多少优势--%! 我的天呢!我竟然完整得翻译了一遍. 使用标记点地图构建SLAM的方法, ...
- 【sqli-labs】 less20 POST - Cookie injections - Uagent field - Error based (POST型基于错误的cookie头部注入)
以admin admin成功登陆之后,保存并显示了cookies信息 如果不点击Delete Your Cookie!按钮,那么访问 http://localhost/sqli-labs-master ...
- Type inference
Type inference refers to the automatic detection of the data type of an expression in a programming ...
- 分块编码(Transfer-Encoding: chunked)VS Content-length
参考链接: HTTP 协议中的 Transfer-Encoding 分块传输编码 https://www.cnblogs.com/xuehaoyue/p/6639029.html 一.背景: 持续连接 ...
- Math工具类的使用
1.包:java.lang 不需要导包 2.Math 类包含用于执行基本数学运算的方法,如初等指数.对数.平方根和三角函数. 特点: 该类中的方法都是静态方法,所以可以直接使用类名.方法名(实参)调 ...