Antenna Placement
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 5486 |
|
Accepted: 2743 |
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>
using namespace std;
int ipmap[41][11]; //标记存在城市'*'的位置,并依次记录城市的编号
int ip; //城市编号(最终是城市数量)
int V1,V2; //二分图的两个顶点集
int M; //最大二分匹配
bool city[401][401]; //标记两个城市之间是否能连通
//通过“拆点”操作,把每一个城市拆分为2个,分别属于所构造的二分图的两个点集
bool vist[401];
int link[401];
int dire_r[4]={-1,1,0,0};
int dire_c[4]={0,0,-1,1}; //分别对应四个方位 上 下 左 右
/*Hungary Algorithm*/
bool dfs(int x)
{
for(int y=1;y<=V2;y++)
if(city[x][y] && !vist[y])
{
vist[y]=true;
if(link[y]==0 || dfs(link[y]))
{
link[y]=x;
return true;
}
}
return false;
}
void search(void)
{
for(int x=1;x<=V1;x++)
{
memset(vist,false,sizeof(vist));
if(dfs(x))
M++;
}
return;
}
int main(void)
{
int test,h,w;
cin>>test;
while(test--)
{
/*Initial*/
memset(ipmap,0,sizeof(ipmap));
memset(city,false,sizeof(city));
memset(link,0,sizeof(link));
ip=0;
M=0;
/*Read in the maps*/
cin>>h>>w;
int i,j;
char temp;
for(i=1;i<=h;i++)
for(j=1;j<=w;j++)
{
cin>>temp;
if(temp=='*')
ipmap[i][j]=++ip;
}
/*Structure the Bipartite Graphs*/
for(i=1;i<=h;i++)
for(j=1;j<=w;j++)
if(ipmap[i][j])
for(int k=0;k<4;k++)
{
int x=i+dire_r[k];
int y=j+dire_c[k];
if(ipmap[x][y])
city[ ipmap[i][j] ][ ipmap[x][y] ]=true; //"拆点"操作是"顺便"被完成的
} //二分图构造完毕后,之后的问题就和POJ3041一样处理了
V1=V2=ip;
/*增广轨搜索*/
search();
/*Output*/
cout<<ip-M/2<<endl; //无向二分图:最小路径覆盖数 = "拆点"前原图的顶点数 - 最大匹配数/2
}
return 0;
}