POJ3020

http://poj.org/problem?id=3020

                                                                     Antenna Placement

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11846   Accepted: 5826

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

題意比較複雜。就是圖上爲*的點需要覆蓋,但是每個覆蓋點被覆蓋後可以多覆蓋上下左右相鄰的點中的一個。用第一個樣例舉例。如果(5,2)被覆蓋,那麼可以選擇(5,1),(5,3),(6,2)【(4,2)不是需要被覆蓋的點】中的一個不用再被覆蓋。

把需要覆蓋點建立一個點集,相鄰的之間建一條邊,那麼題目就轉換成選取最少的邊將所有的邊覆蓋。即二分圖的最小點覆蓋問題。本題一定是個二分圖,因爲不可能出現奇數環。按題意,在座標上只有相鄰點可能有邊,不能走斜線,那麼環一定是個矩形,矩形一定是個偶數環。所以本體的圖建出來一定是個二分圖。

二分圖的最小邊覆蓋=點數-最大匹配數。

對於這種並沒有分成兩個點集的二分圖,建雙向邊,跑最大匹配,得到的結果是最大匹配的兩倍。最大匹配是X點集存對應的Y,這個相當於Y點集也存了對應的X,X與Y是一一對應的關係,所以是兩倍。

#include <cstdio>
#include <cstring>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
int line[410][410],cnt;
char map[45][15];
int map2[45][15],used[450],ans[450];
bool found(int x)
{
	for(int i=1;i<cnt;i++)
		if(line[x][i]&&!used[i])
		{
			used[i]=1;
			if(!ans[i]||found(ans[i]))
			{
				ans[i]=x;
				return 1;
			}
		}
	return 0;
}
int main()
{
	int t;
	scanf("%d",&t);
	int h,w;
	while(t--)
	{
		cnt=1;
		mem(line,0);
		mem(map2,0);
		scanf("%d%d",&h,&w);
		for(int i=1;i<=h;i++)
		{
			scanf("%s",map[i]+1);
			for(int j=1;j<=w;j++)
			{
				if(map[i][j]=='*')
				{
					map2[i][j]=cnt++;
					if(map2[i-1][j])
						line[map2[i][j]][map2[i-1][j]]=line[map2[i-1][j]][map2[i][j]]=1;
					if(map2[i][j-1])
						line[map2[i][j]][map2[i][j-1]]=line[map2[i][j-1]][map2[i][j]]=1;
				}
			}
		}
		mem(ans,0);
		int res=0;
		for(int i=1;i<cnt;i++)
		{
			mem(used,0);
			if(found(i))
				res++;	
		}
		printf("%d\n",cnt-res/2-1);	
	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章