HDU - 1045 Fire Net(二分匹配)
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..
.... 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(二分匹配)的更多相关文章
- hdu 1045 Fire Net(二分匹配 or 暴搜)
Fire Net Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- hdu 1045 Fire Net 二分图匹配 && HDU-1281-棋盘游戏
题意:任意两个个'车'不能出现在同一行或同一列,当然如果他们中间有墙的话那就没有什么事,问最多能放多少个'车' 代码+注释: 1 //二分图最大匹配问题 2 //难点在建图方面,如果这个图里面一道墙也 ...
- 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 【连通块的压缩 二分图匹配】
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit: 2000/1000 MS (Java/Others) ...
- 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经典题)
Fire Net Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
随机推荐
- Mac下如何使用Vim
帖子是转载的,原帖地址 Mac OX其实已经默认装好了vim了.不需要再安装Macvim https://ruby-china.org/topics/4905 Mac开发利器之程序员编辑器MacVim ...
- linux基础-安装篇
linux现在的版本已经升级到7.x的版本了,现在虽然企业还是使用6.x的版本,但是未来的趋势肯定是往7的方向发展的,很多的培训机构或者一些学校的课程也都更新的7的版本进行教学,下面的安装实例就是以7 ...
- 一、java自带的观察者模式
Observer对象是观察者,Observable对象是被观察者. 官网api文档:http://docs.oracle.com/javase/7/docs/api/ 1. 实现观察者模式 实现观察者 ...
- innodb更改行格式,系统盘占用急剧升高
#大表引擎修改后,数据量较myisam引擎表大很多,对存储的行格式修改后,数据量减小. #备库修改时,由于服务器时间较早,系统盘20G,突然收到/磁盘空间占比89%的报警,立即将修改中断,恢复正常 # ...
- Git忽略对特定文件的跟踪和提交
1.有未提交过的文件,并且此文件项目组中的其他人员也需要忽略,可将此文件的完整路径写入项目文件夹下的.gitignore文件. 2.有未提交过的文件,此这些文件与项目组中的其他人员无关,毋须写入.gi ...
- Oracle获取时间日期月份星期数
1.日期和字符转换函数用法(to_date,to_char)select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') as nowTime from dual; ...
- java中String类型的相关知识
String类方法整理说明: ·Length()用来求字符串的长度,返回值为字符串的长度: ·charAt()取该字符串某个位置的字符,从0开始,为char类型: ·getChars()将这个字符串中 ...
- ZZNU 1992: 情人节的尴尬
题目描述 情人节这不刚过去没多久吗,我得给大家爆个料.这个事关于小飞飞的,小飞飞呢,要给她女票买礼物,但是呢有个比较尴尬的事情,小飞飞有些钱在某宝里,有些钱在某东里,众所周知,这俩可是死对头,想相互转 ...
- 安卓android:scaleType属性
ImageView.ScaleType.CENTER|android:scaleType="center" 以原图的几何中心点和ImagView的几何中心点为基准,按图片的原来si ...
- Hibernate使用原生SQL(转载)
本文转载,出处如下:http://bhdweb.iteye.com/blog/801084 HQL尽管容易使用,但是在一些复杂的数据操作上功能有限.特别是在实现复杂的报表统计与计算,以及多表连接查询上 ...