POJ-3020 Antenna Placement

Antenna Placement
题目链接
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 10466 Accepted: 5171
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
Source

Svenskt M?sterskap i Programmering/Norgesmesterskapet 2001

题目大意
给出一幅 n×m 的图,图上的点分为两类(两种字符表示),‘*’ 和 ‘o’,其中 ‘*’ 表示必须有雷达覆盖的点。
每个雷达可以覆盖一个点和一个与这个点相邻的点。问最少需要多少个雷达。
n≤40,m≤10

扫描二维码关注公众号,回复: 915209 查看本文章

题解
这题有很多解法,我想到的是二分图匹配。
我们把点分为两类,分类依据为它们的坐标的奇偶性,奇数一类,偶数一类,因为每对能匹配的点,x坐标+y坐标的奇偶性肯定不同。
所以我们就对每个要求覆盖点,向及其周围的要求覆盖点建边,然后刷一趟二分图最大匹配就好了。

其他解法:
我在POJ上浏览了一下,还有用网络流解的、状压DP解的、和一种利用 “带花树开花算法” 的解法。

不过状压DP的方法,感觉写起来很麻烦,不如二分图匹配来得简单。

代码

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=55,maxm=55,flg[4][2]={{1,0},{0,1},{-1,0},{0,-1}},TT=50;
int T,n,m,mp[maxn][maxm],x,y,use[maxn][maxm],now[maxn][maxm],ans;
struct js{
    int x,y;
    void p(int a,int b){x=a;y=b;}
}a[405];
bool pd(int x,int y)
{
    if (x<1||x>n||y<1||y>m) return 0;
    return 1;
}
int Cgt(){char ch=getchar();while(ch!='*'&&ch!='o')ch=getchar();return ch=='*';}
bool dfs(int x,int y)
{
    for (int k=0;k<4;k++)
    {
        int X=x+flg[k][0],Y=y+flg[k][1];
        if (pd(X,Y)&&!use[X][Y]&&mp[X][Y])
        {
            int &s=now[X][Y];use[X][Y]=1;
            if (!s||dfs(s/TT,s%TT))
            {
                s=x*TT+y;return 1;
            }
        }
    }
    return 0;
}
int main()
{
    scanf("%d",&T);
    for (int t=1;t<=T;t++)
    {
        scanf("%d%d",&n,&m);
        x=y=0;
        memset(a,0,sizeof a);
        memset(mp,0,sizeof mp);
        for (int i=1;i<=n;i++)
         for (int j=1;j<=m;j++)
           if (mp[i][j]=Cgt())
             if ((i^j)&1) a[++x].p(i,j);else y++;
        memset(now,0,sizeof now);
        ans=x+y;
        for (int i=1;i<=x;i++)
        {
            memset(use,0,sizeof use);
            if (dfs(a[i].x,a[i].y)) ans--;
        }
        printf("%d\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/xu0_zy/article/details/80054170