• 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.

    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
    

    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
    ", my_ans);
    54     }
    55     return 0;
    56 }
  • 相关阅读:
    【特种兵系列】String中的==和equals()
    windows7 x64下maven安装和配置
    微信开发之本地接口调试(非80端口,无需上传服务器服务器访问)
    Java微信公众平台接口封装源码分享
    jdk安装
    用户权限管理
    Linux常用命令(一)
    Xshell显示中文乱码问题
    伪装虽易测试不易之微信浏览器
    每次新建项目出现appcompat_v7 解决方法
  • 原文地址:https://www.cnblogs.com/GetcharZp/p/9466419.html
Copyright © 2020-2023  润新知