Asteroids
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 18289 |
|
Accepted: 9968 |
Description
Bessie wants to navigate she spaceship through a dangerous asteroid field in the shape of an n x N grid (1 <= N <= 5 ). The grid contains K asteroids (1 <= k <=), which is conveniently located at the lattice points of the G Rid.
Fortunately, Bessie have a powerful weapon that can vaporize all the asteroids on any given row or column of the grid with A single shot. This weapon was quite expensive, so she wishes to use it sparingly. Given the location of the asteroids in the field, find the minimum number of shots Bessie needs to fire to eliminate a ll of the asteroids.
Input
* Line 1:two integers N and K, separated to a single space.
* Lines 2..k+1:each line contains, 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 a asteroid and "." is empty space:
x.x
. X.
. X.
OUTPUT DETAILS:
Bessie may fire across row 1 to destroy the asteroids at (all) and (1,3), and then she could fire down column 2 to destroy T He asteroids at (2,2) and (3,2).
Problem Solving Ideas:
Http://www.cnblogs.com/lyy289065406/archive/2011/07/30/2121730.html
Consider the phalanx as a special dichotomy (with rows and columns as two vertex sets V1, V2, where | v1|=| v2|)
Each row x or column y is then treated as a point, and the obstacle (x, y) can be considered as the edge of the connection x and Y. After the composition according to this idea. The problem is transformed into the least-chosen point (x or Y), so that it is adjacent to all the edges from these points, which is actually the minimum point coverage problem.
re-using the König of the maximum matching of two-fractal graphs theorem:
minimum number of points covered = maximum number of matches
(PS: Minimum point overlay: If you choose a point that is equivalent to covering all the edges with it as the endpoint, you need to select the fewest points to cover all the edges of the graph.) )
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5 6 using namespacestd;7 Const intMAX = -+Ten;8 intG[max][max],link[max],vis[max];9 intn,v1,v2,k;Ten intDfsintx) One { A for(inti =1; I <= V2; i++) - { - if(Vis[i] = =0&&G[x][i]) the { -Vis[i] =1; - if(Link[i] = =0||DFS (Link[i])) - { +Link[i] =x; - return true; + } A } at } - return false; - } - intMain () - { -scanf"%d%d", &n,&k); inV2 = V1 =N; - for(inti =1; I <= K; i++) to { + intx, y; -scanf"%d%d",&x,&y); theG[x][y] =true; * $ }Panax Notoginseng intm =0; -memset (Link,0,sizeof(link)); the for(inti =1; I <= V1; i++) + { Amemset (Vis,0,sizeof(Vis)); the if(Dfs (i)) +m++; - } $printf"%d\n", m); $ return 0; -}
View Code
Poj3041asteroids (minimum point overlay + but not very understanding why)