Antenna Placement

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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 题目大意:用1*2的长条覆盖图中的“*”。问最少需要多少个长条。 解题思路:最小路径覆盖:选择最少的边,让每个顶点都被选中,单独的结点可以作为一条路径。建图思路都是奇偶性建图。但是不同的是,这个需要把所有“*”都覆盖。那么我们考虑最小路径覆盖。
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn = 660;
const int INF = 0x3f3f3f3f;
vector<int>G[maxn];
int Mx[maxn], My[maxn], dx[maxn], dy[maxn], used[maxn], dis;
char Map[maxn][maxn];
int lis[maxn][maxn];
bool SearchP(int _n){
queue<int>Q;
memset(dx,-1,sizeof(dx));
memset(dy,-1,sizeof(dy));
int dis = INF;
for(int i = 1; i <= _n; i++){
if(Mx[i] == -1){
dx[i] = 0;
Q.push(i);
}
}
int v;
while(!Q.empty()){
int u = Q.front(); Q.pop();
if(dx[u] > dis) break;
for(int i = 0; i < G[u].size(); i++){
v = G[u][i];
if(dy[v] == -1){
dy[v] = dx[u] + 1;
if(My[v] == -1){
dis = dy[v];
}else{
dx[My[v]] = dy[v] + 1;
Q.push(My[v]);
}
}
}
}
return dis != INF;
}
int dfs(int u){
int v;
for(int i = 0; i < G[u].size(); i++){
v = G[u][i];
if(!used[v] && dy[v] == dx[u] + 1){
used[v] = 1;
if(My[v] != -1 && dy[v] == dis){
continue;
}
if(My[v] == -1 || dfs(My[v])){
Mx[u] = v;
My[v] = u;
return true;
}
}
}
return false;
}
int MaxMatch(int ln,int rn){
int ret = 0;
memset(Mx,-1,sizeof(Mx));
memset(My,-1,sizeof(My));
while(SearchP(ln)){
memset(used,0,sizeof(used));
for(int i = 1; i <= ln; i++){
if(Mx[i] == -1 && dfs(i)){
ret++;
}
}
}
return ret;
}
int main(){
int T, cas = 0, n, m, N, M;
scanf("%d",&T);
while(T--){
n = m = 0;
scanf("%d%d",&N,&M);
for(int i = 0; i <= 210; i++){
G[i].clear();
}
for(int i = 0; i <= M+1; i++){
Map[N+1][i] = 'o';
Map[0][i] = 'o';
}
for(int i = 0; i <= N+1; i++){
Map[i][0] = 'o';
Map[i][M+1] = 'o';
}
for(int i = 1; i <= N; i++){
getchar();
for(int j = 1; j <= M; j++){
scanf("%c",&Map[i][j]);
if(Map[i][j] == '*'){
if((i+j)%2 == 0){
++n;
lis[i][j] = n;
if(Map[i-1][j] == '*'){
G[n].push_back(lis[i-1][j]);
}
if(Map[i][j-1] == '*'){
G[n].push_back(lis[i][j-1]);
}
}else{
++m;
lis[i][j] = m;
if(Map[i-1][j] == '*'){
G[lis[i-1][j]].push_back(m);
}
if(Map[i][j-1] == '*'){
G[lis[i][j-1]].push_back(m);
}
}
}
}
}
int res = MaxMatch(n,m);
printf("%d\n",n+m-res);
}
return 0;
}
/*
55
4 4
o***
oo*o
oooo
oooo */

  


最新文章

  1. Linux上mongodb开机自启动
  2. iOS 上架被拒原因保存
  3. JQuery选择器细节-遁地龙卷风
  4. boost.asio源码剖析(二) ---- 架构浅析
  5. ☀【Node】处理POST请求
  6. Oracle数据库字符串连接方法
  7. 常用加密算法的Java实现总结(二) ——对称加密算法DES、3DES和AES
  8. (转载)PHP 动态生成表格
  9. css行高line-height的用法(转)
  10. Matlab基本数据类型
  11. VB6.0快捷键大全(转)
  12. const形参与非const形参
  13. 窗口缩小div内容隐藏看不到怎么解决?
  14. 定位bug的姿势对吗?
  15. HTML基础系列
  16. 配置IIS网站,我遇到的那些坑~
  17. 【hdu 4658】Integer Partition (无序分拆数、五边形数定理)
  18. mysql建表规范及注意事项
  19. centos nfs配置备忘
  20. 首页的css

热门文章

  1. layui下select下拉框不显示或没有效果
  2. Git配置代理命令
  3. Hadoop的安装与配置
  4. JavaWeb中MVC的使用--以管理系统举例
  5. SSH—网上商城之商品图片文件上传
  6. django 基础框架学习 (二)
  7. 完美解决:"library not found for - "
  8. CF796C Bank Hacking 思维
  9. 阿里云服务器部署Tornado应用指南
  10. mpvue 小程序应用拖动排序