• poj3041


                                                                                                   Asteroids
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 12270   Accepted: 6679

    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.
     

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

    Source

     

     

     

    #include <iostream>  
        using namespace std;  
        const int N = 510; //定义 顶点最大个数  
        bool map[N][N],used[N]; //保存 顶点 边关系  
        int link[N]; //记录 与Xi关联的Yi元素 line[Yj] = Xi,-1表示未关联   
        int n,k; //定义 顶点数 与边数  
        bool DFS(int t) //检验顶点t 是否可以增广  
        {  
            for(int i=1; i<=n; i++) //Y中所有顶点 i  
            {  
                if(!used[i] && map[t][i]) //如果顶点i 未被使用,并且t与i有边相连  
                {  
                    used[i] = true; //将顶点i标记  
                    if(link[i]==-1 || DFS(link[i]))//如果顶点i 没有 被标记的边连接到 t,  
                                                   //或者 i与X中的顶点link[i]构成的边被标记 但X的顶点link[i]可以增广  
                    {  
                        link[i] = t;//修改边的标记方式,使顶点t与i构成的边关系被标记;  
                        return true;//可以增广  
                    }  
                }  
            }  
            return false;  
        }  
        int main()  
        {  
            int i,x,y,ans=0;  
            memset(link, -1, sizeof(link));//初始化  
            memset(map, false, sizeof(map));  
            scanf("%d %d", &n, &k);  
            for(i=0; i<k; i++)  
            {  
                scanf("%d %d", &x, &y);  
                map[x][y] = true;  
            }  
            for(i=1; i<=n; i++)//选择一个顶点Xi  
            {  
                memset(used, false, sizeof(used));//将 所有X顶点设为未标志  
                if(DFS(i)) ans++;//如果Xi可以增广,匹配数加1  
            }  
            printf("%d
    ", ans);  
            return 0;  
        }  
  • 相关阅读:
    深入理解系统调用
    基于mykernel 2.0编写一个操作系统内核
    如何评测一个软件工程师的计算机网络知识水平与网络编程技能水平?
    如何评测软件工程知识技能水平?
    深入理解TCP的三次握手及其源代码
    Socket与系统调用深度分析
    未来的图书会是什么样子?
    构建调试Linux内核网络代码的环境MenuOS系统
    Python笔记005-神奇的+=
    Python笔记004-元组的拆包和命名元组
  • 原文地址:https://www.cnblogs.com/Deng1185246160/p/3238242.html
Copyright © 2020-2023  润新知