zoukankan      html  css  js  c++  java
  • poj 3020Antenna Placement

    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

    当初初学的时候,没什么思路,推荐blog:http://blog.csdn.net/lyy289065406/article/details/6647040
    这讲的很清楚了。不献丑了。。
    View Code
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 using namespace std;
     6 int dx[]={-1,0,0,1};
     7 int dy[]={0,-1,1,0};
     8 int map[410][410],vis[410];
     9 int mx[410],my[410];
    10 int maxx,h,w;
    11 int dfs(int u)
    12 {
    13     int i;
    14     for(i=0;i<=maxx;i++)
    15     {
    16         if(!vis[i]&&map[u][i])
    17         {
    18             vis[i]=1;
    19             if(my[i]==-1||dfs(my[i]))
    20             {
    21                 my[i]=u;
    22                 mx[u]=i;
    23                 return 1;
    24             }
    25         }
    26     }
    27     return 0;
    28 }
    29 int hungary()
    30 {
    31     int cnt=0,i;
    32     for(i=0;i<=maxx;i++)
    33         if(mx[i]==-1)
    34         {
    35             memset(vis,0,sizeof(vis));
    36             if(dfs(i)) cnt++;
    37         }
    38     return cnt;
    39 }
    40 int main()
    41 {
    42     int t,x,y;
    43     int i,j,num,k;
    44     char s[45][15];
    45     scanf("%d",&t);
    46     while(t--)
    47     {
    48         maxx=num=0;
    49         memset(map,0,sizeof(map));
    50         memset(mx,-1,sizeof(mx));
    51         memset(my,-1,sizeof(my));
    52         scanf("%d%d",&h,&w);
    53         for(i=0;i<h;i++)
    54             scanf("%s",s[i]);
    55         for(i=0;i<h;i++)
    56             for(j=0;j<w;j++)
    57             {
    58                 if(s[i][j]=='*')
    59                 {
    60                     num++;
    61                     for(k=0;k<4;k++)
    62                     {
    63                         x=i+dx[k];
    64                         y=j+dy[k];
    65                         if(x>=0&&x<h&&y>=0&&y<w&&s[x][y]=='*')
    66                         {
    67                             map[w*i+j][w*x+y]=1;
    68                             if(maxx<w*i+j) maxx=w*i+j;
    69                             if(maxx<w*x+y) maxx=w*x+y;
    70                         }
    71                     }
    72                 }
    73             }
    74         //printf("%d\n",hungary());
    75         //printf("%d\n",num);
    76         printf("%d\n",num-hungary()/2);
    77     }
    78     return 0;
    79 }
  • 相关阅读:
    h5页面页面在iphoneX手机上底部会有留白解决办法
    自定义单张图片放大预览功能,可支持手势缩放,依赖jquery
    js事件内部有click事件时,click事件会重复调用解决方法
    h5页面通过阿里云的broswer-js-sdk上传文件
    python字符串前加r、f、u、l 的区别
    Python基础面试题 :计算列表中出现最多次的字符
    python基础入门教程:传参是传值还是传引用
    Python 面试题:输入一个数组,输出该数组的第二大的数字
    Python 7种超实用的数据清洗方法,这你一定要掌握
    python教程:3个非常有用的内置函数(filter/map/reduce)
  • 原文地址:https://www.cnblogs.com/wilsonjuxta/p/3008744.html
Copyright © 2011-2022 走看看