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

.X..
....
XX..
.... XX
.X .X.
X.X
.X. ...
.XX
.XX ....
....
....
.... Sample Output

题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1045

********************************************

题意:n乘n 的矩阵,一般同一行同一列不能放两个'O',如果有'X'分割开来则可以,问你在'.'处最多可以放多少个'O'。

分析:二分匹配。

AC代码:

 #include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<algorithm>
#include<time.h>
#include<stack>
#include<vector>
using namespace std;
#define N 1200
#define INF 0x3f3f3f3f int vv[N],x,y,n;
char maps[][];
int vis[N][N],v[N]; struct node
{
int x,y;
} a[N][N]; int Hungary(int u)///匈牙利算法匹配
{
for(int i=; i<=y; i++)
if(vis[u][i]&&!v[i])
{
v[i]=;
if(!vv[i]||Hungary(vv[i]))
{
vv[i]=u;
return ;
}
} return ;
} int main()
{
int i,j; while(scanf("%d", &n),n)
{
for(i=; i<n; i++)
scanf("%s", maps[i]); x=,y=;
memset(vis,,sizeof(vis)); for(i=; i<n; i++)
for(j=; j<n; j++)
{
///把图分割,以相连的‘.’为行和列重新分配编号
if(maps[i][j]=='.')
{
if(j==||maps[i][j-]=='X')
x++;
a[i][j].x=x;
}
if(maps[j][i]=='.')
{
if(j==||maps[j-][i]=='X')
y++;
a[j][i].y=y;
}
} for(i=; i<n; i++)
for(j=; j<n; j++)
if(maps[i][j]=='.')
{
int u=a[i][j].x;
int v=a[i][j].y;
vis[u][v]=;///用行匹配列
} int ans=;
memset(vv,,sizeof(vv));
for(i=; i<=x; i++)
{
memset(v,,sizeof(v));
if(Hungary(i)==)
ans++;
} printf("%d\n", ans);
}
return ;
}

HDU - 1045 Fire Net(二分匹配)的更多相关文章

  1. hdu 1045 Fire Net(二分匹配 or 暴搜)

    Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  2. hdu 1045 Fire Net 二分图匹配 && HDU-1281-棋盘游戏

    题意:任意两个个'车'不能出现在同一行或同一列,当然如果他们中间有墙的话那就没有什么事,问最多能放多少个'车' 代码+注释: 1 //二分图最大匹配问题 2 //难点在建图方面,如果这个图里面一道墙也 ...

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

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

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

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

  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 【二分图匹配】

    <题目链接> 题目大意: 这题意思是给出一张图,图中'X'表示wall,'.'表示空地,可以放置炮台,同一条直线上只能有一个炮台,除非有'X'隔开,问在给出的图中最多能放置多少个炮台. 解 ...

  7. hdu 1045 Fire Net(最小覆盖点+构图(缩点))

    http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit:1000MS     Memory Limit:32768KB   ...

  8. HDU 1045(Fire Net)题解

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

  9. hdu 1045:Fire Net(DFS经典题)

    Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

随机推荐

  1. 使用jquery.validate.js插件进行表单里控件的验证

    jsp中具体实现的代码: <%@ page language="java" contentType="text/html; charset=UTF-8" ...

  2. mysql bit类型数据库中无法显示

    表an_bit bit类型字段id select bin(id+0) from an_bit显示二进制 select id+0 from an_bit显示十进制 http://blog.csdn.ne ...

  3. 定时发布任务,在global.asax中获取文件的物理路径的方法

    如果要把一个相对路径或者虚拟路径映射道服务器的物理路径,通常会使用Server.MapPath()函数,比如将根目录下的html目录映射为物理路径:Server.MapPath("html& ...

  4. Masonry的一些等间距布局

    控件之间的间距相等,但是控件的宽度是不定的. 下列的代码:定义间距为10,yellowview的宽度是由redView的宽度计算出来的. UIView *redView = [[UIView allo ...

  5. mysql无法输入中文

    #查看系统的环境变量 echo $LANG zh_CN.UTF-8 vim /etc/sysconfig/i18n 1 LANG="en_US.UTF-8"2 SYSFONT=&q ...

  6. vs2010帮助文档下载以及帮助查看器(H3Viewer)的使用

    在工作中遇到想查看vs2010的帮助文档.推荐使用H3Viewer.一个第三方的免费软件,独立于VS2010运行的帮助查看器.这方面的资料并不多.把本次自己使用的心得分享给大家. H3Viewer官方 ...

  7. net之session漫谈及分布式session解决方案

    最近一直在纠结net下分布式会话的实现,现将近日来的个人感想记录如下,如果有什么更好的解决方案请指教. 1.什么是session: Session 对象存储特定用户会话所需的属性及配置信息.这样,当用 ...

  8. Android:关于Edittext的一些设置

    1.自动弹出输入框. et_order_search.setFocusableInTouchMode(true); et_order_search.requestFocus(); CmzBossApp ...

  9. mybatis配置sql超时时间

    mybatis如果不配置,默认超时时间是不做限制的.当系统慢sql很多时,势必会增加数据库压力,系统性能及稳定性降低.所以有必要要设置sql超时设置,下面配置超时时间是5分钟. 第一步:全局配置如下 ...

  10. css3实现进度条

    HTML 结构很简单,但不是 Single Element: <div class="spinner"><i></i></div> ...