Poj 3020 antenna placement

Source: Internet
Author: User
Antenna placement Time Limit: 1000 msmemory limit: 65536 K Total submissions: 6342 accepted: 3131Description


The global aerial research centre has been allotted the task of building the maximum th 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 4 dair, 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 4 Dair antennas depicted by ellipses covering them.
 
Obviusly, 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. when an antenna is PLA CED 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 particle 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
10 1
*
*
*
O
*
*
*
*
*
*
Sample output


17

5


Composition !!!!!

Sort each city and then determine the relationship between cities. It can be known as a undirected bipartite graph !!

Use the formula of the undirected bipartite graph's minimum path overwrite = number of vertices-maximum bipartite matching/2;


The AC code is as follows:


#include<iostream>#include<cstring>using namespace std;int w[405][405];int vis[405],s[405];char map[45][15];int v[45][15];int tt;int xyl(int a){    int i,j;    for(i=1;i<tt;i++)    {        if(w[a][i]&&!vis[i])        {            vis[i]=1;            if(s[i]==0||xyl(s[i]))            {                s[i]=a;                return 1;            }        }    }    return 0;}int main(){    int t;    int n,m;    int i,j;    cin>>t;    while(t--)    {        memset(w,0,sizeof w);        cin>>n>>m;        tt=1;        for(i=0;i<n;i++)        {            cin>>map[i];            for(j=0;j<m;j++)                if(map[i][j]=='o')                v[i][j]=0;                else                    v[i][j]=tt++;        }        for(i=0;i<n;i++)        {            for(j=0;j<m;j++)            {                //cout<<v[i][j]<<" ";                if(v[i-1][j]!=0&&i>=1)                    w[v[i][j]][v[i-1][j]]=1;                if(v[i+1][j]!=0&&i+1<n)                    w[v[i][j]][v[i+1][j]]=1;                if(v[i][j-1]!=0&&j>=1)                    w[v[i][j]][v[i][j-1]]=1;                if(v[i][j+1]!=0&&j+1<m)                    w[v[i][j]][v[i][j+1]]=1;            }            //cout<<endl;        }        int ans=0;        memset(s,0,sizeof s);        for(i=1;i<tt;i++)        {            memset(vis,0,sizeof vis);            if(xyl(i))                ans++;        }        cout<<tt-1-ans/2<<endl;    }    return 0;}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.