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. vue-jwt 实战

    作用:把用户的信息储存到客户端 每次客户端带上token 校验是否登陆过 1.使用主要有两个api jwt.sign(payload, secretOrPrivateKey, [options, ca ...

  2. 接触oracle的第二天

    1.2 Sqlplus 这是一个轻量级的功能强大的客户端, 是 dba 必须掌握的工具. 我们可以配置 sqlplus 的一些行为,两个命令: show. 用来显示配置参数 set. 用来设置配置参数 ...

  3. ARM指令集的最新版本包括针对JavaScript的优化

    在ARM指令集中,ARMv8.3添加了一个新的float-to-int指令,其错误和超出范围的值按照JavaScript的方式处理.以前[指令]获取JavaScript的语义要慢得多,JavaScri ...

  4. 如何编译Java程序以及运行程序

    一.编辑运行 二.HelloWorld程序的组成: 三.public static void main(String[] args){} 四.System.out.println("Hell ...

  5. nuxt.js 封装axios

    1.安装axios cnpm install axios --save 2.在plugins文件夹下面创建service.js import axios from 'axios' import { M ...

  6. 阿里云产品家族再添新丁:视觉AI、CPFS一体机助力企业全面上云

    近日举行的2019阿里云广东峰会上,阿里云宣布推出面向混合云场景的CPFS一体机和视觉AI一体机,两款新品具备超高性能.开箱即用等特性,极大降低企业上云的周期和门槛. 加上此前推出的POLARDB数据 ...

  7. MySQL两个时间相减

    SELECT TIMESTAMPDIFF(MONTH,'2009-10-01','2009-09-01'); interval可是: SECOND 秒 SECONDS MINUTE 分钟 MINUTE ...

  8. size_t是什么?

    在32位编译器下size_t可看做unsigned int: 在64位编译器下size_t可看做unsigned long long: sizeof返回的数据类型就为size_t. C++之size_ ...

  9. UIWebView 禁止检测链接弹出UIActionSheet

    解决方法一: 添加以下代码禁止检测类型 webView.dataDetectorTypes = UIDataDetectorTypeNone; 解决方法二: - (void)webViewDidFin ...

  10. windows下数据库备份bat

    @echo offset "Ymd=%date:~,4%%date:~5,2%%date:~8,2%"C:/mysql/bin/mysqldump --opt -u root -- ...