zoukankan      html  css  js  c++  java
  • Asteroids

    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

    ***********************************************************************************************
    题目来源 PKU 3041
    匈牙利算法。
    ************************************************************************************************
     1 #include<iostream>
     2 #include<string>
     3 #include<cstdio>
     4 #include<cstring>
     5 #include<algorithm>
     6 #include<cmath>
     7 using namespace std;
     8 int map[1000][1000];
     9 int bmask[1000];
    10 int ny,nx;
    11 int cy[1000],cx[1000];
    12 int n,k,a,b;
    13 int xm,ym;
    14 int findpath(int u)//找增广路径
    15  {
    16      int x,y;
    17      for(int i=1;i<=ny;i++)
    18       {
    19           if(map[u][i]&&bmask[i]==0)
    20            {
    21                bmask[i]=1;
    22                if(cy[i]==-1||findpath(cy[i]))
    23                  {
    24                      cy[i]=u;
    25                      cx[u]=i;
    26                      return 1;
    27                  }
    28 
    29            }
    30       }
    31       return 0;
    32  }
    33  int match()//算最大匹配
    34  {
    35      int res(0);
    36      for(int i=1;i<=nx;i++)
    37        cx[i]=-1;
    38      for(int j=1;j<=ny;j++)
    39        cy[j]=-1;
    40     for(a=1;a<=nx;a++)
    41      {
    42 
    43          if(cx[a]==-1)
    44           {
    45               for(b=1;b<=ny;b++)
    46                  bmask[b]=0;
    47               res+=findpath(a);
    48 
    49           }
    50 
    51      }
    52     return res;
    53  }
    54  int main()
    55  {
    56      cin>>n>>k;
    57      nx=n;
    58      ny=n;
    59      memset(map,0,sizeof(map));
    60      for(int i=1;i<=k;i++)
    61       {
    62           cin>>xm>>ym;
    63           map[xm][ym]=1;
    64       }
    65     cout<<match()<<endl;
    66     return 0;
    67  }
    View Code
  • 相关阅读:
    今天实现了 沿路径移动
    enum类型的本质(转)
    (转)成为优秀技术人员的两点建议
    深入理解 C# 协变和逆变
    web通信
    ajax入门(复习)
    git版本管理工具的使用
    在asp.net 中使用httpmodules对网页进行处理
    asp.net http概念原理复习
    web page复习笔记
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3245372.html
Copyright © 2011-2022 走看看