hdu_1045Fire Net(二分图匹配)
标签: 图论 二分图匹配
题目链接
Fire Net
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10069 Accepted Submission(s): 5863
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
题意:
要在一个矩阵格子里面放大炮,每个炮可以攻打一行或一列,注意,这里有墙的地方可以抵挡攻击,问你最多可以在这个图上放几个炮,使得每个炮不能被其他的炮打上.
题解:
首先是建图,这个图因为有了墙的存在把每一行每一列划分成了不同的联通快,即建图的时候,扫描每一行,每一列,把当前联通的部分建成一个点,如果一个点和对应的列节点有交点的话则建立一条从行到列的一个边,然后最后计算这个图的二分图匹配即可
代码:
//二分图一般用链表存边
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 50;
char mp[N][N];
int vis[N];
int head[N];
struct Edge{
int to;
int next;
}edge[N*N];
int rm[N];
int Ecnt;
int id[N][N];//保存节点的编号
void init()
{
Ecnt = 0;
memset(head,-1,sizeof(head));
memset(rm,-1,sizeof(rm));
}
void add(int from, int to){
edge[Ecnt].to = to;
edge[Ecnt].next = head[from];
head[from] = Ecnt++;
edge[Ecnt].to = from;
edge[Ecnt].next = head[to];
head[to] = Ecnt++;
}
int list(int s){
for(int i = head[s]; i != -1; i = edge[i].next){
int t = edge[i].to;
if(vis[t]) continue;
vis[t] = 1;
if(rm[t]==-1||list(rm[t])){
rm[t] = s;
return 1;
}
}
return 0;
}
int Max_match(int n)
{
int ans = 0;
for(int i = 1; i <= n; i++){//注意是从1开始编号的
memset(vis,0,sizeof(vis));
vis[i] = 1;
if(list(i)) ans++;
}
return ans;
}
int main()
{
int n;
while(~scanf("%d",&n),n)
{
init();
for(int i = 0; i < n; i++){
scanf("%s",mp[i]);
}
int cntx,cnty;
cntx = cnty = 0;
memset(id,0,sizeof(id));
int tp = 1;
//建图,先扫描行
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(mp[i][j] == 'X'){id[i][j] = 0;tp++;}//没有点标记为0
else id[i][j] = tp;
}
tp++;//每一行结束的时候也要区分
}
int tm = tp-1;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(mp[j][i]=='X'){
tp++;
}
else add(id[j][i],tp);
}
tp++;
}
int ans = Max_match(tm);
printf("%d
",ans);
}
return 0;
}
注释:
这个题给了我们一个建图的思路