POJ3020

Antenna Placement

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3167   Accepted: 1510

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. 
POJ3020 
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

Source

解题思路:相邻的点构成二分图,
              二分图有两倍的相邻的点,求最大匹配,匈牙利算法
              二倍的最大匹配---cal-match
              输入最大匹配的点安放一个天线,没有在最大匹配的点安放一个天线
               match+total-2*match=total-match=total- cal-match/2
#include <iostream>

#include <cstdio>

#include <cstring>



using namespace std;



int h,w;

int result[801];

bool graph[801][801];

bool visit[801];

int cnt;



bool find(int startNode)

{

    for(int i=1; i<=cnt; i++)

    {

        if(!visit[i] && graph[startNode][i])

        {

            visit[i]=true;

            if(result[i]==0 || find(result[i]))

            {

                result[i]=startNode;

                return true;

            }

        }

    }

    return false;

}

int main()

{

    int n;

    char ch;

	int map[50][30];

	int answer;



	cin>>n;



	while(n--)

	{

		//init

		cin>>h>>w;

		cnt=0;

		memset(map,0,sizeof(map));

		for(int r=1; r<=h; r++)

		{

		    for(int c=1; c<=w; c++)

			{

			    cin>>ch;

			    if(ch=='*')

			    {

			        cnt++;

			        map[r][c]=cnt;

			    }

			    else

			    {

			        map[r][c]=0;

			    }

			}

			getchar();

		}

		memset(graph,0,sizeof(graph));

		memset(result,0,sizeof(result));



        //init binary graph

		for(int r=1; r<=h; r++)

		{

		    for(int c=1; c<=w; c++)

			{

			    if(map[r][c]!=0)

			    {

                    if(map[r-1][c]!=0)  //up

                    {

                        graph[map[r][c]][map[r-1][c]]=true;

                       // graph[map[r-1][c]][graph[r][c]]=true;

                    }

                    if(map[r+1][c]!=0)  //down

                    {

                        graph[map[r][c]][map[r+1][c]]=true;

                        //graph[map[r+1][c]][graph[r][c]]=true;

                    }

                    if(map[r][c-1]!=0)  //left

                    {

                        graph[map[r][c]][map[r][c-1]]=true;

                      //  graph[map[r][c-1]][graph[r][c]]=true;

                    }

                    if(map[r][c+1]!=0)  //right

                    {

                        graph[map[r][c]][map[r][c+1]]=true;

                       // graph[map[r][c+1]][graph[r][c]]=true;

                    }

			    }

			}

		}

		answer=0;

		for(int i=1; i<=cnt; i++)

		{

		    memset(visit,0,sizeof(visit));

		    if(find(i))

		        answer++;



		}

		cout<<cnt-answer/2<<endl;



	}

	return 0;

}



你可能感兴趣的:(poj)