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 题意:‘*’代表城市 'o'代表空地 要求你用最小的无线电雷达来覆盖所有的城市 雷达只可以建立在城市上 每一个雷达,只可以覆盖它自己所在的位置和与它相邻的一个位置 【上 || 下 || 左】 注意:也就是说一个雷达只可以覆盖两个城市
思路:
首先,可以用最大匹配的思想来想, 题目可以这样理解, 用一个1*2跟1*1的矩形来套*,不得相交,问最小几个矩形,那肯定是先所有1*2矩形套,剩下的1*1了, 前者就是二分图最大匹配呀~跟HDU 4185一样,假设得出的最大匹配是 x,那么一共2*x个点, n-2*x就是剩下的点, 所以答案就是n-x;
还可以用最小边覆盖来想:
看到是两两连边不由得想起二分图匹配,实际上就是用最少的边覆盖所有的点的问题,我觉得最小边覆盖实际上就是最小路径覆盖的一个特殊情况,只不过要求必须是二分图才行而最小路径覆盖只要是PxP的有向图就行。 而他们的公式都是一样的,都等于最大独立集数
实质是一样的。。
代码:(因为我建的是无向图,所以最大匹配/2)
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <vector> using namespace std; const int maxn = 505; vector<int> v[maxn*maxn]; int n, m, k, book[maxn*maxn], match[maxn*maxn], dir[4][2] = {1,0,-1,0,0,1,0,-1}, id[maxn][maxn]; char maze[maxn][maxn]; int Find(int x) { for(int i = 0; i < v[x].size(); i++) { int to = v[x][i]; if(book[to]) continue; book[to] = 1; if(!match[to] || Find(match[to])) { match[to] = x; return 1; } } return 0; } int main() { int t; cin >> t; while(t--) { int x, y, ans = 0; memset(match, 0, sizeof(match)); memset(id, 0, sizeof(id)); for(int i = 0; i < maxn; i++) v[i].clear(); scanf("%d%d", &n, &m); int cnt = 1; for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) { scanf(" %c", &maze[i][j]); if(maze[i][j] == '*') id[i][j] = cnt++; } for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { if(maze[i][j] == '*') { for(int k = 0; k < 4; k++) { int tx = i + dir[k][0]; int ty = j + dir[k][1]; if(tx >= 0 && tx < n && ty >= 0 && ty < m) v[id[i][j]].push_back(id[tx][ty]); } } } } for(int i = 1; i <= n*m; i++) { memset(book, 0, sizeof(book)); ans += Find(i); } printf("%d\n", cnt-1-ans/2); } return 0; }