hdu 1045 Fire Net(最小覆盖点+构图(缩点))
http://acm.hdu.edu.cn/showproblem.php?pid=1045
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Description
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
Output
Sample Input
.X..
....
XX..
....
2
XX
.X
3
.X.
X.X
.X.
3
...
.XX
.XX
4
....
....
....
....
0
Sample Output
1
5
2
4
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#define N 310 using namespace std; char maps[N][N];
int G[N][N], vis[N], used[N];
int n, x, y, a[N][N], b[N][N]; bool Find(int u)
{
int i;
for(i = ; i <= y ; i++)
{
if(!vis[i] && G[u][i])
{
vis[i] = ;
if(!used[i] || Find(used[i]))
{
used[i] = u;
return true;
}
}
}
return false;
} void Build()
{
int i, j;
x = y = ;
memset(a, , sizeof(a));
memset(b, , sizeof(b));
for(i = ; i <= n ; i++)
{
for(j = ; j <= n ; j++)
{
if(maps[i][j] == '.')
{
if(maps[i][j - ] == '.')
a[i][j] = a[i][j - ];
else
a[i][j] = ++x;
}
}
}
for(i = ; i <= n ; i++)
{
for(j = ; j <= n ; j++)
{
if(maps[i][j] == '.')
{
if(maps[i - ][j] == '.')
b[i][j] = b[i - ][j];
else
b[i][j] = ++y;
G[a[i][j]][b[i][j]] = ;
}
}
} } int main()
{
int i, j, ans;
while(scanf("%d", &n), n)
{
ans = ;
memset(G, , sizeof(G));
for(i = ; i <= n ; i++)
{
getchar();
for(j = ; j <= n ; j++)
scanf("%c", &maps[i][j]);
}
Build();
memset(used, , sizeof(used));
for(i = ; i <= x ; i++)
{
memset(vis, , sizeof(vis));
if(Find(i))
ans++;
}
printf("%d\n", ans);
}
return ;
}
hdu 1045 Fire Net(最小覆盖点+构图(缩点))的更多相关文章
- HDU 1045 Fire Net(行列匹配变形+缩点建图)
题意:n*n的棋盘上放置房子.同一方同一列不能有两个,除非他们之间被墙隔开,这种话. 把原始图分别按行和列缩点 建图:横竖分区.先看每一列.同一列相连的空地同一时候看成一个点,显然这种区域不可以同一时 ...
- 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——————【最大匹配、构图、邻接矩阵做法】
Fire Net Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Sta ...
- HDU 1045(Fire Net)题解
以防万一,题目原文和链接均附在文末.那么先是题目分析: [一句话题意] 给定大小的棋盘中部分格子存在可以阻止互相攻击的墙,问棋盘中可以放置最多多少个可以横纵攻击炮塔. [题目分析] 这题本来在搜索专题 ...
- 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(dfs,跟8皇后问题很相似)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit: 2000/1000 MS (Java/Others) ...
- 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 二分图建图
HDU 1045 题意: 在一个n*n地图中,有许多可以挡住子弹的墙,问最多可以放几个炮台,使得炮台不会相互损害.炮台会向四面发射子弹. 思路: 把行列分开做,先处理行,把同一行中相互联通的点缩成一个 ...
- hdu 1045 Fire Net(二分图)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1045 题目大意为给定一个最大为4*4的棋盘,棋盘可以放置堡垒,处在同一行或者同一列的堡垒可以相互攻击, ...
随机推荐
- linux嵌入式系统驱动程序的阻塞与异步
对于那些需要进程独占的设备,需要使用linux提供的阻塞编程.步骤如下: 1.在设备驱动程序中定义该设备的进程等待列多,并将其初始化 static wait_queue_head_t wait_que ...
- [再做01背包] POJ 3624 Charm Bracelet
接触动态规划的第一题是数塔问题,第二题就是01背包问题了. 当时看的懵懵懂懂,回过头来再看这道题还是非常简单的了. 用 dp[i][j] 表示取前i种物品,使它们总体积不超过j的最优取法取得的价值总和 ...
- get 与post 的接收传值方式
1.get方法接收值写法 string ad = Request["name"]; 2.post方法接收值写法 <%string a = Request.Form[" ...
- 计算机视觉入门 Intorduction To Computer Vision
本文将主要介绍图像分类问题,即给定一张图片,我们来给这张图片打一个标签,标签来自于预先设定的集合,比如{people,cat,dog...}等,这是CV的核心问题,图像分类在实际应用中也有许多变形,而 ...
- 摘录:官方文档对ROWID虚拟行的定义
ROWID Pseudocolumn For each row in the database, the ROWID pseudocolumn returns the address of the r ...
- 深入学习Heritrix---解析处理器(Processor)(转)
深入学习Heritrix---解析处理器(Processor) 本节解析与处理器有关的内容. 与处理器有关的主要在以下几个类:Processor(处理器类),ProcessorChain(处理器类), ...
- 【转】【iOS系列】-iOS查看沙盒文件图文教程(真机+模拟器)
原文网址:http://www.cnblogs.com/fengtengfei/p/5090276.html 1:模拟器 1.1 方法1: 程序中打印一下的地址,能直接前往沙盒路径. NSString ...
- Hbase常用命令
转:http://lib.csdn.net/article/hadoop/33499
- C++宏定义详解
一.#define的基本用法 #define是C语言中提供的宏定义命令,其主要目的是为程序员在编程时提供一定的方便,并能在一定程度上提高程序的运行效率,但学生在学习时往往不能 理解该命令的本质 ...
- 基于CentOS与VmwareStation10搭建Oracle11G RAC 64集群环境:4.安装Oracle RAC FAQ-4.2.Oracleasm Createdisk ASM磁盘失败:Instantiating disk: failed
1.错误信息:Instantiating disk: failed [root@linuxrac1 /]# /usr/sbin/oracleasm createdisk OCR_VOTE /dev/s ...