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

Fire Net

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12920    Accepted Submission(s): 7840

Problem 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
 
Source
 

题解:

此题可以直接枚举,但如果数据更大的话,就需要用二分图来解了:

1.对于每一行,把连通的部分缩成一个点,并为之编号xid。同理,每一列也如此yid。

2.如果a[x][y]是空格,那么就在点xid[x][y]与点yid[x][y]之间连一条边。表明:如果在[x][y]处放置一个棋子,那么在点xid[x][y]和点yid[x][y]所涉及到的区域里,已经存在着攻击,所以不能再放其他棋子。

3.求出最大匹配数,就是x坐标与y坐标的最大组合数,每一对组合,都代表着一个可放置点(影响的范围为一段区域),所以最大匹配数,即为最大放置数。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
const int INF = 2e9;
const int MOD = 1e9+;
const int MAXN = +; int n, uN, vN;
char a[MAXN][MAXN];
int M[MAXN][MAXN], xid[MAXN][MAXN], yid[MAXN][MAXN], link[MAXN];
bool vis[MAXN]; bool dfs(int u)
{
for(int i = ; i<=vN; i++)
if(M[u][i] && !vis[i])
{
vis[i] = true;
if(link[i]==- || dfs(link[i]))
{
link[i] = u;
return true;
}
}
return false;
} int hungary()
{
int ret = ;
memset(link, -, sizeof(link));
for(int i = ; i<=uN; i++)
{
memset(vis, , sizeof(vis));
if(dfs(i)) ret++;
}
return ret;
} int main()
{
while(scanf("%d", &n) && n)
{
for(int i = ; i<=n; i++)
scanf("%s", a[i]+); uN = vN = ;
for(int i = ; i<=n; i++) //每一行或列,把连通的部分缩成一点
for(int j = ; j<=n; j++)
{
if(a[i][j]=='.')
{
if(j== || a[i][j-]=='X') ++uN;
xid[i][j] = uN;
} if(a[j][i]=='.')
{
if(j== || a[j-][i]=='X') ++vN;
yid[j][i] = vN;
}
} memset(M, false, sizeof(M));
for(int i = ; i<=n; i++)
for(int j = ; j<=n; j++)
if(a[i][j]!='X')
M[xid[i][j]][yid[i][j]] = true; int ans = hungary();
printf("%d\n", ans);
}
}

HDU1045 Fire Net —— 二分图最大匹配的更多相关文章

  1. HDU - 1045 Fire Net (二分图最大匹配-匈牙利算法)

    (点击此处查看原题) 匈牙利算法简介 个人认为这个算法是一种贪心+暴力的算法,对于二分图的两部X和Y,记x为X部一点,y为Y部一点,我们枚举X的每个点x,如果Y部存在匹配的点y并且y没有被其他的x匹配 ...

  2. HDU1045(KB10-A 二分图最大匹配)

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

  3. HDU 1045 - Fire Net - [DFS][二分图最大匹配][匈牙利算法模板][最大流求二分图最大匹配]

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

  4. POJ 3041 Asteroids / UESTC 253 Asteroids(二分图最大匹配,最小点匹配)

    POJ 3041 Asteroids / UESTC 253 Asteroids(二分图最大匹配,最小点匹配) Description Bessie wants to navigate her spa ...

  5. POJ 2226二分图最大匹配

    匈牙利算法是由匈牙利数学家Edmonds于1965年提出,因而得名.匈牙利算法是基于Hall定理中充分性证明的思想,它是二部图匹配最常见的算法,该算法的核心就是寻找增广路径,它是一种用增广路径求二分图 ...

  6. POJ2239 Selecting Courses(二分图最大匹配)

    题目链接 N节课,每节课在一个星期中的某一节,求最多能选几节课 好吧,想了半天没想出来,最后看了题解是二分图最大匹配,好弱 建图: 每节课 与 时间有一条边 #include <iostream ...

  7. poj 2239 二分图最大匹配,基础题

    1.poj 2239   Selecting Courses   二分图最大匹配问题 2.总结:看到一个题解,直接用三维数组做的,很巧妙,很暴力.. 题意:N种课,给出时间,每种课在星期几的第几节课上 ...

  8. UESTC 919 SOUND OF DESTINY --二分图最大匹配+匈牙利算法

    二分图最大匹配的匈牙利算法模板题. 由题目易知,需求二分图的最大匹配数,采取匈牙利算法,并采用邻接表来存储边,用邻接矩阵会超时,因为邻接表复杂度O(nm),而邻接矩阵最坏情况下复杂度可达O(n^3). ...

  9. 二分图最大匹配的K&#246;nig定理及其证明

     二分图最大匹配的K?nig定理及其证明 本文将是这一系列里最短的一篇,因为我只打算把K?nig定理证了,其它的废话一概没有.    以下五个问题我可能会在以后的文章里说,如果你现在很想知道的话,网上 ...

随机推荐

  1. Android开发——流量统计

    1. 获取应用UID 在设备的proc目录下我们可以看到一些比较熟悉的目录/文件,比如data,system,cpuinfo(获取CPU信息)等,其中uid_stat的各个以应用Uid命名的目录下,便 ...

  2. Oracle数据库之初步接触

    每个Oracle数据库都是数据的集合,这些数据包含在一个或多个文件中.数据库有物理和逻辑两种结构.在开发应用程序的过程中,会创建诸如表和索引这样的结构,这些结构用于数据行的存储和查询.可以为对象的名称 ...

  3. Java面向对象重要关键字

    面向对象过程接触的重要关键字:extends   super   this   final   static  extends 关键字用作继承功能,可以申明一个类是从另外一个类继承而来的,一般形式如下 ...

  4. Leetcode 312.戳气球

    戳气球 有 n 个气球,编号为0 到 n-1,每个气球上都标有一个数字,这些数字存在数组 nums 中. 现在要求你戳破所有的气球.每当你戳破一个气球 i 时,你可以获得 nums[left] * n ...

  5. Exact Change(01背包)

    描述 Seller: That will be fourteen dollars. Buyer: Here's a twenty. Seller: Sorry, I don't have any ch ...

  6. centos配置mutt跟msmtp发送邮件

    一.安装mutt yum install mutt 二.配置mutt vi /etc/Muttrc 在里面找到下面几行,并将内容修改为你自己的内容(下面几行分布在不同位置,请耐心查找,记得去掉它行首的 ...

  7. [luoguP2948] [USACO09OPEN]滑雪课Ski Lessons(DP)

    传送门 f[i][j]表示i时刻能力值为j的最大滑雪数 显然f[0][1]=0,开始搜索 三种转移: ①美美的喝上一杯**:f[i+1][j]=max(f[i+1][j],f[i][j]) ②滑雪,f ...

  8. [POJ2352] Stars(树状数组)

    传送门 先按照下标x排序,然后依次把y加入树状数组,边加入边统计即可. 注意下标re从零开始,需+1s ——代码 # include <iostream> # include <cs ...

  9. poj1091:跳蚤【容斥原理】

    题目大意:中文题就不翻译了 思路:假设跳蚤选择X1个第一张卡片,X2个第二张卡片...Xn个第n张卡片,Xn+1张写着m的卡片,那么就可以列出方程:a1*X1+a2*X2+…+an*Xn+m*X(n+ ...

  10. Android Application基本组成部分

    Android Application基本组成部分 四个核心的组件 Activity活动,主要用于前台和用户交互,即UI,Activity只是加载一个View而并非一个UI对象 Service服务,主 ...