Antenna Placement(POJ--3020

来源:互联网 发布:怎样复制淘宝链接 编辑:程序博客网 时间:2024/05/21 09:58

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.
题意:一个天线可以覆盖两颗星星,求要覆盖全部的星星所需的最少天线。T组输入,每组输入n和m分别代表地图的行数和列数,地图中“*”代表要覆盖的星星,“o”代表空地。
思路:将星星进行编号,只要是相邻的就连线建图(此图是无向图),其实该题求得是二分图的最大独立集,最大独立集=顶点数-最大匹配数,由于此图是无向图所以求得的最大匹配有重边所以应该除以2即此题的最大独立集=顶点数-最大匹配数/2。

Sample Input

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

Sample Output

175

#include <cstdio>#include <queue>#include <cstring>#include <algorithm>#define Size 50#define INF 0x3f3f3f3fusing namespace std;int dx[]= {1,0,-1,0};              //相邻的四个方向坐标int dy[]= {0,1,0,-1};char mmap[Size][Size];int num[Size][Size],sum[450][450],match[450],chk[450],cnt;int dfs(int p){    int t;    for(int i=1; i<=cnt; i++)            {        if(sum[p][i]&&!chk[i])        <span style="font-family: Arial, Helvetica, sans-serif;">//找p的一个对应点且该点没有检查过(检查指尝试过更改i的匹配</span>        {            chk[i]=1;                          //设检查标志            t=match[i];                       //取出i的原匹配点t            match[i]=p;                      //保证新的匹配方案            if(t==-1||dfs(t))               //若t点是一个初始值,表示i点原来没有匹配,则增广成功返回或者t点能重新找到一个新的匹配点                return 1;            match[i]=t;                        //若不成功,则恢复i点原匹配方案,重新找点来增广        }    }    return 0;                                   //p点不能增广进去}int main(){    //freopen("lalala.text","r",stdin);    int t,n,m;    scanf("%d",&t);    while(t--)    {        memset(mmap,0,sizeof(mmap));        memset(num,0,sizeof(num));        memset(sum,0,sizeof(sum));        memset(match,-1,sizeof(match));        scanf("%d %d",&n,&m);        cnt=0;        for(int i=1; i<=n; i++)        {            scanf("%s",mmap[i]+1);            for(int j=1; j<=m; j++)            {                if(mmap[i][j]=='*')                    num[i][j]=++cnt;        //给星星进行编号并对当前坐标进行标记            }        }//        for(int i=1; i<=n; i++)//            for(int j=1; j<=m; j++)//                printf("%d%c",num[i][j],j==m?'\n':' ');        for(int i=1; i<=n; i++)            for(int j=1; j<=m; j++)            {                if(mmap[i][j]=='*')                {                    for(int k=0; k<4; k++)                    {                        int xx=i+dx[k];                        int yy=j+dy[k];                        if(xx<=n&&xx>=1&&yy<=m&&yy>=1&&mmap[xx][yy]=='*')                        {                            sum[num[i][j]][num[xx][yy]]=1;     //连线建图                        }                    }                }            }        int res=0;        for(int i=1; i<=cnt; i++)       //对cnt个点依次进行增广        {            memset(chk,0,sizeof(chk));     //一次增广中对chk初始化            res+=dfs(i);                                //增广成功,表示i点找到了一个匹配,多了一条匹配边        }        res=cnt-res/2;        printf("%d\n",res);    }    return 0;}



0 0
原创粉丝点击