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. Java作用域

    1. java访问控制修饰符 Java中,可以使用访问控制符来保护对类.变量.方法和构造方法的访问.Java支持4种不同的访问权限. 默认的,也称为 default,在同一包内可见,不使用任何修饰符. ...

  2. postman+jenkins+newman做接口测试的持续集成

    为何要做接口自动化测试的持续集成? 1. 接口相对稳定,改动少,比起GUI自动化测试来说性价比更加高些,不容易出现GUI自动化那种掉到维护脚本的坑里. 2. 接口测试比较简单,一个规范的接口,测试只需 ...

  3. 【Python之路】第二篇--初识Python

    Python简介 Python可以应用于众多领域,如:数据分析.组件集成.网络服务.图像处理.数值计算和科学计算等众多领域.目前业内几乎所有大中型互联网企业都在使用Python,如:Youtube.D ...

  4. perl LWP::UserAgent获取源码与响应

    #!/usr/bin/perl -w use strict; use LWP::UserAgent; my $useragent = new LWP::UserAgent; my $url = 'ht ...

  5. php生成图片缩略图的类方法

    //php生成缩略图片的类 class ResizeImage{ public $type;//图片类型 public $width;//实际宽度 public $height;//实际高度 publ ...

  6. 【.NET】XML文件的创建,修改,删除

    类名:XML /// 1.创建XML文档 /// 2.在根节点下增加子元素 /// 3.在元素下增加子元素 /// 4.获取类型为制定值的一组节点 /// 5.抓取网页上的xml文档赋值给XmlDoc ...

  7. Mysql 基础语法1

    MySQL的软件架构 1.开启MySQL服务器:以windows服务的形式开启,在cmd下net startmysql|net stop mysql,在cmd下使用mysqld –default-fi ...

  8. 一些常见的CFD基本概念(飞机为例)(摘抄)

    分享一些常见的,常用的但不容易掌握的CFD基础概念. 1.理想气体:不考虑流体粘性的影响. 2.不可压缩流体/恒密度:不考虑流体密度的变化.

  9. 第一百零五节,JavaScript正则表达式

    JavaScript正则表达式 学习要点: 1.什么是正则表达式 2.创建正则表达式 3.获取控制 4.常用的正则 假设用户需要在HTML表单中填写姓名.地址.出生日期等.那么在将表单提交到服务器进一 ...

  10. 第一百节,JavaScript表达式中的运算符

    JavaScript表达式中的运算符 学习要点: 1.什么是表达式 2.一元运算符 3.算术运算符 4.关系运算符 5.逻辑运算符 6.*位运算符 7.赋值运算符 8.其他运算符 9.运算符优先级 E ...