Fire Net

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 17669    Accepted Submission(s): 10749

Problem Description
Suppose that we have a square city with straight streets. A map of a city is a square board with n rows and n columns, each representing a street or a piece of wall.

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.

 
Input
The input file contains one or more map descriptions, followed by a line containing the number 0 that signals the end of the file. Each map description begins with a line containing a positive integer n that is the size of the city; n will be at most 4. The next n lines each describe one row of the map, with a '.' indicating an open space and an uppercase 'X' indicating a wall. There are no spaces in the input file. 
 
Output
For each test case, output one line containing the maximum number of blockhouses that can be placed in the city in a legal configuration.
 
Sample Input
4
.X..
....
XX..
....
2
XX
.X
3
.X.
X.X
.X.
3
...
.XX
.XX
4
....
....
....
....
0
 
Sample Output
5
1
5
2
4
 
Source
 
 
 
 /*
本题大意:给出一个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(缩点 + 二分匹配)的更多相关文章

  1. HDOJ(HDU).1045 Fire Net (DFS)

    HDOJ(HDU).1045 Fire Net [从零开始DFS(7)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DFS HD ...

  2. HDU - 1045 Fire Net(二分匹配)

    Description Suppose that we have a square city with straight streets. A map of a city is a square bo ...

  3. hdu 1045 Fire Net(二分匹配 or 暴搜)

    Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  4. hdu 1045 Fire Net(最小覆盖点+构图(缩点))

    http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit:1000MS     Memory Limit:32768KB   ...

  5. HDU 1045 Fire Net 【连通块的压缩 二分图匹配】

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit: 2000/1000 MS (Java/Others)    ...

  6. HDU 1045 Fire Net 【二分图匹配】

    <题目链接> 题目大意: 这题意思是给出一张图,图中'X'表示wall,'.'表示空地,可以放置炮台,同一条直线上只能有一个炮台,除非有'X'隔开,问在给出的图中最多能放置多少个炮台. 解 ...

  7. HDU 1045(Fire Net)题解

    以防万一,题目原文和链接均附在文末.那么先是题目分析: [一句话题意] 给定大小的棋盘中部分格子存在可以阻止互相攻击的墙,问棋盘中可以放置最多多少个可以横纵攻击炮塔. [题目分析] 这题本来在搜索专题 ...

  8. HDU 1045 Fire Net 二分图建图

    HDU 1045 题意: 在一个n*n地图中,有许多可以挡住子弹的墙,问最多可以放几个炮台,使得炮台不会相互损害.炮台会向四面发射子弹. 思路: 把行列分开做,先处理行,把同一行中相互联通的点缩成一个 ...

  9. hdu 1045:Fire Net(DFS经典题)

    Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

随机推荐

  1. pip install mysql_python报错解决办法

    首先请注意,mysql_python只支持Python2,所以假如你是python3,就直接用python-connector去吧.下面这一条命令就可以了 pip install mysql-conn ...

  2. CH0805 防线 (二分值域,前缀和,特殊性质)

    $ CH~0805~ $ 防线 (二分值域,前缀和,特殊性质) $ solution: $ 注意博主所给题面的输出和原题有些不同 这道题当时想了很久很久,就是想不到怎么写.果然还是太 $ vegeta ...

  3. <el-menu>菜单标签(里面可以包括:<el-submenu>和<el-menu-item>)

    <el-menu> 1.router属性,若使用router属性menu-item的index将对应router的path属性 2.mode,下拉菜单的模式分为horizontal和ver ...

  4. MySQL数据库1初识MySQL

    目录 Mysql 一.数据库是什么? 二.为啥使用数据库?(*****) 三.数据库的分类(*****) 1.关系型数据库 2.非关系型数据库 3.关系型与非关系型区别: 四.数据库MySQL的架构 ...

  5. python+requests接口自动化框架

    为什么要做接口自动化框架 1.业务与配置的分离 2.数据与程序的分离:数据的变更不影响程序 3.有日志功能,实现无人值守 4.自动发送测试报告 5.不懂编程的测试人员也可以进行测试 正常接口测试的流程 ...

  6. CSS定位机制之浮动定位float

    一.浮动定位实现的效果 二.使用float实现浮动定位 三.使用clear属性清除浮动定位 四.浮动定位的应用(布局) 一.浮动定位实现的效果   (一).块元素(div)在文档流中默认垂直排列,如果 ...

  7. Python Flask框架入门

    序言 Flask封装功能不及Django完善,性能不及Tornado,但是Flask的第三方开源组件比丰富. 如果你是一个追求极简风格的完美主义者,那么Flask适合你. 资料

  8. BFC、IFC、GFC和FFC

    基本概念 Box 是 CSS 布局的对象和基本单位, 直观点来说,就是一个页面是由很多个Box 组成的.元素的类型和 display 属性,决定了这个 Box 的类型. 不同类型的 Box, 会参与不 ...

  9. codeforces D Salary Changing

    题意:给你n个人,和s块钱,每个人都有一个工资区间,你给所有人都发工资.然后要他们工资的中位数最大. 思路:二分找那个值.那个值要满足至少有n/2+1个工资区间内. #include<cstdi ...

  10. 通过java反射机制,修改年龄字段的值

    需求:将生日转为年龄 /** * 获取年龄值 */ public List getAgeInfo(List list) throws Exception { if (null == list || l ...