ZOJ 1654 Place the Robots
题目大意:
在空地上放置尽可能多机器人,机器人朝上下左右4个方向发射子弹,子弹能穿过草地,但不能穿过墙,
两个机器人之间的子弹要保证互不干扰,求所能放置的机器人的最大个数
每个机器人所在的位置确定了,那么对应的横向和竖向子弹能到达的空地就全部被覆盖了
我们将横向所能连接在一块的空地区域标上同一个标号
比如o*o#o , 就可标号为10102因为1,3空地中间的草地不影响子弹的穿越
同理,我们将竖向的所能连接在一块的空地区域标上同一个标号
那么就可以建立一个横向到达竖向的二部图匹配
每次成功匹配一对,就相当于在这横竖交叉处放了一个炮台
这就转化成了最大匹配数
#include <cstdio>
#include <cstring> using namespace std;
const int N = ; char str[N][N];
int n , m , xs[N][N] , ys[N][N] , idx , idy;//idx记录以行为轴的最大标号,idy则表示以列为轴的最大标号
int cx[N*N] , cy[N*N] , visy[N*N];
bool g[N*N][N*N];//int是4个字节,用int会MLE,bool一个字节 int dfs(int u)
{
for(int v = ; v<=idy ; v++){
if(g[u][v] && !visy[v]){
visy[v] = ;
if(cy[v] == - || dfs(cy[v])){
cx[u] = v;
cy[v] = u;
return ;
}
}
}
return ;
} int MaxMatch()
{
memset(cx , - , sizeof(cx));
memset(cy , - , sizeof(cy));
int ans = ;
for(int i= ; i<=idx ; i++){
if(cx[i] == -){
memset(visy , , sizeof(visy));
ans += dfs(i);
}
}
return ans;
} int main()
{
// freopen("a.in" , "r" , stdin);
int T , cas = ;
scanf("%d" , &T);
while(T--)
{
scanf("%d%d" , &n , &m);
for(int i = ; i<n ; i++)
scanf("%s" , str[i]); //给每行炮台所能触及的位置编为相同号
int id = , flag = ;
for(int i= ; i<n ; i++){
if(flag) flag = , id++;
for(int j = ; j < m ; j++){
if(str[i][j] == 'o')
xs[i][j] = id , idx = id , flag = ;
else if(str[i][j] == '#')
flag = , id++;
}
} //给每列炮台所能触及的位置编为相同号
id = , flag = ;
for(int i= ; i<m ; i++){
if(flag) flag = , id++;
for(int j = ; j < n ; j++){
if(str[j][i] == 'o')
ys[j][i] = id , idy = id , flag = ;
else if(str[j][i] == '#')
flag = , id++;
}
} //构造二部图
memset(g , , sizeof(g));
for(int i = ; i<n ; i++)
for(int j = ; j<m ; j++){
if(str[i][j] == 'o')
g[xs[i][j]][ys[i][j]] = true;
}
printf("Case :%d\n%d\n" , ++cas , MaxMatch());
}
return ;
}
ZOJ 1654 Place the Robots的更多相关文章
- 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(放置机器人)------最大独立集
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 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 (二分图最大匹配)
题意:在一个m*n的地图上,有空地,草和墙,其中空地和草能穿透攻击光线,而墙不能.每个机器人能够上下左右攻击,问在地图上最多能放多少个不互相攻击的机器人. 这个题和HDU 1045 - Fire N ...
- ZOJ 1654 二分匹配基础题
题意: 给你一副图, 有草地(*),空地(o)和墙(#),空地上可以放机器人, 机器人向上下左右4个方向开枪(枪不能穿墙),问你在所有机器人都不相互攻击的情况下能放的最多的机器人数. 思路:这是一类经 ...
- ACM -二分图题目小结
暂时只包括与最大匹配相关的问题. 求最大独立集,最小路径覆盖等等大多数题目都可以转化为求最大匹配用匈牙利算法解决. 1.最大匹配(边集) 此类问题最直接,直接用匈牙利算法即可. HDU 2063 过 ...
- ZOJ 1654--Place the Robots【二分匹配 && 经典建图】
Place the Robots Time Limit: 5 Seconds Memory Limit: 32768 KB Robert is a famous engineer. One ...
- ZOJ题目分类
ZOJ题目分类初学者题: 1001 1037 1048 1049 1051 1067 1115 1151 1201 1205 1216 1240 1241 1242 1251 1292 1331 13 ...
随机推荐
- 论文翻译-SELF TRAINING AUTONOMOUS DRIVING AGENT
文献地址 链接:https://pan.baidu.com/s/1gHrpnOf1FXLp9u8OJ2-oCg 提取码:y2w6 作者 Shashank Kotyan, Danilo Vasconce ...
- jQuery——表单应用(3)
HTML: <!--表单-多行文本框应用-滚动条高度变化--> <!DOCTYPE html> <html> <head> <meta chars ...
- Hdu 4612 Warm up (双连通分支+树的直径)
题目链接: Hdu 4612 Warm up 题目描述: 给一个无向连通图,问加上一条边后,桥的数目最少会有几个? 解题思路: 题目描述很清楚,题目也很裸,就是一眼看穿怎么做的,先求出来双连通分量,然 ...
- Android 性能优化(10)网络优化( 6)Optimizing General Network Use
Optimizing General Network Use This lesson teaches you to Compress Data Cache Files Locally Optimize ...
- 306 Additive Number 加法数
Additive number is a string whose digits can form additive sequence.A valid additive sequence should ...
- NPOI 导出excel数据超65535自动分表
工作上遇到的问题,网上找了一些资料 整理了一个比较可行的解决方案. NPOI 大数据量分多个sheet导出 代码段 /// <summary> /// DataTable转换成Excel文 ...
- NodeJs学习记录(一)初步学习,杂乱备忘
2016/12/26 星期一 1.在win7下安装了NodeJs 1)进入官网 https://nodejs.org/en/download/,下载对应的安装包,我目前下载的是node-v6.2.0- ...
- LN : leetcode 515 Find Largest Value in Each Tree Row
lc 515 Find Largest Value in Each Tree Row 515 Find Largest Value in Each Tree Row You need to find ...
- window下编写python脚本在linux下运行出错 usr/bin/python^M: bad interpreter: No such file or directory
今天在windows下使用notepad++写了个python脚本,传到linux服务器执行后提示:-bash: ./logger.py: usr/bin/python^M: bad interpre ...
- STL_map的使用
转自:http://www.kuqin.com/cpluspluslib/20071231/3265.html Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在m ...