ZOJ 1654 二分匹配基础题
题意: 给你一副图, 有草地(*),空地(o)和墙(#),空地上可以放机器人, 机器人向上下左右4个方向开枪(枪不能穿墙),问你在所有机器人都不相互攻击的情况下能放的最多的机器人数。
思路:这是一类经典题的衍化,如果没有墙,我们会将行和列看成两列点阵,然后就可以用二分匹配解。
现在有墙怎么办呢, 把某一行或列(有墙的拆分成多个区域,可以看成多个行或列), 拆好以后更没有墙的做法一样了。
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn = 1505;
vector <int> edge[maxn]; //记录以左排点为起点的单向边
int pre[maxn]; //右点阵的大小
bool vis[maxn]; //右点阵的大小
int n, m;
bool dfs(int u) {
int i, v;
for(i = 0; i < (int)edge[u].size(); i++) {
v = edge[u][i];
if(vis[v])
continue;
vis[v] = 1;
if(pre[v] == -1 || dfs(pre[v])) {
pre[v] = u;
return 1;
}
}
return 0;
}
char mp[51][51];
int num[51][51]; int nx, ny, x[maxn][maxn], y[maxn][maxn];
int main() {
int i, j, cas, ca = 1;
scanf("%d", &cas);
while(cas--) {
scanf("%d%d", &n, &m);
for(i = 0; i < n; i++)
scanf("%s", mp[i]);
memset(x, -1, sizeof(x));
nx = 0;
for(i = 0; i < n; i++) {
for(j = 0; j < m; j++)
if(mp[i][j] == 'o') x[i][j]= nx;
else if(mp[i][j] == '#') nx++;
nx++;
}
memset(y, -1, sizeof(y));
ny = 0;
for(j = 0; j < m; j++) {
for(i = 0; i < n; i++)
if(mp[i][j] == 'o') y[i][j] = ny;
else if(mp[i][j] == '#') ny++;
ny++;
}
for(i = 0; i < nx; i++) edge[i].clear(); for(i = 0; i < n; i++)
for(j = 0; j < m; j++)
if(mp[i][j] == 'o')
edge[x[i][j]].push_back(y[i][j]);
memset(pre, -1, sizeof(int)*ny);
//建边
int cnt = 0;
for(i = 0; i < nx; i++) {
memset(vis, 0, sizeof(int)*ny);
if(dfs(i)) cnt++;
}
printf("Case :%d\n%d\n", ca++, cnt);
}
return 0;
}
ZOJ 1654 二分匹配基础题的更多相关文章
- UVALive 6525 Attacking rooks 二分匹配 经典题
题目链接:option=com_onlinejudge&Itemid=8&page=show_problem&problem=4536">点击打开链接 题意: ...
- nyoj_239:月老的难题@_@(二分图匹配基础题)
题目链接 放假回家不知道多少人被父母催着去相亲啊hhhhhhhhhhhhhh @_@ 参考:二分图的最大匹配.完美匹配和匈牙利算法 #include<bits/stdc++.h> usin ...
- POJ 1469 ZOJ1140 二分匹配裸题
很裸,左点阵n,右点阵m 问最大匹配是否为n #include <cstdio> #include <cstring> #include <vector> usin ...
- COURSES 赤裸裸的二分匹配大水题
COURSES 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include ...
- poj2239 poj1274【二分匹配】
题意: 就是尽可能的选多的课 思路: 把课程和上课的时间看作二分图 跑一跑二分匹配就好了 #include<iostream> #include<cstdio> #includ ...
- UVA5874 Social Holidaying 二分匹配
二分匹配简单题,看懂题意,建图比较重要. #include<stdio.h> #include<string.h> #define maxn 1100 int map[maxn ...
- hdu 1068 Girls and Boys (二分匹配)
Girls and Boys Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- zoj 1002 Fire Net (二分匹配)
Fire Net Time Limit: 2 Seconds Memory Limit: 65536 KB Suppose that we have a square city with s ...
- zoj 2362 Beloved Sons【二分匹配】
题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2361 来源:http://acm.hust.edu.cn/vjudg ...
随机推荐
- H3C HCSE 官方培训胶片(中文) 下载
H3C HCSE 官方培训胶片(中文) 点击文件名下载 HM-040 OSPF路由协议(V5.1).ppt HM-041 BGP协议原理及配置(V5.0).ppt HM-041 BGP协议原理及配置( ...
- xcode APP 打包以及提交apple审核详细流程(新版本更新提交审核)
链接地址:http://blog.csdn.net/mad1989/article/details/8167529 打包发布APP流程真机测试和APP发布流程APP提交审核流程真机测试打包发布上传出错 ...
- 基于 SSH 的工具叫 sshfs. sshfs 可以让你在本地直接挂载远程主机的文件系统
另外一个很赞的基于 SSH 的工具叫 sshfs. sshfs 可以让你在本地直接挂载远程主机的文件系统. $ sshfs -o idmap=user user@hostname:/home/user ...
- AngularJS_百度百科
AngularJS_百度百科 AngularJS 编辑 AngularJS是为克服HTML在构建应用上的不足而设计的. 目录 1简介引引 端对 ...
- sqlserver bak还原
一.查看: restore filelistonly from disk='F:\Db\A_backup.bak' 二.还原:RESTORE DATABASE AFROM DISK = 'F:\Db\ ...
- Qt生成灰度图(转载)
Qt生成灰度图(转载) 项目中用到大量基础图像处理知识,其中灰度图的生成是很重要的一环. 先补充一些基础知识: ------------------------------------------ ...
- 极限挑战—C#100万条数据导入SQL SERVER数据库仅用4秒 (附源码)
原文:极限挑战-C#100万条数据导入SQL SERVER数据库仅用4秒 (附源码) 实际工作中有时候需要把大量数据导入数据库,然后用于各种程序计算,本实验将使用5中方法完成这个过程,并详细记录各种方 ...
- poj 3778
这就是个超级水题……!!!!写一写来纪念一下自己的错误…… 如果某个学生的的成绩是其他俩个或三个学生成绩的和则给予奖励 直接暴力,所以一开始直接用数组标记两个人或三个人的和,但是忽略了这种情况 20( ...
- Windows8 Metro快捷键 | Win8迷
Windows8 Metro快捷键 | Win8迷 Win + Q : 打开 搜索面板 Win + C : 打开屏幕右侧的Charms简化菜单 Win + 空格 : 切换输入语言和键盘布局
- Possible concurrency problem: Replicated version id X matches in-memory version for session ...
The message basically is saying that a replicated session is overriding an existing session in that ...