zoukankan      html  css  js  c++  java
  • 【POJ 3401】Asteroids

    题面

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

    题解

    先翻译一下:在N*N的矩阵里面,有k个星球。有一个神奇的武器,每次可以消灭一行或者一列的星球,问最少使用多少次武器可以消灭所有的星球。

    每次读入一颗星球的位置,就将他所在的行和列连一条边。
    求解最大匹配即可

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<cmath>
    #include<vector>
    #include<algorithm>
    using namespace std;
    #define MAX 510
    inline int read()
    {
    	   register int x=0,t=1;
    	   register char ch=getchar();
    	   while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
    	   if(ch=='-'){t=-1;ch=getchar();}
    	   while(ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
    	   return x*t;
    }
    vector<int> e[MAX];
    bool vis[MAX];
    int match[MAX],Ans=0,N,K;
    bool DFS(int u)
    {
    	   for(int i=0;i<e[u].size();++i)
    	   {
    	   	      int v=e[u][i];
    	   	      if(!vis[v])
    	   	      {
    	   	      	     vis[v]=true;
    	   	      	     if(!match[v]||DFS(match[v]))
    	   	      	     {
    	   	      	     	    match[v]=u;
    	   	      	     	    return true;
    	   	      	     }
    	   	      }
    	   }
    	   return false;
    }
    int main()
    {
    	   N=read();
    	   K=read();
    	   for(int i=1;i<=K;++i)
    			   e[read()].push_back(read());	   	
    	   for(int i=1;i<=N;++i)
    	   {
    	   	     memset(vis,0,sizeof(vis));
    	   	     if(DFS(i))
    	   	        ++Ans;
    	   }
    	   
    	   cout<<Ans<<endl;
    	   
    	   return 0;
    }
    
  • 相关阅读:
    交叉编译GDB
    交叉编译Python
    DEBUG memory问题的一些工具以及注意事项
    [qemu] QEMU虚拟机的安装步骤
    分享一个public domain 的大数运算库,可以用于RSA加密解密
    [KERNEL] 在procfs中新增文件的方法
    [backtrace]在不改变代码的情况下,打印可执行文件的backtrace
    【幻化万千戏红尘】qianfeng-JAVA课程结束总结
    【幻化万千戏红尘】qianfengDay30-正则表达式、注解注释基础学习:
    【幻化万千戏红尘】qianfengDay29-Json解析、Gson解析、FASTJSON解析基础学习:
  • 原文地址:https://www.cnblogs.com/cjyyb/p/7227453.html
Copyright © 2011-2022 走看看