题意: 给你一副图, 有草地(*),空地(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 二分匹配基础题的更多相关文章

  1. UVALive 6525 Attacking rooks 二分匹配 经典题

    题目链接:option=com_onlinejudge&Itemid=8&page=show_problem&problem=4536">点击打开链接 题意: ...

  2. nyoj_239:月老的难题@_@(二分图匹配基础题)

    题目链接 放假回家不知道多少人被父母催着去相亲啊hhhhhhhhhhhhhh @_@ 参考:二分图的最大匹配.完美匹配和匈牙利算法 #include<bits/stdc++.h> usin ...

  3. POJ 1469 ZOJ1140 二分匹配裸题

    很裸,左点阵n,右点阵m 问最大匹配是否为n #include <cstdio> #include <cstring> #include <vector> usin ...

  4. COURSES 赤裸裸的二分匹配大水题

    COURSES 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include ...

  5. poj2239 poj1274【二分匹配】

    题意: 就是尽可能的选多的课 思路: 把课程和上课的时间看作二分图 跑一跑二分匹配就好了 #include<iostream> #include<cstdio> #includ ...

  6. UVA5874 Social Holidaying 二分匹配

    二分匹配简单题,看懂题意,建图比较重要. #include<stdio.h> #include<string.h> #define maxn 1100 int map[maxn ...

  7. hdu 1068 Girls and Boys (二分匹配)

    Girls and Boys Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  8. zoj 1002 Fire Net (二分匹配)

    Fire Net Time Limit: 2 Seconds      Memory Limit: 65536 KB Suppose that we have a square city with s ...

  9. zoj 2362 Beloved Sons【二分匹配】

    题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2361 来源:http://acm.hust.edu.cn/vjudg ...

随机推荐

  1. 圣魔大战3(Castle Fantisia)艾伦希亚战记完美攻略

    作为城堡幻想曲系列续作,艾伦希亚战记继承了前作的战棋+养成模式进行游戏. (城堡幻想曲3,纠正大家个错误哦,不是圣魔大战3,圣魔大战是城堡幻想曲2,圣魔大战不是个系列,艾伦西亚战记==艾伦希亚战记,一 ...

  2. POJ 2031 prim

    Building a Space Station Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 4400 Accepted: 2 ...

  3. BZOJ 1264: [AHOI2006]基因匹配Match( LCS )

    序列最大长度2w * 5 = 10w, O(n²)的LCS会T.. LCS 只有当a[i] == b[j]时, 才能更新答案, 我们可以记录n个数在第一个序列中出现的5个位置, 然后从左往右扫第二个序 ...

  4. 基于visual Studio2013解决面试题之1309求子集

     题目

  5. Juuluu 旗下企业站点管理系统3.0.1公布!

    KgE金刚企业站点管理系统是广州聚龙软件为国内中小企业开发的一款企业站点管理系统,KgE採用可视化的标签模型,可在Dreamvear等网页编辑下可视化编辑,KgE使用javaeemsyqlyuijqu ...

  6. Google的Java经常使用类库 Guava

    Guava 中文是石榴的意思,该项目是 Google 的一个开源项目,包括很多 Google 核心的 Java 经常使用库. 1. 基本工具 [Basic utilities]     让使用Java ...

  7. 基于visual Studio2013解决C语言竞赛题之1079狼羊过河

        题目 解决代码及点评 /************************************************************************/ /* ...

  8. 浅析嵌入式Linux系统的构成和启动过程

    在我们的周围,大量的嵌入式设备都是基于Linux系统来构建的,嵌入式Linux与主机Linux相比有着自己的一些特点,本文就嵌入式Linux系统的构成和启动过程做一些总结. 一.嵌入式Linux系统构 ...

  9. sed 技巧一例:特定位置插入

    通过一例子熟悉 sed 的运用 下面命令是在修改 ~/fs/install/nzos.conf 文件, 并在 env 第一次出现的地方再添加一行 env LXC_EXTRA_PORT=5556 sed ...

  10. Oracle 经常使用的改动语句

    一.表Table 測试表:MY_TEST_TABLE -- Create table create table MY_TEST_TABLE ( A VARCHAR2(30), B NUMBER(10) ...