您的位置:首页 > 其它

HDU 1045 Fire Net(二分图最大匹配)

2017-07-10 14:26 357 查看


Fire Net

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

Total Submission(s): 12126    Accepted Submission(s): 7315


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

Zhejiang University Local Contest 2001

题目大意:

    给个有障碍物的矩阵图,问最多能放多少个城堡,使得他们不会向上下左右相互攻击到。

解题思路:

    这时一个二分图建图的经典模型,我们可以把横坐标和纵坐标看作二分图的两边,每个点就是一条边,这样同一横坐标和纵坐标就只会出现一次,不过这题还有障碍物,那么我们就可以让障碍物把一行或一列分成多行或多列,这样就使得障碍物起到了阻碍作用。

AC代码:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <vector>
#include <queue>
#include <stack>
#include <deque>
#include <string>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define LL long long
#define fi first
#define se second
#define mem(a,b) memset((a),(b),sizeof(a))

const int MAXN=4+1;
const int MAXV=MAXN*MAXN;
char maze[MAXN][MAXN];
int N,x_id[MAXN][MAXN],y_id[MAXN][MAXN];
int num_x;//左边顶点数
vector<int> G[MAXV];//图的邻接表形式(左边顶点在前面,只需要建立从左边指向右边的边即可)
int match_x[MAXV],match_y[MAXV];//顶点匹配对象
int dis,dis_x[MAXV],dis_y[MAXV];//距离(用于多路增广)
bool used[MAXV];

bool searchP()//标记距离(用于多路增广)
{
queue<int> que;
dis=INF;
mem(dis_x,-1);
mem(dis_y,-1);
for(int i=0;i<num_x;++i)
if(match_x[i]==-1)
{
que.push(i);
dis_x[i]=0;
}
while(!que.empty())
{
int u=que.front(); que.pop();
if(dis_x[u]>dis)
break;
for(int i=0;i<G[u].size();++i)
{
int v=G[u][i];
if(dis_y[v]==-1)
{
dis_y[v]=dis_x[u]+1;
if(match_y[v]==-1)
dis=dis_y[v];
else
{
dis_x[match_y[v]]=dis_y[v]+1;
que.push(match_y[v]);
}
}
}
}
return dis!=INF;
}

bool dfs(int u)//dfs增广
{
for(int i=0;i<G[u].size();++i)
{
int v=G[u][i];
if(!used[v]&&dis_y[v]==dis_x[u]+1)
{
used[v]=true;
if(match_y[v]!=-1&&dis_y[v]==dis)
continue;
if(match_y[v]==-1||dfs(match_y[v]))
{
match_y[v]=u;
match_x[u]=v;
return true;
}
}
}
return false;
}

int hopcroft_carp()
{
int res=0;
mem(match_x,-1);
mem(match_y,-1);
while(searchP())
{
mem(used,0);
for(int i=0;i<num_x;++i)
if(match_x[i]==-1&&dfs(i))
++res;
}
return res;
}

void init()
{
num_x=0;
for(int i=0;i<MAXV;++i)
G[i].clear();
}

int main()
{
while(~scanf("%d",&N)&&N)
{
init();
for(int i=0;i<N;++i)
{
scanf("%s",maze[i]);
for(int j=0;j<N;++j)
{
if(maze[i][j]=='.')
x_id[i][j]=num_x;
else ++num_x;
}
++num_x;
}
int cnt=0;
for(int j=0;j<N;++j)
{
for(int i=0;i<N;++i)
{
if(maze[i][j]=='.')
{
y_id[i][j]=cnt;
G[x_id[i][j]].push_back(y_id[i][j]);
}
else ++cnt;
}
++cnt;
}
printf("%d\n",hopcroft_carp());
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: