Fire Net
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6538 Accepted Submission(s): 3713
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......2XX.X3.X.X.X.X.3....XX.XX4................0
Sample Output
51524
#include<stdio.h>
#include<string.h>
int n; //方格大小
char maze[5][5]; //方格的内容
int mazer[5][5]; //按行缩点,如果某行连续几个点都是'.',则这几个点都编相同的序号
int mazec[5][5]; //按列缩点,如果 某列连续几个点都是'.',则这几个点都编相同的序号
int path[20][20]; //行和列之间编号之间的联系,表示二分图之间可以连接的边
int link[20]; //二分图不同子集之间的联系
int used[20]; //二分图使用时候的内部标记
int rn,cn; //行列缩点的编号
int dfs(int n)
{
for(int i=0;i<cn;i++)
{
if(path[n][i]==1 && used[i]==0)
{
used[i] = 1;
if(link[i]==0 || dfs(link[i])) //link[i]==0表示要连接的点没有其他的边连接着;dfs[link[i]]表示要连接的点有边连接着,则让连接着边的点再搜索其他的点进行连接
{
link[i] = n;
return 1;
}
}
}
return 0;
}
int match() //可以进行二分图的最大匹配
{
memset(link, 0, sizeof(link));
int ans = 0;
for(int i=0;i<rn;i++)
{
memset(used, 0, sizeof(used));
ans += dfs(i);
}
return ans;
}
int main()
{
while(scanf("%d", &n), n)
{
for(int i=0;i<n;i++)
{
scanf("%s", maze[i]);
}
memset(path, 0, sizeof(path));
memset(mazer, 0, sizeof(mazer));
memset(mazec, 0, sizeof(mazec));
rn = cn = 1;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(maze[i][j]=='.' && mazer[i][j]==0) //按行缩点
{
for(int k=j;maze[i][k]=='.' && k<n;k++)
{
mazer[i][k] = rn;
}
rn++;
}
if(maze[j][i]=='.' && mazec[j][i]==0) //按列锁点
{
for(int k=j;maze[k][i]=='.' && k<n;k++)
{
mazec[k][i] = cn;
}
cn++;
}
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(mazer[i][j]!=0 && mazec[i][j]!=0) //连边,表示二分图可以连接的边
{
path[mazer[i][j]][mazec[i][j]] = 1;
}
}
}
printf("%d\n", match());
}
}