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

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

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
 
题意:
给一个N*N的方阵,“.”代表空地,“X”代表有一堵墙,现要求在空地上放置碉堡,并且满足同一条线上的碉堡间必须有一堵墙挡着,求最多能放置的碉堡数;
 
题解:
 
方法①
由于N<=4,所以放置碉堡情况最多不会超过2^16=65536,可以直接用DFS去暴力枚举每一个方格上是否放置碉堡;
AC代码:
 #include<cstdio>
#include<algorithm>
using namespace std;
int n;
char map[][];
int ans;
void dfs(int now,int num)
{
if(now==n*n+)
{
ans=max(ans,num);
return;
} int row=(now-)/n+, col=(now-)%n+; if(map[row][col]=='X')
{
dfs(now+,num);
return;
} bool ok=;
for(int i=col-;i>=;i--)//向前遍历当前行
{
if(map[row][i]=='B')
{
ok=;
break;
}
if(map[row][i]=='X') break;
}
for(int i=row-;i>=;i--)//向前遍历当前列
{
if(map[i][col]=='B')
{
ok=;
break;
}
if(map[i][col]=='X') break;
}
if(ok)
{
map[row][col]='B';
dfs(now+,num+);
map[row][col]='.';
}
dfs(now+,num);
return;
}
int main()
{
while(scanf("%d",&n) && n!=)
{
for(int i=;i<=n;i++) scanf("%s",map[i]+);
ans=;
dfs(,);
printf("%d\n",ans);
}
}

方法②

用二分图最大匹配来做本题。

前置知识点:http://www.cnblogs.com/dilthey/p/7647630.html

建模:

我们对这个方阵的每一行,以方阵的边界或者一堵墙为端点,我们对每一个“行段”(从一端开始到一端结束)的方格组标记为一个顶点,全部放入L集;

例如:

再对这个方阵的每一列,依然以方阵的边界或者一堵墙为端点,我们对每一个“列段”(从一端开始到一端结束)的方格组标记为一个顶点,全部放入R集;

例如:

那么,对应于方格内的每个格子,它们都有一个L集内的顶点编号,一个R集内的顶点编号,我们就建立一条连接两个这两个顶点的边;

then,根据匹配的要求,任意两条边都没有公共顶点,

即任意一个格子,都独占一个L集内的顶点,一个R集内的顶点,

即如果这里放了一个碉堡,那么它都独占了它所在的一个“行段”,一个“列段”,这就满足了任意两个碉堡间不会互相攻击;

那么,任意一种碉堡的放置方案,都对应一个「匹配」,我们找到最大匹配,就找到了放置碉堡最多的方案。

AC代码:

 #include<cstdio>
#include<cstring>
#include<vector>
#define MAX 35
using namespace std;
//匈牙利算法 - st
struct Edge{
int u,v;
};
vector<Edge> E;
vector<int> G[MAX];
int lN,rN;
int matching[MAX];
int vis[MAX];
void init(int l,int r)
{
E.clear();
for(int i=l;i<=r;i++) G[i].clear();
}
void add_edge(int u,int v)
{
E.push_back((Edge){u,v});
E.push_back((Edge){v,u});
int _size=E.size();
G[u].push_back(_size-);
G[v].push_back(_size-);
}
bool dfs(int u)
{
for(int i=,_size=G[u].size();i<_size;i++)
{
int v=E[G[u][i]].v;
if (!vis[v])
{
vis[v]=;
if(!matching[v] || dfs(matching[v]))
{
matching[v]=u;
matching[u]=v;
return true;
}
}
}
return false;
}
int hungarian()
{
int ret=;
memset(matching,,sizeof(matching));
for(int i=;i<=lN;i++)
{
if(!matching[i])
{
memset(vis,,sizeof(vis));
if(dfs(i)) ret++;
}
}
return ret;
}
//匈牙利算法 - ed
int main()
{
int n;
char mp[][];
int row_id[][],col_id[][];
while(scanf("%d",&n) && n!=)
{
for(int i=;i<=n;i++) scanf("%s",mp[i]+); lN=, rN=;
for(int i=;i<=n;i++)//对“行段”进行编号
{
for(int j=;j<=n;j++)
{
if(mp[i][j]=='.')
{
if( j== || mp[i][j-]=='X' ) row_id[i][j] = ++lN;
else row_id[i][j] = lN;
}
}
}
for(int j=;j<=n;j++)//对“列段”进行编号
{
for(int i=;i<=n;i++)
{
if(mp[i][j]=='.')
{
if( i== || mp[i-][j]=='X' ) col_id[i][j] = lN + (++rN);
else col_id[i][j] = lN + rN;
}
}
} init(,lN+rN);
for(int i=;i<=n;i++)//建边、建图
{
for(int j=;j<=n;j++)
{
if(mp[i][j]=='X') continue;
add_edge(row_id[i][j],col_id[i][j]);
}
} printf("%d\n",hungarian());
}
}

方法③

当然了,二分图最大匹配,也可以使用最大流来求解;

我们对二分图进行如此构建流网络:

建立超级源点s,超级汇点t;

对所有L集的点,连一条从s出发的边,容量为1;

对所有R集的点,连一条到达t的边,容量为1;

对原二分图本就存在的边,直接赋值容量=1,加入流网络;

最后求出最大流,即二分图的最大匹配。

AC代码:

 #include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#define MAX 35
#define INF 0x3f3f3f3f
using namespace std;
struct Edge{
int u,v,c,f;
};
struct Dinic
{
int s,t;
vector<Edge> E;
vector<int> G[MAX];
bool vis[MAX];
int lev[MAX];
int cur[MAX];
void init(int l,int r)
{
E.clear();
for(int i=l;i<=r;i++) G[i].clear();
}
void addedge(int from,int to,int cap)
{
E.push_back((Edge){from,to,cap,});
E.push_back((Edge){to,from,,});
int m=E.size();
G[from].push_back(m-);
G[to].push_back(m-);
}
bool bfs()
{
memset(vis,,sizeof(vis));
queue<int> q;
q.push(s);
lev[s]=;
vis[s]=;
while(!q.empty())
{
int now=q.front(); q.pop();
for(int i=,_size=G[now].size();i<_size;i++)
{
Edge edge=E[G[now][i]];
int nex=edge.v;
if(!vis[nex] && edge.c>edge.f)
{
lev[nex]=lev[now]+;
q.push(nex);
vis[nex]=;
}
}
}
return vis[t];
}
int dfs(int now,int aug)
{
if(now==t || aug==) return aug;
int flow=,f;
for(int& i=cur[now],_size=G[now].size();i<_size;i++)
{
Edge& edge=E[G[now][i]];
int nex=edge.v;
if(lev[now]+ == lev[nex] && (f=dfs(nex,min(aug,edge.c-edge.f)))>)
{
edge.f+=f;
E[G[now][i]^].f-=f;
flow+=f;
aug-=f;
if(!aug) break;
}
}
return flow;
}
int maxflow()
{
int flow=;
while(bfs())
{
memset(cur,,sizeof(cur));
flow+=dfs(s,INF);
}
return flow;
}
}dinic;
int main()
{
int n;
char mp[][];
int row_id[][],col_id[][];
while(scanf("%d",&n) && n!=)
{
for(int i=;i<=n;i++) scanf("%s",mp[i]+); int lN=, rN=;
for(int i=;i<=n;i++)//对“行段”进行编号
{
for(int j=;j<=n;j++)
{
if(mp[i][j]=='.')
{
if( j== || mp[i][j-]=='X' ) row_id[i][j] = ++lN;
else row_id[i][j] = lN;
}
}
}
for(int j=;j<=n;j++)//对“列段”进行编号
{
for(int i=;i<=n;i++)
{
if(mp[i][j]=='.')
{
if( i== || mp[i-][j]=='X' ) col_id[i][j] = lN + (++rN);
else col_id[i][j] = lN + rN;
}
}
} dinic.init(,lN+rN+);
dinic.s=, dinic.t=lN+rN+;
for(int i=;i<=lN;i++) dinic.addedge(dinic.s,i,);
for(int i=;i<=rN;i++) dinic.addedge(i+lN,dinic.t,);
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
if(mp[i][j]=='X') continue;
dinic.addedge(row_id[i][j],col_id[i][j],);
}
}
printf("%d\n",dinic.maxflow());
}
}

(当然,从复杂度上,就不难看出,用Dinic算法求二分图最大匹配比匈牙利算法慢。)

HDU 1045 - Fire Net - [DFS][二分图最大匹配][匈牙利算法模板][最大流求二分图最大匹配]的更多相关文章

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

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

  2. HDU 2444 - The Accomodation of Students - [二分图判断][匈牙利算法模板]

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

  3. hdu 2063 过山车 (最大匹配 匈牙利算法模板)

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

  4. Ural1109_Conference(二分图最大匹配/匈牙利算法/网络最大流)

    解题报告 二分图第一题. 题目描写叙述: 为了參加即将召开的会议,A国派出M位代表,B国派出N位代表,(N,M<=1000) 会议召开前,选出K队代表,每对代表必须一个是A国的,一个是B国的; ...

  5. HDU 1045 Fire Net(DFS)

    Fire Net Problem Description Suppose that we have a square city with straight streets. A map of a ci ...

  6. 51Nod 飞行员配对(二分图最大匹配)(匈牙利算法模板题)

    第二次世界大战时期,英国皇家空军从沦陷国征募了大量外籍飞行员.由皇家空军派出的每一架飞机都需要配备在航行技能和语言上能互相配合的2名飞行员,其中1名是英国飞行员,另1名是外籍飞行员.在众多的飞行员中, ...

  7. USACO 4.2 The Perfect Stall(二分图匹配匈牙利算法)

    The Perfect StallHal Burch Farmer John completed his new barn just last week, complete with all the ...

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

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

  9. HDU 5943 Kingdom of Obsession 【二分图匹配 匈牙利算法】 (2016年中国大学生程序设计竞赛(杭州))

    Kingdom of Obsession Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

随机推荐

  1. nginx: [error] open() "/run/nginx.pid" failed (2: No such file or directory)

    当你执行sudo nginx -s reload时出现nginx: [error] open() "/run/nginx.pid" failed (2: No such file ...

  2. VS调试DLL代码使用”附加到进程“

    如果一个DLL解决方案,被另一个DLL2解决方案依赖,DLL2被可执行程序exe1引用 如何调试DLL的代码断点呢 1.可以参考另一篇随笔DLL如何调试 2.先运行起来exe1,然后再DLL项目中”调 ...

  3. Unity中Surface Shader执行过程

  4. 64位Oracle 11g 使用PL/SQL

    Oracle 11g和PL/SQL安装完后,发现打开PL/SQL并不能连接Oracle数据库! [第一回合]完败! 先是在网上找解决方法,说是需要使用Net Configuration Assista ...

  5. N76E003的学习之路(一)

    N76E003是8051内核的一款单片机MCU,它提供丰富的特殊功能模块,包括: 1KRAM其中包括256字节SRAM,768字节XRAM. 最多可达18个标准管脚. 两组标准16位定时器/计数器:定 ...

  6. 《Lua程序设计》9.3 以协同程序实现迭代器 学习笔记

    例:编写一个迭代器,使其可以遍历某个数组的所有排列组合形式.代码如下: function permgen(a, n) n = n or #a -- 默认n为a的大小 then -- 还需要改变吗? p ...

  7. (转载)Recyclerview | Intent与Bundle在传值上的区别 | 设置布局背景为白色的三种方法

    用Recyclerview实现列表分组.下拉刷新以及上拉加载更多  http://www.jianshu.com/p/be62ce21ea57 Intent与Bundle在传值上的区别http://b ...

  8. 【线程】Thread中的join介绍

    因为sleep.wait.join等阻塞,可以使用interrupted exception异常唤醒. 一.作用 Thread类中的join方法的主要作用就是同步,它可以使得线程之间的并行执行变为串行 ...

  9. 【Python系列】python关键字、符号、数据类型等分类

    https://github.com/AndyFlower/Python/blob/master/sample/python前言如下部分为python关键字,操作符号,格式字符.转义字符等,以后有时间 ...

  10. Android Studio 删除 Module

    1.选中Module右击,选择 Open Module Settings,打开Project Structure 窗空.(或者选中Module,按F4打开Project Structure窗口) 2. ...