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
题意:一个n*m的平面内有一些点,有1*2的纸条,可以横放或竖放,求最少用多少张纸条才能覆盖所有点?
题解:纸条相当于一条边,上道题求得为覆盖所有边的最小点数,这道题则逆其道而行,可转化为覆盖所有点的最小边数,即最小路径覆盖
二分图中最小路径覆盖=点数-最小边覆盖
然后就可以用匈牙利跑了~
代码如下:
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; vector<int> g[];
int vis[],link[];
int map[][],n,m,cnt,ttt;
char c[][]; int dfs(int x)
{
int sz=g[x].size();
for(int k=;k<sz;k++)
{
int y=g[x][k];
if(!vis[y])
{
vis[y]=;
if(!link[y]||dfs(link[y]))
{
link[y]=x;
return ;
}
}
}
return ;
} int search()
{
memset(link,,sizeof(link));
int tmp=;
for(int i=;i<=cnt;i++)
{
if(dfs(i))
{
memset(vis,,sizeof(vis));
tmp++;
}
}
return tmp;
} int main()
{
scanf("%d",&ttt);
while(ttt--)
{
cnt=;
scanf("%d%d",&n,&m);
memset(map,,sizeof(map));
for(int i=;i<=;i++)
{
g[i].clear();
}
for(int i=;i<=n;i++)
{
scanf("%s",c[i]);
for(int j=;j<m;j++)
{
if(c[i][j]=='o')
{
map[i][j+]=;
}
else
{
map[i][j+]=++cnt;
}
}
} for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
if(map[i][j])
{
if(map[i][j+])
{
g[map[i][j]].push_back(map[i][j+]);
}
if(map[i][j-])
{
g[map[i][j]].push_back(map[i][j-]);
}
if(map[i+][j])
{
g[map[i][j]].push_back(map[i+][j]);
}
if(map[i-][j])
{
g[map[i][j]].push_back(map[i-][j]);
}
}
}
}
int ans=search();
int x=cnt-ans/;
printf("%d\n",x);
}
}

 

最新文章

  1. LoadRunner之篇
  2. C#~异步编程在项目中的使用
  3. ie7下&lt;a href=&quot;javascript:;&quot;&gt;标签不反应
  4. Redis学习笔记七:独立功能之排序
  5. iOS开发——高级篇——地图 MapKit
  6. uGUI练习(七) Drag And Drop
  7. MySQL逻辑备份与恢复
  8. jquery easyui Combobox 实现 两级联动
  9. Java初始化(成员变量)
  10. 关于在asp.net的web页面中的全局变量问题
  11. Android01--开发环境搭建
  12. oracle 排序
  13. sublimeText3安装package control和禁止弹出更新下载弹窗
  14. Java中数组的初始化方式
  15. VS2008下直接安装使用Boost库1.46.1版本号
  16. Spring MVC+JSP实现三级联动
  17. Android NDK开发及调用标准linux动态库.so文件
  18. EF的join用法
  19. Linux下PHP连接MS SQLServer的办法
  20. yii pageTitle与Yii::app()-&gt;name的区别

热门文章

  1. JAVA中常见异常类
  2. ubutun
  3. Yii2 Post请求的时候出现400错误
  4. java中二维数组求最大值代码。。。。。。不懂呀
  5. 1115 Counting Nodes in a BST
  6. spring-boot多环境配置文件
  7. Vue 简单的总结四(项目流程,DIY脚手架、vue-cli的使用)
  8. MySQL高可用MHA实战
  9. java使用POST发送soap报文请求webservice返回500错误解析
  10. C#获取访问者ip和获取本机ip地址