Asteroids(最小点覆盖)

Asteroids
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 18183   Accepted: 9905

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 4
1 1
1 3
2 2
3 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.
题解:
题意就是说,一个飞船在飞,N*N的网格里面有障碍,一个子弹可以打穿一行或者一列,让找消除所有障碍的最小子弹
代码:
 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<vector> 
 7 #define mem(x,y) memset(x,y,sizeof(x))
 8 using namespace std;
 9 const int INF=0x3f3f3f3f;
10 const int MAXN=1010;
11 vector<int>vec[MAXN];
12 int usd[MAXN],vis[MAXN];
13 bool dfs(int x){
14     for(int i=0;i<vec[x].size();i++){
15         int v=vec[x][i];
16             if(!vis[v]){
17                 vis[v]=1;
18             if(usd[v]==-1||dfs(usd[v])){
19                 usd[v]=x;return true;
20             }
21         }
22     }
23     return false;
24 }
25 int main(){
26     int N,K,R,C;
27     while(~scanf("%d%d",&N,&K)){
28         mem(vis,0);mem(usd,-1);
29         for(int i=1;i<=N;i++)vec[i].clear();
30         while(K--){
31             scanf("%d%d",&R,&C);
32             vec[R].push_back(C);
33         }
34         int ans=0;
35         for(int i=1;i<=N;i++){
36             mem(vis,0);
37             if(dfs(i))ans++;
38         }
39         printf("%d
",ans);
40     }
41     return 0;
42 }
原文地址:https://www.cnblogs.com/handsomecui/p/4958738.html