hdu-1045.fire net(缩点 + 二分匹配)
Fire Net
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 17669 Accepted Submission(s): 10749
A blockhouse is a small castle that has four openings through which to shoot. The four openings are facing North, East, South, and West, respectively. There will be one machine gun shooting through each opening.
Here we assume that a bullet is so powerful that it can run across any distance and destroy a blockhouse on its way. On the other hand, a wall is so strongly built that can stop the bullets.
The goal is to place as many blockhouses in a city as possible so that no two can destroy each other. A configuration of blockhouses is legal provided that no two blockhouses are on the same horizontal row or vertical column in a map unless there is at least one wall separating them. In this problem we will consider small square cities (at most 4x4) that contain walls through which bullets cannot run through.
The following image shows five pictures of the same board. The first picture is the empty board, the second and third pictures show legal configurations, and the fourth and fifth pictures show illegal configurations. For this board, the maximum number of blockhouses in a legal configuration is 5; the second picture shows one way to do it, but there are several other ways.

Your task is to write a program that, given a description of a map, calculates the maximum number of blockhouses that can be placed in the city in a legal configuration.
.X..
....
XX..
....
2
XX
.X
3
.X.
X.X
.X.
3
...
.XX
.XX
4
....
....
....
....
0
1
5
2
4
/*
本题大意:给出一个n * n的矩阵,让你在这个矩阵中放置尽可能多的炮台。
放置炮台的规则为,任意两个炮台都不在同一行同一列,除非他们之间有wall。 本题思路:这题很明显回溯可以做,但是下面我们用二分匹配讲解一下这个题,
很明显我们知道如果一个点被选取,那么和他在同一行或者同一列的中间不存
在wall的所有点都不能被选取,所以我们就可以考虑如何限制?我们可以把
这个图按照行缩点,再按照列缩点,那么我们就可以知道,对于缩点之后的行
标号i和列标号j,如果i -> j之间存在一条边,那就说明在方格(i, j)上放了
一个点,那么i行j列不能再放其它其它点,那么我们可以想到对得到的缩点进行
建立二分图,行和列分别为二部图的两部分,然后依据原图建边,寻找到最大匹配
即为答案。
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = + ;
bool used[maxn];//表示是否在交错路中
int linker[maxn];//存储匹配点
int g[maxn][maxn];//存缩点之后的图
char str[maxn][maxn];
int x[maxn][maxn], cntx;//行点集
int y[maxn][maxn], cnty;//列点集 bool dfs(int u) {
for(int v = ; v <= cnty; v ++) {
if(g[u][v] && !used[v]) {
used[v] = true;
if(linker[v] == - || dfs(linker[v])) {
linker[v] = u;
return true;
}
}
}
return false;
} int hungary() {
int res = ;
memset(linker, -, sizeof linker);
for(int u = ; u <= cntx; u ++) {
memset(used, false, sizeof used);
if(dfs(u)) res ++;
}
return res;
} int main() {
int n;
while(scanf("%d", &n) && n) {
memset(x, , sizeof x);
memset(y, , sizeof y);
memset(g, false, sizeof g);
for(int i = ; i < n; i ++) {
scanf("%s", str[i]);
}
cntx = ; //对行缩点
for(int i = ; i < n; i ++) {
for(int j = ; j < n; j ++) {
if(str[i][j] == '.') x[i][j] = cntx;
if(str[i][j] == 'X') cntx ++;
}
cntx ++;
} //对列缩点
cnty = ;
for(int j = ; j < n; j ++) {
for(int i = ; i < n; i ++) {
if(str[i][j] == '.') y[i][j] = cnty;
if(str[i][j] == 'X') cnty ++;
}
cnty ++;
}
for(int i = ; i < n; i ++) {
for(int j = ; j < n; j ++) {
if(str[i][j] == '.') g[x[i][j]][y[i][j]] = true;
}
}
printf("%d\n", hungary());
}
return ;
}
hdu-1045.fire net(缩点 + 二分匹配)的更多相关文章
- HDOJ(HDU).1045 Fire Net (DFS)
HDOJ(HDU).1045 Fire Net [从零开始DFS(7)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DFS HD ...
- HDU - 1045 Fire Net(二分匹配)
Description Suppose that we have a square city with straight streets. A map of a city is a square bo ...
- hdu 1045 Fire Net(二分匹配 or 暴搜)
Fire Net Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- hdu 1045 Fire Net(最小覆盖点+构图(缩点))
http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit:1000MS Memory Limit:32768KB ...
- HDU 1045 Fire Net 【连通块的压缩 二分图匹配】
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 1045 Fire Net 【二分图匹配】
<题目链接> 题目大意: 这题意思是给出一张图,图中'X'表示wall,'.'表示空地,可以放置炮台,同一条直线上只能有一个炮台,除非有'X'隔开,问在给出的图中最多能放置多少个炮台. 解 ...
- HDU 1045(Fire Net)题解
以防万一,题目原文和链接均附在文末.那么先是题目分析: [一句话题意] 给定大小的棋盘中部分格子存在可以阻止互相攻击的墙,问棋盘中可以放置最多多少个可以横纵攻击炮塔. [题目分析] 这题本来在搜索专题 ...
- HDU 1045 Fire Net 二分图建图
HDU 1045 题意: 在一个n*n地图中,有许多可以挡住子弹的墙,问最多可以放几个炮台,使得炮台不会相互损害.炮台会向四面发射子弹. 思路: 把行列分开做,先处理行,把同一行中相互联通的点缩成一个 ...
- hdu 1045:Fire Net(DFS经典题)
Fire Net Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
随机推荐
- GUI学习之十六——QSpinBox学习总结
我们在上一章讲了步长调节器QAbstractSpinBox,这一节来讲一下它的一个子类:QSpinBox 一.描述 QSpinBox是一个主要处理整数和离散值集合的步长调节器控件,它允许用户通过单击增 ...
- Linux shell 误操作
shell脚本在日常运维中是必不可少会应用到,下面是自己亲身经历过的一件事.会了定期清除日志,编写了一个shell脚本,内容如下: [root@centos- tmp]# more remote_lo ...
- window.location对象 获取页面地址
window.location对象的属性: 属性 含义 值 location.protocol 协议 "http://"或"https://" location ...
- toString和toLocalString
toLocalString()是调用每个数组元素的 toLocaleString() 方法,然后使用地区特定的分隔符把生成的字符串连接起来,形成一个字符串. toString()方法获取的是Strin ...
- 【hackerrank】Type of Triangle
题目如下: Write a query identifying the type of each record in the TRIANGLES table using its three side ...
- twint 安装及使用
分享这个post是自己方便查,还有中文网界对这个东西介绍太少. 更多的就看github项目twint吧. Installation: git+pip3: git clone https://githu ...
- Linux学习-NFS服务
一.NFS服务相关介绍 1.NFS简介 NFS (Network File System) 网络文件系统,基于内核的文件系统.Sun公司开发,通过使用NFS,用户和程序可以像访问本地文件一样访问远端系 ...
- 一些性能优化的tips
工作中积累的一些性能优化的tips,记录一下: 1. Message的创建 Message message = Message.obtain(); // 推荐 Message message = n ...
- Android 获取屏幕尺寸与密度(转载)
android中获取屏幕的长于宽,参考了网上有很多代码,但结果与实际不符,如我的手机是i9000,屏幕大小是480*800px,得到的结果却为320*533 结果很不靠谱,于是自己写了几行代码,亲测一 ...
- Http请求状态大全
一.HTTP状态码分类 HTTP状态码由三个十进制数字组成,第一个十进制数字定义了状态码的类型,后两个数字没有分类的作用.HTTP状态码共分为5种类型: 分类 分类描述 1** 信息 服务器收到请求, ...