zoukankan      html  css  js  c++  java
  • Poj 3041 Asteroids

    Asteroids
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 25270   Accepted: 13635

    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

     
    //题意:有一个n*n的矩阵,上面有m个黑格子
    //        现在有一把刷子,可以将一行或一列上的黑格子刷成白色
    //        问最少需要几刷子可以让矩阵变成全白
    //        格子不是黑的就是白的 
    
    //用横坐标和纵坐标建立二分图,如果有黑格子在(x,y)
    //那么就让左侧x连向右侧y
    //那么我们就可以得到同行和同列的格子的个数
    //然后求最小点覆盖就可以了 
    
    //最小点覆盖=最大匹配数
    
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    using namespace std;
    
    inline int read()
    {
        char c=getchar();int num=0;
        for(;!isdigit(c);c=getchar());
        for(;isdigit(c);c=getchar())
            num=num*10+c-'0';
        return num;
    }
    
    const int M=1e4+5;
    
    int n,m;
    int match[M];
    int vis[M],tim;
    int head[M],num_edge;
    struct Edge
    {
        int v,nxt;
    }edge[M];
    
    void add_edge(int u,int v)
    {
        edge[++num_edge].v=v;
        edge[num_edge].nxt=head[u];
        head[u]=num_edge;
    }
    
    bool dfs(int u)
    {
        for(int i=head[u],v;i;i=edge[i].nxt)
        {
            v=edge[i].v;
            if(vis[v]==tim)
                continue;
            vis[v]=tim;
            if(!match[v]||dfs(match[v]))
            {
                match[v]=u;
                return 1;
            }
        }
        return 0;
    }
    
    int main()
    {
        n=read(),m=read();
        for(int i=1,x,y;i<=m;++i)
        {
            x=read(),y=read();
            add_edge(x,y);
        }
        int ans=0;
        for(int i=1;i<=n;++i)
        {
            ++tim;
            ans+=dfs(i);
        }
        printf("%d",ans);
        return 0;
    }
  • 相关阅读:
    August 4th, 2016, Week 32nd, Thursday
    August 3rd, 2016, Week 32nd, Wednesday
    Java的垃圾回收机制
    学java入门到精通,不得不看的15本书
    java中set和get方法的理解
    eclipse快捷键
    main方法无法编译
    Java构造器和方法的区别
    交换两个变量的值,不使用第三个变量
    计算圆周率 Pi (π)值, 精确到小数点后 10000 位 只需要 30 多句代码
  • 原文地址:https://www.cnblogs.com/lovewhy/p/9029361.html
Copyright © 2011-2022 走看看