poj 3020 Antenna Placement

来源:互联网 发布:cda数据分析师怎么样 编辑:程序博客网 时间:2024/06/03 14:04
Antenna Placement
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 8561 Accepted: 4238

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

27 9ooo**oooo**oo*ooo*o*oo**o**ooooooooo*******ooo*o*oo*oo*******oo10 1***o******

Sample Output

175

Source

Svenskt Mästerskap i Programmering/Norgesmesterskapet 2001

提示

题意:

h*w的区域有n个城市(在图中表示为*),如何使用最少的抗干扰雷达才能完全覆盖所有的城市。(覆盖方式请看图)

思路:

这题是最小路径覆盖问题,将雷达能覆盖的路径用图存起来(路径是城市与城市之间才算数),之后对图使用匈牙利算法。(详细解析)

示例程序

Source CodeProblem: 3020Code Length: 1662BMemory: 1020KTime: 16MSLanguage: GCCResult: Accepted#include <stdio.h>#include <string.h>int map[40][10],v[400],match[400],city[400][400];int dfs(int t,int n){    int i;    for(i=0;n>i;i++)    {        if(city[t][i]==1&&v[i]==0)        {            v[i]=1;            if(match[i]==-1||dfs(match[i],n)==1)            {                match[i]=t;                return 1;            }        }    }    return 0;}int pro(int n){    int i,sum=0;    memset(match,-1,sizeof(match));    for(i=0;n>i;i++)    {        memset(v,0,sizeof(v));        sum=sum+dfs(i,n);    }    return sum;}int main(){    int i,i1,i2,i3,h,w,t,id,a,b,x[4]={0,1,0,-1},y[4]={1,0,-1,0};    char ch[11];    scanf("%d",&t);    for(i=1;t>=i;i++)    {        memset(map,-1,sizeof(map));        memset(city,0,sizeof(city));        scanf("%d %d",&h,&w);        id=0;        for(i1=0;h>i1;i1++)        {            scanf("%s",ch);            for(i2=0;ch[i2]!='\0';i2++)//为每个城市编号            {                if(ch[i2]=='*')                {                    map[i1][i2]=id;                    id++;                }            }        }        for(i1=0;h>i1;i1++)        {            for(i2=0;w>i2;i2++)            {                if(map[i1][i2]!=-1)                {                    for(i3=0;3>=i3;i3++)                    {                        a=i1+x[i3];                        b=i2+y[i3];                        if(a<h&&a>=0&&b<w&&b>=0&&map[a][b]!=-1)//城市之间存在通路                        {                            city[map[i1][i2]][map[a][b]]=1;                        }                    }                }            }        }        printf("%d\n",id-pro(id)/2);//最小路径覆盖数=点数量-最小路径边数,最小路径边数=最小路径点数/2,最小路径点数=匈牙利算法    }    return 0;}

0 0
原创粉丝点击