题目链接: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算法求二分图最大匹配比匈牙利算法慢。)

最新文章

  1. CSS笔记
  2. LinQ和ADO.Net增删改查 备忘
  3. Lambda表达式入门
  4. java调用cmd命令删除文件夹及其所有内容
  5. 常见面试第二题之什么是Context
  6. 二叉搜索树的两种实现(数组模拟,STL)
  7. HW4.31
  8. rt: Unknown command &#39;PATH=&#39;
  9. DevExpress中SearchLookUpEdit用法总结
  10. Process Explorer(增强任务管理器) V16.05 免费绿色版
  11. Maven搭建springMVC+spring+hibernate环境
  12. SSIS - 5.优先约束
  13. MySQL Packets larger than max_allowed_packet are not allowed
  14. VS2010/MFC编程入门之五十一(图形图像:GDI对象之画刷CBrush)
  15. newcode wyh的吃鸡(优势队列+BFS)题解
  16. Tomcat组成与工作原理
  17. python接口自动化1-发送get请求
  18. tomcat_日志打印格式问题
  19. 百度地图API学习总结
  20. Java subList的使用

热门文章

  1. 兼容 iOS Retina(视网膜显示) 的程序
  2. SpringBoot------注解把配置文件自动映射到属性和实体类
  3. Dubbo -- 系统学习 笔记 -- 示例 -- 结果缓存
  4. Ansible 如何查看模块文档
  5. inux跟踪线程的方法:LWP和strace命令
  6. easyui combobox 实时刷新
  7. PowerDesigner学习笔记
  8. cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration的解决
  9. windows7内核分析之x86&amp;x64第二章系统调用
  10. delphi 10 Seattle 第一个Android程序