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 ...
随机推荐
- iOS 使用Block实现函数回调
事实上.iOS中的Block就是C++中的函数指针,实现方式都是一样的,以下贴出一个简单的实践. 首先,创建一个回调的类 BlockStudy.h // // BlockStudy.h // Bloc ...
- 在Android手机上获取其它应用的包名及版本
转载请注明出处:http://blog.csdn.net/jason_src/article/details/37757661 获取Android手机上其它应用的包名及版本方法有非常多,能够通过AAP ...
- 1数组的join方法
function log(e) { console.log(e) } 有时候写console.log太长了,所以会自己写个这样的函数省去写console的步骤. 数组的join方法可以把一个数组按照j ...
- 如何获得getElementById的length这个数值?
a=document.getElementById("a").innerHTML.length;我觉得你应该这么写 如果是文本框的话document.getElementById( ...
- git-daemon的快捷搭建
使用git-daemon进行git服务器搭建 1.安装git-daemon 前提是已经安装git sudo apt-get install git git-core 然后安装git-daemon su ...
- JENKINS 打包发布脚本
#!/bin/bash #nohup bash check_new_pkgs_dev.sh & #steps below: ##发布的机器上运行这个脚本 #定时遍历发布包存放路径 #1.遍历所 ...
- Windows phone 8 学习笔记
Windows phone 8 学习笔记(1) 触控输入 http://www.apkbus.com/android-138547-1-1.html Windows phone 8 学习笔记(2) ...
- 【JAVA学习】单例模式的七种写法
尊重版权:http://cantellow.iteye.com/blog/838473 第一种(懒汉.线程不安全): Java代码 public class Singleton { private ...
- FFMPEG H264/H265 编码延迟问题
最新使用FFmpeg进行H264的编码时,发现视频编码有延迟,不是实时编码.进过一番研究发现,仅仅要在调用avcodec_open2函数 打开编码器时,设置AVDictionary參数就可以.关键代码 ...
- Entity FramWork - 在VS里面直接创建表,并同步到数据库
前面具体添加什么直接看: 1.Entity - 使用EF框架进行增删改查 - 模型先行 2.Entity - 使用EF框架进行增删改查 - 数据库先行 然后: 然后右键,可以添加[实体],也就是表.之 ...