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地图中,有许多可以挡住子弹的墙,问最多可以放几个炮台,使得炮台不会相互损害.炮台会向四面发射子弹. 思路: 把行列分开做,先处理行,把同一行中相互联通的点缩成一个 ...
随机推荐
- layui table合计但是未计算的解决
在项目里table开启合计功能,但是并未进行数据计算,后来发现是field写错了的问题,上代码 for(var i = 0; i < that.checkboxAll.data.length; ...
- C# 清空数组Array.Clear
using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public ...
- 从指定Dictionary中移除指定值项
void Removeltems(Dictionary<int, ltem> _dicltemMap, ltem _item) { List<ltem> keys=new Li ...
- Ubuntu安装Docker步骤
环境:Ubuntu Trusty 14.04 (LTS) 前提条件: Docker requires a 64-bit installation regardless of your Ubuntu v ...
- webservice 注解介绍
JAX-WS 注释 “基于 XML 的 Web Service 的 Java API”(JAX-WS)通过使用注释来指定与 Web Service 实现相关联的元数据以及简化 Web Service ...
- 023-将表单序列化为json对象
使用jQuery将表单序列化为json对象,其中serializeJson方法的名字任意,serializeArray()这个jQuery提供的方法.this指的就是谁调用了这个方法. $.fn.se ...
- TCP-Java--图谱
- 迟到的UED(转发)
2013UCAN用户体验设计论坛–精彩视频赏析 - 交互设计 博文 视觉设计 | TaoBaoUEDhttp://ued.taobao.org/blog/2013/09/2013ucan/
- 4.net两种交互模式
.net两种交互模式 (1) C/S:客户端(Client)/服务器模式(Server) (2) B/S:浏览器(Browser)/服务器模式(Server) 来自为知笔记(Wiz)
- typeScript入门(一)构建环境和数据类型
最近入坑v-cli 3.0,发现ts越来越常用了,于是开始入坑学习. 1.构建ts环境 npm install -g typescript Mac和vscode用户可以用以下方式构建tsdemo项目 ...