POJ 3020 Antenna Placement (最大匹配)

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<algorithm>
#include<string.h>
#include<stdio.h>
using namespace std;
int n,m,cut,head[410],id[45][15],carry,dis[410],vis[410];
int f[4][2]={0,1,1,0,0,-1,-1,0};
char str[45][15];
struct node
{
    int r,nextt;
}a[410*410];
void add(int x,int y)
{
    ++carry;
    a[carry].r=y;
    a[carry].nextt=head[x];
    head[x]=carry;
}
void init()
{
    cut=0,carry=0;
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++)
    {
        scanf("%s",str[i]);
        for(int j=0;j<m;j++)
        {
            if(str[i][j]=='*')
            {
                id[i][j]=++cut;
            }
        }
    }
    memset(head,0,sizeof(int)*cut+1);
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(str[i][j]=='*')
            for(int k=0;k<4;k++)
            {
                int x=f[k][0]+i;
                int y=f[k][1]+j;
                if(str[x][y]=='*')
                {
                    add(id[i][j],id[x][y]);
                }
            }
        }
    }
    return ;
}
int found(int u)
{
    int v=head[u];
    while(v)
    {
        int r=a[v].r;
        if(!vis[r])
        {
            vis[r]=1;
            if(!dis[r]||found(dis[r]))
            {
                dis[r]=u;
                return 1;
            }
        }
        v=a[v].nextt;
    }
    return 0;
}
int hun()
{
    int ans=0;
    memset(dis,0,sizeof(int)*cut+1);
    for(int i=1;i<=cut;i++)
    {
        memset(vis,0,sizeof(int)*cut+1);
        if(found(i)) ans++;
    }
    return ans;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        init();
        int ans=cut-hun()/2;
        printf("%d\n",ans);
    }
    return 0;
}

不知道错在哪的代码:第一遍打的代码,没过,打了第二遍(上面那个),就过了。绝望(方法有些略微不同)

#include<algorithm>
#include<string.h>
#include<stdio.h>
#define M 405
using namespace std;
int n,m,id[405][405],cut,carry,head[M*M],dis[M*M],vis[M*M];
int f[4][2]= {0,1,1,0,-1,0,0,-1};
char str[405][405];
struct node
{
    int r,nextt;
} a[M*M];
void add(int x,int y)
{
    a[carry].r=y;
    a[carry].nextt=head[x];
    head[x]=carry++;
}
void init()
{
    scanf("%d%d",&n,&m);
    cut=1,carry=1;
    memset(head,0,sizeof(head));
    memset(id,-1,sizeof(id));
    for(int i=0; i<n; i++)
    {
        scanf("%s",str[i]);
        for(int j=0; j<m; j++)
        {
            if(str[i][j]=='*')
            {
                id[i][j]=cut++;
            }
        }
    }
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<m; j++)
        {
            if(str[i][j]=='*')
                for(int k=0; k<4; k++)
                {
                    int x=i+f[k][0];
                    int y=j+f[k][1];
                    if(i>=0&&j>=0&&i<n&&j<m&&str[x][y]=='*')
                    {
                        add(id[i][j],id[x][y]);
                    }
                }
        }
    }
    return ;
}
int found(int u)
{
    int v=head[u];
    while(v)
    {
        int x=a[v].r;
        if(!vis[x])
        {
            vis[x]=1;
            if(!dis[x]||found(dis[x]))
            {
                dis[x]=u;
                dis[u]=x;
                return true;
            }
        }
        v=a[v].nextt;
    }
    return false;
}
int hun()
{
    int ans=0;
    memset(dis,0,sizeof(dis));
    for(int i=1; i<cut; i++)
    {
        if(!dis[i])
        {
            memset(vis,0,sizeof(vis));
            if(found(i)) ans++;
        }

    }
    return ans;
}
int main()
{
    int t;
    scanf("%d",&t);
    for(int ph=1; ph<=t; ph++)
    {
        init();
        int ans=hun();
        ans=cut-1-ans;
        printf("%d\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_41380961/article/details/81268152
今日推荐