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
题目的大意就是在一个矩形中有一些城市和空地(’ * ‘代表城市,’ o '代表空地),要把所有的城市都覆盖无线,一个基站能覆盖两个相邻的城市,问最少需要多少个基站才可以让所有的城市都覆盖无线。
这题的思路其实和hdu-4185类似,可以先把所有的城市都当成一个点,然后寻找是否存在相邻的点,存在的话就给它们连边,求出最大匹配数,因为匹配好的每条边肯定都是覆盖相邻的两个点,所以最后要求的 最少基站的个数就等于总城市数-边的最大匹配。
要注意的是比如说出现***
三个城市都相邻的话,一个基站只能覆盖两个相邻城市,要么是前面两个,要么是后面两个,题目要看清楚。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<iostream>
#include<algorithm>
using namespace std;
#define maxn 1100
#define ll long long
#define inf 0x3f3f3f3f
#define mod 998244353
char mp[maxn][maxn];
int biao[maxn][maxn],vis[maxn],match[maxn];
int dic[4][2]= {0,1,1,0,-1,0,0,-1};
vector<int> v[maxn];
int n,m;
int dfs(int u)
{
for(int i=0; i<v[u].size(); i++)
{
int k=v[u][i];
if(!vis[k])
{
vis[k]=1;
if(!match[k]||dfs(match[k]))
{
match[k]=u;
return 1;
}
}
}
return 0;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(biao,0,sizeof(biao));
memset(match,0,sizeof(match));
for(int i=0; i<maxn; i++)
v[i].clear();
int cnt=1;
scanf("%d %d",&n,&m);
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
scanf(" %c",&mp[i][j]);
if(mp[i][j]=='*') biao[i][j]=cnt++;
}
}
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
if(mp[i][j]=='*')
{
for(int q=0; q<4; q++)
{
int dx=i+dic[q][0];
int dy=j+dic[q][1];
if(dx>=0&&dx<n&&dy>=0&&dy<m&&mp[dx][dy]=='*')
{
v[biao[i][j]].push_back(biao[dx][dy]);
}
}
}
}
}
int ans=0;
for(int i=1; i<=cnt; i++)
{
memset(vis,0,sizeof(vis));
if(dfs(i)) ans++;
}
cnt-=1;
printf("%d\n",cnt-ans/2);
}
return 0;
}