题目链接: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. Tomcat------如何配置域名和80端口

    1.打开Tomcat的默认安装路径下的Service.xml文件 路径:C:\Program Files\Apache Software Foundation\Tomcat 8.0\conf\Serv ...

  2. 如何使用Maven scope

    maven 有6个scope类型,下面简单总结备忘下 <dependency> <groupId>javax.servlet</groupId> <artif ...

  3. LeetCode[Array]----3Sum

    3Sum Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find a ...

  4. Python easyGUI 登录框 非空验证

    import easygui as g msg='欢迎注册' title='注册' fieldNames=['*用户名','*密码','*重复密码','真实姓名','手机号','QQ','e-mail ...

  5. vux 使用 font-awesome

    1)XIcon 太多坑,不好用,无奈之下,搞了一下 font-awesome 2)下载 font-awesome 源码,并放置到 根目录/src 目录下 ,传送门:http://fontawesome ...

  6. setTag,getTage复用

    radioButtons = new RadioButton[rgMain.getChildCount()]; //遍历RadioGroupfor (int i = 0; i < radioBu ...

  7. Java网络编程之查找Internet地址

    一.概述 连接到Internet上计算机都有一个称为Internet地址或IP地址的唯一的数来标识.由于IP很难记住,人们设计了域名系统(DNS),DNS可以将人们可以记忆的主机名与计算机可以记忆的I ...

  8. delphi xe 怎么生成apk

    f9 运行: 让它执行install[如果没有连接到android环境,会提示安装失败]或, 就在bin下面产生一个apk文件了:好像单单build是没法产生的.

  9. [转载]Array.prototype.slice.call(arguments,1)原理

    Array.prototype.slice.call(arguments,1)该语句涉及两个知识点. arguments是一个关键字,代表当前参数,在javascript中虽然arguments表面上 ...

  10. junit4 详解

    转:http://www.cnblogs.com/eggbucket/archive/2012/02/02/2335697.html JUnit4概述 JUnit4是JUnit框架有史以来的最大改进, ...