HDU 1045 Fire Net 【连通块的压缩 二分图匹配】
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1045
Fire Net
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 15715 Accepted Submission(s): 9519
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的图, 在上面放炮车,要求炮车不能在同一行或者同一列(除非中间有阻碍物),求最多能放多少炮车。
解题思路:
按照行和列,把会冲突的点压缩成一个点,对压缩后的 行和列的点 进行二分图匹配。
AC code:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#define INF 0x3f3f3f3f
using namespace std;
const int MAXN = ;
char str[MAXN][MAXN];
int g[MAXN][MAXN];
int linker[MAXN];
bool used[MAXN];
int xx[MAXN][MAXN], yy[MAXN][MAXN];
int uN, vN; bool Find(int x)
{
for(int i = ; i <= vN; i++){
if(!used[i] && g[x][i]){
used[i] = true;
if(linker[i] == - || Find(linker[i])){
linker[i] = x;
return true;
}
}
}
return false;
} int match()
{
int ans = ;
memset(linker, -, sizeof(linker));
for(int i = ; i <= uN; i++){
memset(used, , sizeof(used));
if(Find(i)) ans++;
}
return ans;
} int main()
{
int k, row, col;
while(~scanf("%d", &k) && k){
for(int i = ; i < k; i++){
scanf("%s", str[i]);
}
memset(xx, , sizeof(xx));
memset(yy, , sizeof(yy));
memset(g, , sizeof(g));
row = col = ;
for(int i = ; i < k; i++){ //压缩连通块
for(int j = ; j < k; j++){
if(str[i][j] == '.'){
if(j == || str[i][j-] == 'X') row++;
xx[i][j] = row;
} if(str[j][i] == '.'){
if(j == || str[j-][i] == 'X') col++;
yy[j][i] = col;
}
}
}
for(int i = ; i < k; i++){
for(int j = ; j < k; j++){
if(str[i][j] == '.')
g[xx[i][j]][yy[i][j]] = ;
}
}
vN = col, uN = row;
printf("%d\n", match());
}
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 【二分图匹配】
<题目链接> 题目大意: 这题意思是给出一张图,图中'X'表示wall,'.'表示空地,可以放置炮台,同一条直线上只能有一个炮台,除非有'X'隔开,问在给出的图中最多能放置多少个炮台. 解 ...
- 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)题解
以防万一,题目原文和链接均附在文末.那么先是题目分析: [一句话题意] 给定大小的棋盘中部分格子存在可以阻止互相攻击的墙,问棋盘中可以放置最多多少个可以横纵攻击炮塔. [题目分析] 这题本来在搜索专题 ...
- HDU 1045 Fire Net(dfs,跟8皇后问题很相似)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 1045——Fire Net——————【最大匹配、构图、邻接矩阵做法】
Fire Net Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Sta ...
- 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(搜索剪枝)
http://acm.split.hdu.edu.cn/showproblem.php?pid=1045 http://acm.hdu.edu.cn/showproblem.php?pid=1045 ...
- HDU 1045 Fire Net 二分图建图
HDU 1045 题意: 在一个n*n地图中,有许多可以挡住子弹的墙,问最多可以放几个炮台,使得炮台不会相互损害.炮台会向四面发射子弹. 思路: 把行列分开做,先处理行,把同一行中相互联通的点缩成一个 ...
随机推荐
- VUE-CLI 设置页面title
router > index.js { path: '/worklist', name: 'worklist', component: worklist, meta: {title:'维修工列表 ...
- VUE 日历签到
<style lang="scss"> @import "../assets/css/px2rem.scss"; .sign-box { width ...
- git 拉新项目
- 页面跳转问题-button 确定提交按钮
form和ajax不可一起用了,button标签默认是用的form表单,所以导致跳转有问题,form不能和ajax一起用的,切记
- URL篇之URL
URL(统一资源定位)是网络上使用的资源定位的方案,它是URI(由URL和URN组成)的子集. URL的通用格式 <scheme>://<user>:<password& ...
- journalctl 中文手册
版权声明 本文译者是一位开源理念的坚定支持者,所以本文虽然不是软件,但是遵照开源的精神发布. 无担保:本文译者不保证译文内容准确无误,亦不承担任何由于使用此文档所导致的损失. 自由使用:任何人都可以自 ...
- Beam编程系列之Apache Beam WordCount Examples(MinimalWordCount example、WordCount example、Debugging WordCount example、WindowedWordCount example)(官网的推荐步骤)
不多说,直接上干货! https://beam.apache.org/get-started/wordcount-example/ 来自官网的: The WordCount examples demo ...
- [转]ASP.NET Web API系列教程(目录)
本文转自:http://www.cnblogs.com/r01cn/archive/2012/11/11/2765432.html 注:微软随ASP.NET MVC 4一起还发布了一个框架,叫做ASP ...
- dubbo-admin网页管理控制台
由于近段时间在看dubbo,网上找到的这个war包发布到tomcat报错,故从git(https://github.com/apache/incubator-dubbo-ops)上重新下载编译了版本 ...
- 一个基于Ionic3.x cordova的移动APP demo
项目地址如遇网络不佳,请移步国内镜像加速节点 前端技术: Angular4.x + ionic3.x + cordova 项目运行: git clone git@github.com:EasyTuan ...