POJ 3020 Antenna Placement(二分图的最大匹配)

来源:互联网 发布:python监控系统性能 编辑:程序博客网 时间:2024/05/20 21:23

题意:一个矩形中,有N个城市’*’,其余网格都为'o'表示空地。现在这n个城市都要覆盖无线,若放置一个基站,那么它至多可以覆盖相邻的两个城市。 问至少放置多少个基站才能使得所有的城市都覆盖无线?这个基站的位置可以任意放,不管当前位置是’*’还是’o’.

思路跟HDU1507做的网格用1*2小矩阵覆盖的题目类似。

           我们把图中的每个’*’都标号,分别放到二分图的左右点集去。(行号+列号==偶数的放左边,行号+列号==奇数的放右边)。 如果存在两个相邻的’*’,那么就在他们之间连一条无向边(这条边肯定是连接左右点集的,因为网格是天然二分的)。这样就行了一个新的二分图G。求出最大匹配边数。(因为二分图的每条匹配边就是一条正好有效覆盖两个相邻’*’的矩形)。既然这样 我们最终需要的基站总数= ‘*’的总数-最大匹配边数(因为每条匹配边正好覆盖了2个’*’)


#include<cstdio>#include<cstring>#include<vector>using namespace std;const int maxn=1000+5;struct Max_Match{    int n,m;    vector<int> g[maxn];    bool vis[maxn];    int left[maxn];    void init(int n,int m)    {        this->n=n;this->m=m;        for(int i=1; i<=n; ++i) g[i].clear();        memset(left,-1,sizeof(left));    }    bool match(int u)    {        for(int i=0;i<g[u].size();++i)        {            int v=g[u][i];            if(!vis[v])            {                vis[v]=true;                if(left[v]==-1 || match(left[v]))                {                    left[v]=u;                    return true;                }            }        }        return false;    }    int solve()    {        int ans=0;        for(int i=1; i<=n; ++i)        {            memset(vis,0,sizeof(vis));            if(match(i)) ++ans;        }        return ans;    }}MM;int cas=1,T;int n,m;struct Node{int x,y;Node(){}Node(int x,int y):x(x),y(y){}}node1[maxn],node2[maxn];bool check(int i,int j){if (node1[i].x+1==node2[j].x && node1[i].y==node2[j].y)return true;if (node1[i].x-1==node2[j].x && node1[i].y==node2[j].y)return true;if (node1[i].x==node2[j].x && node1[i].y+1==node2[j].y)return true;if (node1[i].x==node2[j].x && node1[i].y-1==node2[j].y)return true;return false;}int main(){int k;scanf("%d",&T);    while(T--)    {scanf("%d%d",&n,&m);int num1=0,num2=0;        for (int i = 1;i<=n;i++)for (int j = 1;j<=m;j++){char c;scanf(" %c",&c);if (c == '*')  if ((i+j)%2==0) node1[++num1]=Node(i,j);  else node2[++num2]=Node(i,j);}MM.init(num1,num2);for (int i = 1;i<=num1;i++)for (int j = 1;j<=num2;j++)                if (check(i,j)){MM.g[i].push_back(j);}        printf("%d\n",num1+num2-MM.solve());}    return 0;}

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


0 0