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;
    }
    
  • 相关阅读:
    Spark机器学习基础三
    Spark机器学习基础二
    如何在Windows上的Jupyter Notebook中安装和运行PySpark
    Spark机器学习基础一
    基于卷积神经网络CNN的电影推荐系统
    相似度计算方法
    调参贝叶斯优化(BayesianOptimization)
    isStatic:检测数据是不是除了symbol外的原始数据
    获取当前页面url指定参数值
    Vue.js 运行环境搭建详解(基于windows的手把手安装教学)
  • 原文地址:https://www.cnblogs.com/cjyyb/p/7227453.html
Copyright © 2011-2022 走看看