Antenna Placement
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 12104   Accepted: 5954

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

Source

 
 
 // 题意:给一张图,图中有两种点,一次只能覆盖相邻(上下左右)两点
// 求最小覆盖数
// 二分图最小覆盖数等于最大匹配数,匈牙利算法求最大匹配数
// 此题建图方面有些繁琐,看了大佬的题解后,才建图成功 #include <cstdio>
#include <cstring> using namespace std; const int max_h = ;
const int max_w = ; int h,w;
char s[max_h][max_w]; int n;
int total=,save=,cur;
int direct[][]={{,},{-,},{,},{,-}}; const int max_n=+;
int cx[max_n],cy[max_n],st[max_n];
bool vis[max_n]; struct node
{
int y,nxt;
};
// 这里数组开的小了点,但是刚好够用了,大佬在题解中开了比这大百倍的数组。不知道为什么
// 这个数组的最大用量,应该由add函数的使用上限决定
// 每遍历一个点,最多在上下左右四个方向进行一次add函数的调用,所以只需要4*max_n次即可
node way[]; // 计算在一维数组中的位置
int get(int i,int j)
{
return i*w+j;
} void add(int u,int v)
{
++cur;
// 当前数组中存储邻接点v和st【u】
way[cur].y=v;
way[cur].nxt=st[u];
// st数组存储当u的边在数组中的位置
st[u]=cur;
} int match(int x)
{
for(int i=st[x];i;i=way[i].nxt)
{
int y=way[i].y;
if(!vis[y])
{
vis[y]=;
if(!cy[y] || match(cy[y]))
{
cx[x]=y;
cy[y]=x;
return ;
}
}
}
return ;
} int XYL()
{
memset(cx,,sizeof(cx));
memset(cy,,sizeof(cy));
int ans=;
for(int i=;i<n;++i)
{
// 遍历所有节点,如果找到没有匹配的x,看能否找到与之匹配的y
// 这里加了一步判断,对st[i]不为0的判断,也就是对当前节点不为'o'的判断
// 不加这一个判断也可得出正确的结果,但加入后会有优化,不用再执行之后许多无用的操作
// 毕竟只有'*'的点需要匹配不是吗?
if(!cx[i] && st[i])
{
memset(vis,,sizeof(vis));
ans+=match(i);
}
}
return ans;
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
// 输入数据
scanf("%d %d",&h,&w);
for(int i=;i<h;++i)
{
scanf("%s",s[i]);
} // 初始化way数组和st数组
cur=;
memset(st,,sizeof(st));
// n表示总节点数
n=h*w;
// total表示*的总数
total=; for(int i=;i<h;++i)
{
for(int j=;j<w;++j)
{
if(s[i][j]=='*')
{
++ total;
// 检查相邻四个方向
for(int k=;k<;++k)
{
int tx=i+direct[k][];
int ty=j+direct[k][];
// 在图的范围内且为*时,加边
if(tx>= && tx<h && ty>= && ty<w)
{
if(s[tx][ty]=='*')
{
int u=get(i,j);
int v=get(tx,ty);
add(u,v);
// printf("add");
}
}
}
}
}
} //printf("cur:%d\n",tot); printf("%d\n",total-XYL()/);
}
return ;
} /*
2
7 9
ooo**oooo
**oo*ooo*
o*oo**o**
ooooooooo
*******oo
o*o*oo*oo
*******oo
10 1
*
*
*
o
*
*
*
*
*
*
*/

Antenna Placement poj 3020的更多相关文章

  1. Antenna Placement POJ - 3020 (最小边集覆盖)

    Antenna Placement Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10699   Accepted: 526 ...

  2. Antenna Placement poj 3020(匹配)

    http://poj.org/problem?id=3020 题意:给定一个n*m的矩阵,'*'代表城市,现在想要用1*2的矩阵将所有的城市覆盖,问最少需要多少个矩阵? 分析:先为每个城市进行标号,再 ...

  3. (匹配 二维建图) Antenna Placement --POJ --3020

    链接: http://poj.org/problem?id=3020 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82834#probl ...

  4. (匹配)Antenna Placement --POJ --3020

    链接: http://poj.org/problem?id=3020 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82834#probl ...

  5. Antenna Placement POJ - 3020 二分图匹配 匈牙利 拆点建图 最小路径覆盖

    题意:图没什么用  给出一个地图 地图上有 点 一次可以覆盖2个连续 的点( 左右 或者 上下表示连续)问最少几条边可以使得每个点都被覆盖 最小路径覆盖       最小路径覆盖=|G|-最大匹配数 ...

  6. poj 3020 Antenna Placement(最小路径覆盖 + 构图)

    http://poj.org/problem?id=3020 Antenna Placement Time Limit: 1000MS   Memory Limit: 65536K Total Sub ...

  7. POJ 3020 Antenna Placement【二分匹配——最小路径覆盖】

    链接: http://poj.org/problem?id=3020 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

  8. POJ 3020 Antenna Placement 【最小边覆盖】

    传送门:http://poj.org/problem?id=3020 Antenna Placement Time Limit: 1000MS   Memory Limit: 65536K Total ...

  9. POJ 3020——Antenna Placement——————【 最小路径覆盖、奇偶性建图】

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

随机推荐

  1. 在4K屏下以超过VMWare默认的最高分辨率运行Linux系统

    前言 4K 屏,有其优点也有其弊端.优点就是分辨率高,字体和图标看起来如丝一般顺滑:缺点就是字体和图标小,费眼睛.解决这个缺点的方法也很简单粗暴,就是将系统的显示比例放大.在高分屏不很普及的时候,无论 ...

  2. jquery deferred 转载

    阮一峰的网络日志 » 首页 » 档案 JavaScript http://www.ruanyifeng.com/blog/2011/08/a_detailed_explanation_of_jquer ...

  3. Go语言实现:【剑指offer】把数组排成最小的数

    该题目来源于牛客网<剑指offer>专题. 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个.例如输入数组{3,32,321},则打印出这三个数字 ...

  4. 【TensorFlow】TensorFlow基础 —— 模型的保存读取与可视化方法总结

    TensorFlow提供了一个用于保存模型的工具以及一个可视化方案 这里使用的TensorFlow为1.3.0版本 一.保存模型数据 模型数据以文件的形式保存到本地: 使用神经网络模型进行大数据量和复 ...

  5. Python趣味入门02: 妥妥地安装配置Python(Windows版)

    < 上一篇:Python趣味入门01:你真的了解Python么? 本篇内容手把手教您如何去网上下载安装Python的运行环境,本文写于2020年Python稳定的版本是3.8,Windows流行 ...

  6. 详解SkipList跳跃链表【含代码】

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天继续介绍分布式系统当中常用的数据结构,今天要介绍的数据结构非常了不起,和之前介绍的布隆过滤器一样,是一个功能强大原理简单的数据结构.并且 ...

  7. [Effective Java 读书笔记] 第6章 枚举和注解

    第三十条 用enum代替int 总得来说,使用enum有几点好处 1.编译时的类型安全, 2.可以保证就是自己定义的值,不会有月结风险, 3.每个枚举类型有自己的命名空间 4.枚举可以添加任意的方法和 ...

  8. MongoDB 复本集搭建

    复制集的特点   数据一致性 主是唯一的,但不是固定的  没有MySQL那样的双主结构 大多数原则,集群存活节点小于等于二分之一时集群不可写,只可读. 是否能选举出新的主节点,是由当前复制集成员存活量 ...

  9. 解决Python2.7的UnicodeEncodeError: 'ascii' codec can't encode异常错误

    UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128) ...

  10. xmake v2.3.1 发布, 无缝对接其他构建系统

    最近对xmake内部做了不少的重构来改进,并且新增了不少实用的新特性,欢迎来体验. 项目源码 官方文档 一些新特性: 一键编译其他构建系统维护的项目,实现无缝对接,并且支持交叉编译(比如autotoo ...