• NYOJ 287 Radar 两种AC算法实现


    第一种方法为自己写的,第二种方法为和队友讨论后改进。

    附一句话:博客园是一个分享程序员知识的地方,我不明白,写的代码为啥就不算这个分享的知识呢,为什么老是移除什么首页,不符合规定,所谓云云。。

    代码是程序员分享的一切的思想以及细节的终极体现形式。。。

    如果有一个博客,都是代码,我反而高兴,终于那些繁琐的,具有二义性、甚至多义性的歧义的文字终于没有了,只剩下了简明、逻辑、有效的东西。。。

    本来应该用c++写的,直接使用java写了,感觉更顺手。

    Radar

    时间限制:1000 ms  |  内存限制:65535 KB
    难度:3
     
    描述
    Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point locating in the sea side. And any radar installation, locating on the coasting, can only cover d distance, so an island in the sea can be covered by a radius installation, if the distance between them is at most d. 

    We use Cartesian coordinate system, defining the coasting is the x-axis. The sea side is above x-axis, and the land side below. Given the position of each island in the sea, and given the distance of the coverage of the radar installation, your task is to write a program to find the minimal number of radar installations to cover all the islands. Note that the position of an island is represented by its x-y coordinates.

     

     
    输入
    The input consists of several test cases. The first line of each case contains two integers n (1<=n<=1000) and d, where n is the number of islands in the sea and d is the distance of coverage of the radar installation. This is followed by n lines each containing two integers representing the coordinate of the position of each island. Then a blank line follows to separate the cases. 

    The input is terminated by a line containing pair of zeros
    输出
    For each test case output one line consisting of the test case number followed by the minimal number of radar installations needed. "-1" installation means no solution for that case.
    样例输入
    3 2
    1 2
    -3 1
    2 1
    
    1 2
    0 2
    
    0 0
    样例输出
    Case 1: 2
    Case 2: 1
     1 import java.util.*;
     2 public class Main {
     3   public static void main(String args[]){
     4       Scanner cin=new Scanner(System.in);
     5       int casenum=0;
     6       while(cin.hasNext()){
     7           casenum++;
     8           int n=cin.nextInt();
     9           int d=cin.nextInt();
    10           int radarNum=1;
    11           
    12           if(n==0&&d==0) break;
    13           if(d<=0) {System.out.println("Case "+casenum+": -1");}
    14  
    15 
    16           Xy a[]=new Xy[n];
    17  
    18           boolean slove=true;
    19           
    20           for(int i=0;i<n;i++){
    21     
    22               int x=cin.nextInt();
    23               int y=cin.nextInt();
    24               
    25               if(y>d)  slove=false;
    26               if(d>0&&slove){
    27                a[i]=new Xy();
    28              a[i].min=x-Math.sqrt(d*d-y*y);
    29              a[i].max=x+Math.sqrt(d*d-y*y);
    30           }
    31       }
    32  
    33           if(d>0){
    34           if(n==1&&slove) radarNum=1;
    35           if(n>1&&slove){
    36                 for(int i=0;i<n;i++)
    37                     for(int j=i+1;j<n;j++)
    38                         if(a[i].min>a[j].min){
    39                             Xy tmp=a[i];
    40                             a[i]=a[j];
    41                             a[j]=tmp;
    42                         }
    43             double minRadar=a[0].min;
    44             double maxRadar=a[0].max;
    45             double right=a[0].max;
    46                   
    47               for(int i=1;i<n;i++){
    48  
    49                  if(a[i].max<right){
    50                      right=a[i].max;
    51                  }else
    52                      if(right<a[i].min){
    53                          right=a[i].max;
    54                          radarNum++;
    55                      }
    56                   
    57                   
    58               }
    59           }
    60           System.out.println("Case "+casenum+": "+radarNum);
    61           
    62       }
    63       }
    64   }
    65 
    66   
    67 }
    68 
    69  class Xy {
    70  
    71               double min=0;
    72               double max=0;
    73               public Xy(){
    74                   
    75               }
    76               public Xy(int min,int max){
    77                   this.min=min;
    78                   this.max=max;
    79               }
    80      
    81 }

    第二种AC方法:

     1 import java.util.*;
     2 public class Main {
     3   public static void main(String args[]){
     4       Scanner cin=new Scanner(System.in);
     5       int casenum=0;
     6       while(cin.hasNext()){
     7           casenum++;
     8           int n=cin.nextInt();
     9           int d=cin.nextInt();
    10           int radarNum=1;
    11           
    12           if(n==0&&d==0) break;
    13           if(d<=0) {System.out.println("Case "+casenum+": -1");}
    14  
    15 
    16           Xy a[]=new Xy[n];
    17  
    18           boolean slove=true;
    19           
    20           for(int i=0;i<n;i++){
    21     
    22               int x=cin.nextInt();
    23               int y=cin.nextInt();
    24               
    25               if(y>d)  slove=false;
    26               if(d>0&&slove){
    27                a[i]=new Xy();
    28              a[i].min=x-Math.sqrt(d*d-y*y);
    29              a[i].max=x+Math.sqrt(d*d-y*y);
    30           }
    31       }
    32  
    33           if(d>0){
    34           if(n==1&&slove) radarNum=1;
    35           if(n>1&&slove){
    36                 for(int i=0;i<n;i++)
    37                     for(int j=i+1;j<n;j++)
    38                         if(a[i].min>a[j].min){
    39                             Xy tmp=a[i];
    40                             a[i]=a[j];
    41                             a[j]=tmp;
    42                         }
    43             double minRadar=a[0].min;
    44             double maxRadar=a[0].max;
    45 
    46                   
    47               for(int i=1;i<n;i++){
    48                  if(a[i].min>maxRadar) {
    49                      radarNum++;                     
    50                      minRadar=a[i].min;
    51                      maxRadar=a[i].max;
    52                  }else{
    53                      if(a[i].max>maxRadar) 
    54                          minRadar=a[i].min;
    55                      else{
    56                          minRadar=a[i].min;
    57                          maxRadar=a[i].max;
    58                      }
    59                  }
    60                    
    61                   
    62               }
    63           }
    64           System.out.println("Case "+casenum+": "+radarNum);
    65           
    66       }
    67       }
    68   }
    69 
    70   
    71 }
    72 
    73  class Xy {
    74  
    75               double min=0;
    76               double max=0;
    77               public Xy(){
    78                   
    79               }
    80               public Xy(int min,int max){
    81                   this.min=min;
    82                   this.max=max;
    83               }
    84      
    85 }
  • 相关阅读:
    python中is和==的区别
    深拷贝和浅拷贝
    编码和解码
    with语句处理异常
    python中运行flask报错:UnicodeDecodeError: 'utf8' codec can't decodebyte 0xd5 in position 0:invalid continuation byte
    python中update的基本使用
    python中的程序控制结构
    python中的四种存储结构总结
    python中list,tuple,dict,set特点对比总结
    解决UIScrollview无故偏移和导航条遮挡view的问题
  • 原文地址:https://www.cnblogs.com/zdcaolei/p/2484812.html
Copyright © 2020-2023  润新知