题目链接 : http://poj.org/problem?id=3020

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+,r),(c,r+),(c-,r), or (c,r-), 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  <= h <=  and  < w <= . 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 ooo**oooo
**oo*ooo*
o*oo**o**
ooooooooo
*******oo
o*o*oo*oo
*******oo *
*
*
o
*
*
*
*
*
*
Sample Output

题意:一个n*m的方阵 一个雷达可覆盖两个*,一个*可与四周的一个*被覆盖,一个*可被多个雷达覆盖问至少需要多少雷达能把所有的*覆盖

方法:把每个*变成数字,查看这些数字可与那些数字相连,然后用二分匹配,把能连得点匹配成对

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<queue>
#include<stdlib.h>
#define INF 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof(a));
#define ll long long
using namespace std;
const int N = ;
int dis[][]={{,},{,},{-,},{,-}};
int cot;
int vis[N],d[N],a[N][N];
int G[N][N];
char str[N][N];
int fin(int s)
{
for(int i=;i<=cot;i++)
{
if(!vis[i] && G[s][i])
{
vis[i]=;
if(!d[i] || fin(d[i]))
{
d[i]=s;
return ;
} }
}
return ;
}
int main()
{
int t,n,m,e,f;
scanf("%d",&t);
while(t--)
{
cot=;met(a,);met(G,);
scanf("%d %d",&n,&m);
for(int i=;i<n;i++)
{
scanf("%s",str[i]);
for(int j=;j<m;j++)
{
if(str[i][j]=='*')
a[i][j]=++cot;
}
}
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
if(str[i][j]=='*')
{
e=a[i][j];
for(int k=;k<;k++)
{
int x=i+dis[k][];
int y=j+dis[k][];
if(x>=&&x<n&&y>=&&y<m&&str[x][y]=='*')
{
f=a[x][y];
G[e][f]=G[f][e]=;
}
}
}
}
}
met(d,);
int ans=;
for(int i=;i<=cot;i++)
{
met(vis,);
if(fin(i))
ans++;
}
printf("%d\n",cot-ans/);
}
return ;
}

还有一种方法

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
#include <math.h>
#include <queue>
#include <vector>
using namespace std;
#define N 500
#define INF 0x3f3f3f3f
#define ll long long
#define met(a,b) memset(a,b,sizeof(a));
vector<vector<int> >Q;
int dis[][]={{,},{,},{-,},{,-}};
struct node
{
int v,next;
}Map[N<<];
int a[][],vis[N<<],used[N<<];
char str[][];
int s[N<<],l;
void add(int e,int f)
{
Map[l].v=f;
Map[l].next=s[e];
s[e]=l++;
}
int pan(int u)
{
for(int i=s[u];i!=-;i=Map[i].next)
{
int v=Map[i].v;
if(!vis[v])
{
vis[v]=;
if(!used[v] || pan(used[v]))
{
used[v]=u;
return ;
}
}
}
return ;
}
int main()
{
int t,cot,n,m;
scanf("%d",&t);
while(t--)
{
cot=;met(s,-);l=;met(a,);
scanf("%d %d",&n,&m);
for(int i=; i<n; i++)
{
scanf("%s",str[i]);
for(int j=; j<m; j++)
{
if(str[i][j]=='*')
a[i][j]=cot++;
}
}
for(int i=; i<n; i++)
{
for(int j=; j<m; j++)
if(str[i][j]=='*')
{
for(int k=; k<; k++)
{
int x=i+dis[k][];
int y=j+dis[k][];
if(x>= && x<n && y>=&&y<m&&str[x][y]=='*')
add(a[i][j],a[x][y]);
} }
}
met(used,);int sum=;
for(int i=;i<cot;i++)
{
met(vis,);
sum+=pan(i);
}
printf("%d\n",cot--sum/);
}
return ;
}

(poj)3020 Antenna Placement 匹配的更多相关文章

  1. 二分图最大匹配(匈牙利算法) POJ 3020 Antenna Placement

    题目传送门 /* 题意:*的点占据后能顺带占据四个方向的一个*,问最少要占据多少个 匈牙利算法:按坐标奇偶性把*分为两个集合,那么除了匹配的其中一方是顺带占据外,其他都要占据 */ #include ...

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

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

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

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

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

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

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

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

  6. POJ 3020 Antenna Placement 最大匹配

    Antenna Placement Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6445   Accepted: 3182 ...

  7. poj 3020 Antenna Placement(二分无向图 匈牙利)

    Antenna Placement Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6438   Accepted: 3176 ...

  8. POJ 3020 Antenna Placement

    Antenna Placement Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5645 Accepted: 2825 Des ...

  9. poj 3020 Antenna Placement (最小路径覆盖)

    链接:poj 3020 题意:一个矩形中,有n个城市'*'.'o'表示空地,如今这n个城市都要覆盖无线,若放置一个基站, 那么它至多能够覆盖本身和相邻的一个城市,求至少放置多少个基站才干使得全部的城市 ...

随机推荐

  1. android studio简易了解第一部分

    1.如果还没下载 jdk,先把jdk下载,然后下载android studio 安装 百度 android studio ,百度软件中心可以下载. Android官网可以下载:     网址: htt ...

  2. Java向PostgreSQL发送prepared statement 与 libpq 向PostgreSQL发送prepared statement之比较:

    Java 代码,在数据库端,并没有当成 prepared statetment 被处理. C代码通过libpq 访问数据库端,被当成了 prepared statement 处理.也许是因Postgr ...

  3. SQLSERVER中返回修改后的数据

    在公司看到同事写了个SQL2005的新特性的文章,觉得很实用,在这里和大家分享下. 这种技术主要是用到了inserted和deleted虚拟表,这两张表相信大家都很熟悉.以前我们主要是在触发器中使用. ...

  4. nonce和timestamp在Http安全协议中的作用

    前段时间给客户网站做新浪微博账号登录功能,对OAuth协议以及相关的一些安全协议做了一些研究,顺便就记录一下学习心得吧.在这里就不打算具体讲OAuth的协议流程了,而是针对OAuth请求头里的nonc ...

  5. SQL Sever——无法连接到(local)。“未配置远程连接”和“请求失败或服务未及时响应”

       攻克了上篇博客提到的"远程过程调用失败(0x800706be)"的问题. 新的问题接踵而至. . . 一.       watermark/2/text/aHR0cDovL2 ...

  6. Graph(2014辽宁ACM省赛)

    问题 F: Graph 时间限制: 1 Sec  内存限制: 128 MB 提交: 30  解决: 5 [cid=1073&pid=5&langmask=0" style=& ...

  7. ShowcaseView-master

      ShowcaseView.rar

  8. Android 导入导出CSV,xls文件 .

    1 . http://www.bangchui.org/read.php?tid=62 2 .http://blog.csdn.net/xinzheng_wang/article/details/77 ...

  9. Ambiguous mapping found. Cannot map 'xxxxController' bean method

    1.背景 今天要做一个demo,从github上clone一个springmvc mybatis的工程(https://github.com/komamitsu/Spring-MVC-sample-u ...

  10. JVMInternals--reference

    This article explains the internal architecture of the Java Virtual Machine (JVM). The following dia ...