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地图中,有许多可以挡住子弹的墙,问最多可以放几个炮台,使得炮台不会相互损害.炮台会向四面发射子弹. 思路: 把行列分开做,先处理行,把同一行中相互联通的点缩成一个 ...
随机推荐
- [转]ClassPath是什么
from: https://my.oschina.net/GivingOnenessDestiny/blog/603505 classpath 是什么classpath实际上就是编译后的 以 clas ...
- laravel框架的rabbitmq使用示例[多队列封装]
RabbitMQ是实现了高级消息队列协议(AMQP)的开源消息代理软件(亦称面向消息的中间件).RabbitMQ服务器是用Erlang语言编写的,而集群和故障转移是构建在开放电信平台框架上的.所有主要 ...
- elasticsearch fitler查询例子
- js实现CkeckBox全选与反选
全选与反选 function SelectAll(){ var check = document.getElementsByTagName("input"); // 获取所有inp ...
- 6、图标:icon
1.图标 /* ---html----*/ <ion-content text-center class="icons-basic-page"> <ion-row ...
- ASP.NET安全[开发ASP.NET MVC应用程序时值得注意的安全问题](转)
概述 安全在web领域是一个永远都不会过时的话题,今天我们就来看一看一些在开发ASP.NET MVC应用程序时一些值得我们注意的安全问题.本篇主要包括以下几个内容 : 认证 授权 XSS跨站脚本攻击 ...
- CSP学习之CryptoAPI初识
Crypto API目的就是提供开发者在windows下使用PKI的编程接口. Crypto 提供了很多的加解密相关函数,如编码.解码.加密解密,哈希,数字证书.证书管理证书存储等. 有关 ...
- tushare获取的数据与mysql数据库交互简单范例
#!/usr/bin/python2.7# -*- coding: UTF-8 -*- import tushare as tsimport pandas as pdfrom sqlalchemy i ...
- HIVE的sql语句操作
Hive 是基于Hadoop 构建的一套数据仓库分析系统,它提供了丰富的SQL查询方式来分析存储在hadoop 分布式文件系统中的数据,可以将结构 化的数据文件映射为一张数据库表,并提供完整的SQL查 ...
- C# 读写xml、excel、word、ppt、access
C# 读写xml.excel.word.access 这里只是起个头,不做深入展开,方便以后用到参考 读写xml,主要使用.net 的xml下的document using System;using ...