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 ...
随机推荐
- impala常用语法
参考:https://www.w3cschool.cn/impala/impala_alter_table.html
- LOJ 6192 城市网络(树上倍增)
LOJ #6192. 「美团 CodeM 复赛」城市网络(链接) 一棵以 $ 1 $ 号节点为根的树,每个点有一个权值,有 $ q $ 个询问,每次从 $ x $ 点开始往某个祖先 $ y $ 走,初 ...
- webstorm注册码,亲测2016.1.1版
打开webstorm,点击帮助,注册 注册时,在打开的License Activation窗口中选择“License server”,在输入框输入下面的网址: http://idea.iteblog. ...
- [转]SQLServer : EXEC和sp_executesql的区别
MSSQL为我们提供了两种动态执行SQL语句的命令,分别是EXEC和sp_executesql.通常,sp_executesql则更具有优势,它提供了输入输出接口,而EXEC没有.还有一个最大的好处就 ...
- shell时间转换脚本
字符串转换为时间戳: time2utc #!/bin/sh Time=$ date -d "${Time}" '+%s' 时间戳转日期字符串 utc2time #!/bin/sh ...
- 拆系数$FFT$($4$遍$DFT$)
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> ...
- D. White Lines
D. White Lines 给定一个$n\times n$的$WB$矩阵,给定一个$k*k$的能把$B$变成$W$的橡皮擦,求橡皮擦作用一次后,全为$W$的行.列总数最大值 前缀和差分 #inclu ...
- UVALive 6859 Points (凸包)
Points 题目链接: http://acm.hust.edu.cn/vjudge/contest/130303#problem/E Description http://7xjob4.com1.z ...
- mui初级入门教程(七)— 基于native.js的文件系统管理功能实现
文章来源:小青年原创发布时间:2016-08-01关键词:mui,nativejs,android转载需标注本文原始地址: http://zhaomenghuan.github.io... 前言 这段 ...
- tp5关联模型进行条件查询
public function wordOne(){ return $this->hasOne('TeachWord','id','w_id')->field('id,pid,title' ...