博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 3041——Asteroids
阅读量:6851 次
发布时间:2019-06-26

本文共 2462 字,大约阅读时间需要 8 分钟。

                    poj       3041——Asteroids
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 22604   Accepted: 12247

Description

Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N grid (1 <= N <= 500). The grid contains K asteroids (1 <= K <= 10,000), which are conveniently located at the lattice points of 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

* Line 1: Two integers N and K, separated by a single space. 
* 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

* Line 1: The integer representing the minimum number of times Bessie must shoot.

Sample Input

3 41 11 32 23 2

Sample Output

2

Hint

INPUT DETAILS: 
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).

Source

题目大意
给你一个N*N的矩阵,有一些格子里有小行星,现在Bessie有一些威力很大的炮弹,每一次射击都能够消灭掉矩阵中一行或一列的小行星,
但是炮弹很贵,问你需要消灭掉所有小行星所需的最小炮弹数目。
思路:
这道题和上一道题的思路是一样的,我们只需要将每一行和每一列都当做是二分图的一个集合,然后在进行二分图匹配。
这道题就是一个裸的求最小点覆盖的题。在二分图中,最小点覆盖=最大匹配数。so,着我们就直接求最大匹配数就好了。
代码:
 
#include
#include
#include
#include
#define N 550using namespace std;bool vis[N];int n,m,x,y,ans,pre[N],map[N][N];int read(){ int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){
if(ch=='-') f=-1; ch=getchar();} while(ch<='9'&&ch>='0'){x=x*10+ch-'0'; ch=getchar();} return x*f;}int find(int x){ for(int i=1;i<=n;i++) if(!vis[i]&&map[x][i]) { vis[i]=true; if(!pre[i]||find(pre[i])) { pre[i]=x; return 1; } } return 0;}int main(){ n=read(),m=read(); for(int i=1;i<=m;i++) x=read(),y=read(),map[x][y]=1; for(int i=1;i<=n;i++) { memset(vis,0,sizeof(vis)); if(find(i)) ans++; } printf("%d",ans); return 0;}
 

 

 

转载于:https://www.cnblogs.com/z360/p/7105069.html

你可能感兴趣的文章
MySQL ibdata多路径扩容
查看>>
[差分][栈]JZOJ 4209 已经没有什么好怕的了awa
查看>>
2019寒假纪中总结
查看>>
DH02-策略模式
查看>>
poj 1094 Sorting It All Out
查看>>
配置Instantclient
查看>>
【转】程序员必读书单 1.0
查看>>
编译安装 nginx的http_stub_status_module监控其运行状态
查看>>
AtCoder Regular Contest 069 D
查看>>
关于sql server批量插入与更新两种解决方案
查看>>
判断 iPhone 是否插入了 SIM 卡
查看>>
PUSH
查看>>
格式话输出
查看>>
黄聪:微信小程序 服务器 TLS1.0 1TLS.2 配置详细教学!
查看>>
黄聪:360浏览器、chrome开发扩展插件教程(3)关于本地存储数据
查看>>
(转)java 中变量存储位置总结
查看>>
系统架构介绍
查看>>
oracle唯一约束
查看>>
C++获取类成员函数地址
查看>>
【MOOC手写体】王文敏教授.《人工智能原理》 第10章 机器学习的任务 Part5 C10.2 Classification 分类...
查看>>