Language:
Antenna Placement
Description
The Global Aerial Research Centre has been
allotted
(分配) the task of building the fifth generation of mobile phone nets in Sweden. The most striking reason why they got the job, is their discovery of a new, highly noise
resistant
(抵抗的),
antenna
(天线). It is called 4DAir, and comes in four types. Each type can only
transmit
(传输) and receive signals in a direction
aligned
(结盟) with a (slightly
skewed
(歪斜的))
latitudinal
(纬度的) and
longitudinal
(长度的) grid, because of the
interacting
(相互作用)
electromagnetic
(电磁的) field of the earth. The four types correspond to antennas operating in the directions north, west, south, and east,
respectively
(分别地). Below is an example picture of places of interest,
depicted
(描述) by twelve small rings, and nine 4DAir antennas depicted by
ellipses
(椭圆形) covering them.
Obviously, it is desirable (令人满意的) to use as few antennas as possible, but still provide coverage (覆盖) for each place of interest. We model the problem as follows: Let A be a rectangular (矩形的) matrix (矩阵) describing the surface of Sweden, where an entry of A either is a point of interest, which must be covered by at least one antenna, or empty space. Antennas can only be positioned at an entry in A. When an antenna is placed at row r and column c, this entry is considered covered, but also one of the neighbouring entries (c+1,r),(c,r+1),(c-1,r), or (c,r-1), is covered depending on the type chosen for this particular antenna. What is the least number of antennas for which there exists a placement (布置) in A such that all points of interest are covered? Input
On the first row of
input
(投入) is a single
positive
(积极的)
integer
(整数) n,
specifying
(指定) the number of
scenarios
(方案) that follow. Each scenario begins with a row containing two positive integers h and w, with 1 <= h <= 40 and 0 < w <= 10. Thereafter is a matrix presented, describing the points of interest in Sweden in the form of h lines, each containing w characters from the set ['*','o']. A '*'-character
symbolises
(象证) a point of interest,
whereas
(然而) a 'o'-character represents open space.
Output
For each
scenario
(方案),
output
(输出) the
minimum
(最小的) number of
antennas
(天线) necessary to cover all '*'-entries in the scenario's
matrix
(矩阵), on a row of its own.
Sample Input 2 7 9 ooo**oooo **oo*ooo* o*oo**o** ooooooooo *******oo o*o*oo*oo *******oo 10 1 * * * o * * * * * * Sample Output 17 5 |
#include<iostream>
#include<string.h>
#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
const int MAXN= 501;
int h, w, tol;
char Map[41][41];
bool used[MAXN];
int Hash[41][41], g[MAXN][MAXN], linker[MAXN];
bool dfs(int u)
{
int v;
for(v= 0;v<tol;v++)
{
if(g[u][v]&&!used[v])
{
used[v] = true;
if(linker[v]==-1||dfs(linker[v]))
{
linker[v] = u;
return true;
}
}
}
return false;
}
int hungary()
{
int res = 0;
int u;
memset(linker, -1,sizeof(linker));
for( u = 0; u<tol; ++u)
{
memset(used, 0, sizeof(used));
if(dfs(u))res++;
}
return res;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
tol = 0;
scanf("%d%d",&h,&w);
for(int i = 0; i<h; ++i)
{
scanf("%s",Map[i]);
for(int j = 0; j<w ; ++j)
if(Map[i][j]=='*')
Hash[i][j] = tol++;
}
memset(g, 0,sizeof(g));
for(int i = 0; i<h; ++i)
{
for(int j = 0; j<w; ++j)
{
if(Map[i][j]=='*')
{
if(i-1>=0&&Map[i-1][j]=='*')g[Hash[i][j]][Hash[i-1][j]] = 1;
if(i<h-1&&Map[i+1][j]=='*') g[Hash[i][j]][Hash[i+1][j]]=1;
if(j>0&&Map[i][j-1]=='*') g[Hash[i][j]][Hash[i][j-1]]=1;
if(j<w-1&&Map[i][j+1]=='*') g[Hash[i][j]][Hash[i][j+1]]=1;
}
}
}
printf("%d\n", tol-hungary()/2);//无向二分图:最小路径覆盖数 = "拆点"前原图的顶点数 - 最大匹配数/2
}
return 0;
}
参考:http://blog.csdn.net/lyy289065406/article/details/6647040