poj 3041 Asteroids (最小点覆盖)
Asteroids
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 26239 | Accepted: 14182 |
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.
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.
* 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
C/C++:
1 #include <map> 2 #include <queue> 3 #include <cmath> 4 #include <vector> 5 #include <string> 6 #include <cstdio> 7 #include <cstring> 8 #include <climits> 9 #include <iostream> 10 #include <algorithm> 11 #define INF 0xffffff 12 using namespace std; 13 const int my_max = 505; 14 15 int n, k, a, b, my_ans, my_line[my_max][my_max], my_book[my_max], my_right[my_max]; 16 17 bool my_dfs(int x) 18 { 19 for (int i = 1; i <= n; ++ i) 20 { 21 if (!my_book[i] && my_line[x][i]) 22 { 23 my_book[i] = 1; 24 if (!my_right[i] || my_dfs(my_right[i])) 25 { 26 my_right[i] = x; 27 return true; 28 } 29 } 30 } 31 return false; 32 } 33 34 int main() 35 { 36 while (~scanf("%d%d", &n, &k)) 37 { 38 my_ans = 0; 39 memset(my_line, 0, sizeof(my_line)); 40 memset(my_right, 0, sizeof(my_right)); 41 42 while (k --) 43 { 44 scanf("%d%d", &a, &b); 45 my_line[a][b] = 1; 46 } 47 for (int i = 1; i <= n; ++ i) 48 { 49 memset(my_book, 0, sizeof(my_book)); 50 if (my_dfs(i)) my_ans ++; 51 } 52 53 printf("%d\n", my_ans); 54 } 55 return 0; 56 }