(poj)3020 Antenna Placement 匹配
题目链接 : 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 匹配的更多相关文章
- 二分图最大匹配(匈牙利算法) POJ 3020 Antenna Placement
题目传送门 /* 题意:*的点占据后能顺带占据四个方向的一个*,问最少要占据多少个 匈牙利算法:按坐标奇偶性把*分为两个集合,那么除了匹配的其中一方是顺带占据外,其他都要占据 */ #include ...
- poj 3020 Antenna Placement(最小路径覆盖 + 构图)
http://poj.org/problem?id=3020 Antenna Placement Time Limit: 1000MS Memory Limit: 65536K Total Sub ...
- POJ 3020 Antenna Placement 【最小边覆盖】
传送门:http://poj.org/problem?id=3020 Antenna Placement Time Limit: 1000MS Memory Limit: 65536K Total ...
- POJ 3020 Antenna Placement【二分匹配——最小路径覆盖】
链接: http://poj.org/problem?id=3020 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
- POJ 3020——Antenna Placement——————【 最小路径覆盖、奇偶性建图】
Antenna Placement Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u S ...
- POJ 3020 Antenna Placement 最大匹配
Antenna Placement Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6445 Accepted: 3182 ...
- poj 3020 Antenna Placement(二分无向图 匈牙利)
Antenna Placement Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6438 Accepted: 3176 ...
- POJ 3020 Antenna Placement
Antenna Placement Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5645 Accepted: 2825 Des ...
- poj 3020 Antenna Placement (最小路径覆盖)
链接:poj 3020 题意:一个矩形中,有n个城市'*'.'o'表示空地,如今这n个城市都要覆盖无线,若放置一个基站, 那么它至多能够覆盖本身和相邻的一个城市,求至少放置多少个基站才干使得全部的城市 ...
随机推荐
- C#.NET数据库访问类DBHelper
这是一个与C# .NET通用的数据库访问类,包含了工厂模式.事务处理等安全机制. 调用方式: DBHelper db = new DBHelper(); DbCommand cmd = db.GetS ...
- 海量Office文档搜索
知识管理系统Data Solution研发日记之十 海量Office文档搜索 经过前面两篇文章的介绍,<分享制作精良的知识管理系统 博客备份程序 Site Rebuild>和<分 ...
- python(6)
python(6) 6.1 面向对象编程:OOP相信学过编程的就会了解它有多重要了.当然c什么的就算了 实际上面向对象就是把对象拥有的数据和方法封装在对象里面,由对象调用. 面向对象最重要的是类 ...
- Flume + HDFS + Hive日志收集系统
最近一段时间,负责公司的产品日志埋点与收集工作,搭建了基于Flume+HDFS+Hive日志搜集系统. 一.日志搜集系统架构: 简单画了一下日志搜集系统的架构图,可以看出,flume承担了agent与 ...
- 【转】二叉树 VS hashtable
hash_table和二叉搜索树都经常被用来构建符号表(或者字典)以及相关的结构,并且他们都表现出了很高的效率.最近也在不同的程序中使用了这两种数据结构,实现完毕后思考一下,对两者做了一个简单的比较: ...
- Indesign多媒体富交互插件【MagBuilder】与iOS app 【MagViewer】介绍
[写在前面]进园子有一段时间了,从来都是看别人的文章,自己的一点东西都记在本地笔记里,现在想把一些东西拿来出分享,希望能够认识一些志同道合的朋友和老师. 学习Adobe插件开发的初衷是为了给PS做插件 ...
- 全面解析java注解
一.注解概述 1.学习注解能够读懂别人的代码,特别是框架相关的代码 2.让自己的编程更加简洁,代码更加清晰 3.让别人高看一眼,会使用自定义注解来解决问题 ...
- iOS开发——图形编程Swift篇&CAShapeLayer实现圆形图片加载动画
CAShapeLayer实现圆形图片加载动画 几个星期之前,Michael Villar在Motion试验中创建一个非常有趣的加载动画. 下面的GIF图片展示这个加载动画,它将一个圆形进度指示器和圆形 ...
- android125 zhihuibeijing 缓存
## 三级缓存 ## - 内存缓存, 优先加载, 速度最快 - 本地缓存(内存卡), 次优先加载, 速度快 - 网络缓存, 不优先加载, 速度慢,浪费流量 package com.itheima.zh ...
- 说说log4cplus
<C++ primer 第五版>已经翻了一段时间了,每天早上的班车上看一个小时.书是好书,可惜很多知识还是停留在表面上.每天除了翻书,一是也找到不合适的方法进一步深入,晚上看到新闻联播的老 ...