Asteroids (最小覆盖)
题目很简单,但是需要推到出二分图最大匹配 = 最小覆盖
最小覆盖:证明过程http://blog.sina.com.cn/s/blog_51cea4040100h152.html
Description
the grid.
Fortunately, Bessie has a powerful weapon that can vaporize all the asteroids in any given row or column of the grid with a single shot.This weapon is quite expensive, so she wishes to use it sparingly.Given the location of all the asteroids in the field, find
the minimum number of shots Bessie needs to fire to eliminate all of the asteroids.
Input
* Lines 2..K+1: Each line contains two space-separated integers R and C (1 <= R, C <= N) denoting the row and column coordinates of an asteroid, respectively.
Output
Sample Input
3 4
1 1
1 3
2 2
3 2
Sample Output
2
Hint
The following diagram represents the data, where "X" is an asteroid and "." is empty space:
X.X
.X.
.X.
OUTPUT DETAILS:
Bessie may fire across row 1 to destroy the asteroids at (1,1) and (1,3), and then she may fire down column 2 to destroy the asteroids at (2,2) and (3,2).
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
using namespace std;
int n;
int map[502][502];
bool visited[502];
int match[502]; bool find(int i) ///查找当前的i是否可以匹配
{
int j;
for(j=1;j<=n;j++)
{
if(map[i][j]&&!visited[j])
{
visited[j]=1;
if(match[j]==-1||find(match[j]))
{
match[j]=i;
return 1;
}
}
}
return 0;
}
int main()
{
int k,i,x,y,ans;
while(~scanf("%d%d",&n,&k))
{
ans=0;
memset(map,0,sizeof(map));
memset(match,-1,sizeof(match)); for(i=0;i<k;i++)//对有意思的进行初始化
{
scanf("%d%d",&x,&y);
map[x][y]=1;
}
for(i=1;i<=n;i++)
{
memset(visited,0,sizeof(visited));//开始标记为全部没有访问
if(find(i)) ans++;
}
printf("%d\n",ans);
}
return 0;
}
Asteroids (最小覆盖)的更多相关文章
- Asteroids(匈牙利算法入门)
Asteroids Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16211 Accepted: 8819 Descri ...
- POJ3041轰炸行星(匈牙利算法 最小覆盖点集)
Asteroids Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 25232 Accepted: 13625 Descr ...
- POJ2185Milking Grid(最小覆盖子串 + 二维KMP)
题意: 一个r*c的矩形,求一个子矩形通过平移复制能覆盖整个矩形 关于一个字符串的最小覆盖子串可以看这里http://blog.csdn.net/fjsd155/article/details/686 ...
- UVA 11419SAM I AM(输出 最小覆盖点 )
参考博客:如何找取 最小覆盖点集合 题意:R*C大小的网格,网格上面放了一些目标.可以再网格外发射子弹,子弹会沿着垂直或者水平方向飞行,并且打掉飞行路径上的所有目标,计算最小多少子弹,各从哪些位置发射 ...
- 二维KMP - 求字符矩阵的最小覆盖矩阵 - poj 2185
Milking Grid Problem's Link:http://poj.org/problem?id=2185 Mean: 给你一个n*m的字符矩阵,让你求这个字符矩阵的最小覆盖矩阵,输出这个最 ...
- KMP算法 - 求最小覆盖子串
KMP与最小覆盖子串 最小覆盖子串:对于某个字符串s,它的最小覆盖子串指的是长度最小的子串p,p满足通过自身的多次连接得到q,最后能够使s成为q的子串. 比如: 对于s="abcab&quo ...
- POJ 3041 Asteroids
最小点覆盖数==最大匹配数 Asteroids Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12678 Accepted: ...
- hdu 1240:Asteroids!(三维BFS搜索)
Asteroids! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- hdu---(1054)Strategic Game(最小覆盖边)
Strategic Game Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
随机推荐
- jQuery Mobile_表单元素
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- Knockout应用开发指南(完整版) 目录索引
http://learn.knockoutjs.com/ 所有示例和代码都在在上面直接运行预览 注意:因为它用了google的cdn加速,所要要用代_理+_翻_墙才能正常加载 使用Knockout有 ...
- php 根据指定的键对多维数组进行排序
根据指定的键对多维数组排序,可以按照多个键排序 我们通常有一些多维数组需要排序: $guys = array( array( 'name' => 'jake', 'score' => 80 ...
- 监控Linux性能的18个命令行工具
监控 Linux 性能的 18 个命令行工具 对于系统和网络管理员来说每天监控和调试Linux系统的性能问题是一项繁重的工作.在IT领域作为一名Linux系统的管理员工作5年后,我逐渐 认识到监控和保 ...
- wpa_supplicant移植
移植openssl-0.9.8za cp ../wpa_supplicant-2.5/patches/openssl-0.9.8za-tls-extensions.patch .patch -p1 & ...
- 共享内存是最快的一种IPC方式
在linux进程间通信的方式中,共享内存是一种最快的IPC方式.因此,共享内存用于实现进程间大量的数据传输,共享内存的话,会在内存中单独开辟一段内存空间,这段内存空间有自己特有的数据结构,包括访问权限 ...
- 236. Lowest Common Ancestor of a Binary Tree
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...
- Android Gradle实用技巧——APK文件名中加上SVN版本号,日期等
有时候,我们会希望能把APK文件名上带上打包日期,打包时svn的版本号,应用版本号等.当然这些也可以手动添加,但是手动的话也未免太不优雅了,而且可能会出错. 利用Gradle,我们可以让打包出来的ap ...
- (C# 基础) Datatable
增加一行: DataTable measurements = new DataTable(); measurements.Columns.Add("StationTestName" ...
- pthread_key_t和pthread_key_create()详解
pthread_key_t和pthread_key_create()详解 下面说一下线程中特有的线程存储, Thread Specific Data .线程存储有什么用了?他是什么意思了?大家都知道, ...