http://acm.hdu.edu.cn/showproblem.php?pid=1045

Fire Net

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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
这题二分匹配, 也可以直接用DFS搜索,这里上二分匹配的代码
 该题与poj2226做法相同,具体如何构图在本博客poj2226这道题的题解上讲解的很清楚,在这里就不再过多解释
链接如下:
#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(最小覆盖点+构图(缩点))的更多相关文章

  1. HDU 1045 Fire Net(行列匹配变形+缩点建图)

    题意:n*n的棋盘上放置房子.同一方同一列不能有两个,除非他们之间被墙隔开,这种话. 把原始图分别按行和列缩点 建图:横竖分区.先看每一列.同一列相连的空地同一时候看成一个点,显然这种区域不可以同一时 ...

  2. HDOJ(HDU).1045 Fire Net (DFS)

    HDOJ(HDU).1045 Fire Net [从零开始DFS(7)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DFS HD ...

  3. HDU 1045——Fire Net——————【最大匹配、构图、邻接矩阵做法】

    Fire Net Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Sta ...

  4. HDU 1045(Fire Net)题解

    以防万一,题目原文和链接均附在文末.那么先是题目分析: [一句话题意] 给定大小的棋盘中部分格子存在可以阻止互相攻击的墙,问棋盘中可以放置最多多少个可以横纵攻击炮塔. [题目分析] 这题本来在搜索专题 ...

  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(dfs,跟8皇后问题很相似)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit: 2000/1000 MS (Java/Others)   ...

  7. HDU 1045 Fire Net 状压暴力

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit: 2000/1000 MS (Java/Others)  ...

  8. HDU 1045 Fire Net 二分图建图

    HDU 1045 题意: 在一个n*n地图中,有许多可以挡住子弹的墙,问最多可以放几个炮台,使得炮台不会相互损害.炮台会向四面发射子弹. 思路: 把行列分开做,先处理行,把同一行中相互联通的点缩成一个 ...

  9. hdu 1045 Fire Net(二分图)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1045 题目大意为给定一个最大为4*4的棋盘,棋盘可以放置堡垒,处在同一行或者同一列的堡垒可以相互攻击, ...

随机推荐

  1. 51nod1161 Partial Sums

    开始想的是O(n2logk)的算法但是显然会tle.看了解题报告然后就打表找起规律来.嘛是组合数嘛.时间复杂度是O(nlogn+n2)的 #include<cstdio> #include ...

  2. 点滴积累【SQL Server】---SQL语句操作约束

    说明: --主键约束(Primary Key constraint):要求主键列的数据唯一,并且不允许为空. --唯一约束(Unique Constraint):要求该列唯一,允许为空,但只能出现一个 ...

  3. IE 火狐浏览器对时间格式的兼容性;使用原型对象的方式 prototype关键字;时间格式化

    在ie中 时间格式如果用横杠来显示  "2013-05-10 19:20:59" 是可以正确识别的(如果用斜杠,IE也可以正确识别), 但是如果是火狐,则只能识别斜杠模式 &quo ...

  4. C语言之复杂指针详解

    在<C陷阱与缺陷>第二章第一节中有这样一个声明: (*(void(*)())0)(): 看到这样的表达式估计让不少人都“不寒而栗”了吧,其实虽然看起来复杂,但是构造这类表达式其实只有一条简 ...

  5. unity3d 破解安装

    1.下载破解程序,执行生成unity_v4.x.ulf文件 2.断网 3.执行unity客户端,load该lisence文件即可 注意:安装unity客户端完成后,未破解,切记别打开unity客户端 

  6. 【原创】搭建Nginx(负载均衡)+Redis(Session共享)+Tomcat集群

    为什么移除首页?哪里不符合要求?倒是回我邮件啊! 一.环境搭建 Linux下Vagrant搭建Tomcat7.Java7 二.Nginx的安装配置与测试 *虚拟机下转至root sudo -i 1)下 ...

  7. jsonp实现跨域访问

    要实现JSONP跨域访问,首先就要了解什么是跨域?然后JSONP与JSON的关系? 1.什么是跨域? 跨域简单的说就是一个域名下的程序和另一个域名下的程序做数据交互.比如说:现有一个http://ww ...

  8. oracle 常用语句

    创建用户及授权create temporary tablespace test_temp tempfile 'C:\oracle\product\10.2.0\oradata\hszxdbtemp.d ...

  9. Android服务之Service

    android中服务是运行在后台的东西,级别与activity差不多.既然说service是运行在后台的服务,那么它就是不可见的,没有界面的东西.你可以启动一个服务Service来播放音乐,或者记录你 ...

  10. Dev gridControl z

    Dev gridControl 添加表标题 1.OptionsView ->ShowViewCaption = True 2.ViewCaption = "标题" Dev g ...