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
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).
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).
KEY:
这题貌似好早学长们布置的题目……当时也有人讲了……貌似还是不会……今天看二分图匹配, 突然想去做,居然神奇的AC了。
这题用的是二分图匹配的匈牙利算法。
- Source Code
- #include<iostream>
- #define N 500
- using namespace std;
- int a[N+1][N+1];
- int n,m;
- int match[N+1];
- int check[N+1];
- int dfs(int p)
- {
- int i,t;
- for(i=1;i<=n;i++)
- if(a[i][p]&&!check[i])
- {
- check[i]=1;
- t=match[i];
- match[i]=p;
- if(t==0||dfs(t)) return 1;
- match[i]=t;
- }
- return 0;
- }
- void input()
- {
- scanf("%d %d",&n,&m);
- int i;
- int x,y;
- for(i=1;i<=m;i++)
- {
- scanf("%d %d",&x,&y);
- a[x][y]=1;
- }
- }
- int main()
- {
- // freopen("pku_3041.in","r",stdin);
- input();
- int i,max=0;
- for(i=1;i<=n;i++)
- {
- memset(check,0,sizeof(check));
- if(dfs(i)) max++;
- }
- printf("%d/n",max);
- return 0;
- }
本文介绍了一个使用二分图匹配的匈牙利算法解决太空扫雷问题的方法。目标是最小化清除NxN网格中所有障碍所需的射击次数。通过将行和列视为二分图的两边,并利用匈牙利算法寻找最大匹配,从而确定最少射击次数。

346

被折叠的 条评论
为什么被折叠?



