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个方向开枪(枪不能穿墙),问你在所有机器人都不相互攻击的情况下能放的最多的机器人数. 思路:这是一类经 ...
随机推荐
- selenium3 + Python - 处理浏览器弹窗(转载)
作者:Real_Tino 转载链接:https://blog.csdn.net/real_tino/article/details/59068827 我们在浏览网页时经常会碰到各种花样的弹窗,在做UI ...
- Windows7下caffe-ssd-microsoft下编译
整个编译可谓漫长 编译了两天 网上教程也很多 但是也很杂 遇到各种错误 总归是编完了 1.下载Windows版本的Caffe-SSD源码 下载链接:https://github.com/conner9 ...
- indeed 4.22 第一次网测
1.第一题 没有看 2. 由于数据范围很小,所以每一层需要全排列,寻找最小的花费,然后所有层加起来就是最后的结果. #include<bits/stdc++.h> #define pb p ...
- android 将手机号中间隐藏为星号(*)
){ StringBuilder sb =new StringBuilder(); ; i < pNumber.length(); i++) { char c = pNumber.charAt( ...
- 努比亚(nubia) M2青春版 NX573J 解锁BootLoader 并进入临时recovery ROOT
努比亚(nubia) M2青春版 NX573J 解锁BootLoader 并进入临时recovery ROOT 工具下载链接:https://pan.baidu.com/s/1NfRTdXtdAZRi ...
- chown chmod chgrp chattr chroot usermod 命令简单分析
chown用于修改文件或目录的所属主与所属组,格式为:“chown [参数] 所属主:所属组 文件或目录名称”.[root@localtion ~]# chown root:bin test[root ...
- java Web(3)
Servlet 是运行在Web服务器或应用服务器上的Java程序 在Web上创建动态内容的有效而强大的解决方案 由容器来管理生命周期与Web服务器交互 由Sun规范了其功能 Servlet部署: 一个 ...
- 【sqli-labs】 less40 GET -Blind based -String -Stacked(GET型基于盲注的堆叠查询字符型注入)
提交,页面正常,说明是')闭合的 http://192.168.136.128/sqli-labs-master/Less-40/?id=1')%23 http://192.168.136.128/s ...
- Data mapping-数据映射
数据映射:根据数据的结构信息建立数据间的映射操作机制. 数据映射的要素: 一.数据 1.源数据: 2.目标数据: 3.数据间关系: 4.数据的元数据(结构信息). 5.元素类型的对应关系. 二.元数据 ...
- vue使用插槽分发内容slot的用法
将父组件的内容放到子组件指定的位置叫做内容分发 //在父组件里使用子组件 <son-tmp> <div>我是文字,我需要放到son-tmp组件里面制定的位置</div&g ...