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 ...
随机推荐
- ListView 使用方法(Asp.Net)
您将须要用到的独有数据绑定控件. Fritz Onion 代码下载位置: ExtremeASPNET2008_03.exe (192 KB) Browse the Code Online 文件夹 L ...
- 程序启动报错:ORA-12505;PL/SQL却可以登录的解决方法
一.异常{ ORA-12505, TNS:listener does not currently know of SID given in connect descriptor The Connect ...
- sql: DUAL
FROM <<Oracle.Database.11g.SQL>> dual is a table that contains a single row. The followi ...
- net core VS goang web
asp.net core VS goang web[修正篇] 先前写过一篇文章:http://www.cnblogs.com/gengzhe/p/5557789.html,也是asp.net core ...
- js数组基础整理
首页: 主要整理了一下数组中常用的一些基础知识,代码都是自己手敲,有不对的地方希望能指出,目前只有4篇,后续会不断的增加这一板块. 由于少于100字不能发所以把一些最基本的创建数组也写上. // 创建 ...
- 关于Delphi中TRttiContext.FindType失效的问题
自从Delphi2010后,Delphi中的Rtti功能得到了增强.我们终于可以不用先RegisterClass,再GetClass获取类的信息了.而只是简单的通过TRttiContext.GetTy ...
- cocos2d-x游戏开发系列教程-坦克大战游戏关卡选择场景的编写下
上篇文章写了Paddle类来处理精灵的点击.触摸事件,现在我们在Paddle的基础上 写一个MyPaddle类,来处理上一关.下一关.开始游戏按钮的点击事件. 1.类声明如下: class MyPad ...
- 性能测试之LoardRunner 检查点
概述 1.检查点概念 2.实例 以下是详细介绍 检查点:首先来看一下VuGen确定脚本运行成功的判断条件.在录制编写脚本后,通常就会进行回放,如果回放通过没有错误,就认为脚本是正确的.究竟VuGen怎 ...
- OSI七层模型基础知识及各层常见应用
OSI Open Source Initiative(简称OSI,有译作开放源代码促进会.开放原始码组织)是一个旨在推动开源软件发展的非盈利组织.OSI参考模型(OSI/RM)的全称是开放系统互 ...
- LeetCode77:Combinations
Given two integers n and k, return all possible combinations of k numbers out of 1 - n. For example, ...