poj3020-Antenna 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? 
poj3020-Antenna Placement_第1张图片

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
推荐指数:※※※
来源:http://poj.org/problem?id=3020
同样是匈牙利问题:http://blog.csdn.net/zhu_liangwei/article/details/11488809
最小路径覆盖=|P|-最大二分匹配。
难得建模的过程。
首先要对点进行编号,可以共享信号站的点之间有一条连线。
其次,对编号过的点,映射成二分图,又是一个过程。
还要注意:
1.使用邻接矩阵的时候,adj[i][j],j就表示了顶点,跟邻接表有区别。
2.点的总数不是当期矩阵的高度或宽度,而是映射之后,可以的点数h*w。
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
using namespace std;
const int N=500;
int adj[N][N];
int find(int index,int *visited,int *link,int n){
	int i,j;
	for(i=1;i<=n;i++){
		if(adj[index][i]!=0&&visited[i]==false){
			visited[i]=true;
			if(link[i]==-1||find(link[i],visited,link,n)==true){
				link[i]=index;
				return true;
			}
		}
	}
	return false;
}
int hung(int n){
	int i,count=0;
	int visited[N],link[N];
	memset(link,-1,sizeof(link));
	for(i=1;i<=n;i++){
		memset(visited,0,sizeof(visited));
		if(find(i,visited,link,n)==true)
			count++;
	}
	return count;
}
int main()
{
	int h,w,i,j;
	int sce;
	scanf("%d",&sce);
	while(sce--){
		int place[N][N];
		int num[N][N];
		memset(place,0,sizeof(place));
		memset(num,0,sizeof(num));
		int count=0;
		scanf("%d%d",&h,&w);
		for(i=0;i<h;i++){
			char s[	N];
			scanf("%s",s);
			for(j=0;j<w;j++){
				if(s[j]=='o')
					place[i][j]=0;
				else{
					place[i][j]=1;
					num[i][j]=++count;
				}
			}
		}
		memset(adj,0,sizeof(adj));
		for(i=0;i<h;i++){
			for(j=0;j<w;j++){
				if(i>0&&1==place[i-1][j])
					adj[num[i][j]][num[i-1][j]]=1;
				if(j>0&&1==place[i][j-1])
					adj[num[i][j]][num[i][j-1]]=1;
				if(i<h-1&&1==place[i+1][j])
					adj[num[i][j]][num[i+1][j]]=1;
				if(j<w-1&&1==place[i][j+1])
					adj[num[i][j]][num[i][j+1]]=1;
			}
		}
		int times=hung(count);
		printf("%d\n",count-times/2);
	}
	return 0;
}


 
 
 
 
 

你可能感兴趣的:(poj3020-Antenna Placement)