您的位置:首页 > 其它

ACM: 初中数学 poj 1328

2016-05-19 23:17 501 查看
                                                                 
Radar Installation

Description

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.



Figure A Sample Input of Radar Installations
Input

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

Output

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.

Sample Input

3 2

1 2

-3 1

 

2 1

1 2

0 2

 

0 0

Sample Output

Case 1: 2

Case 2: 1

 

题意: 在X轴上安装雷达 , 用最少的雷达个数覆盖全部的岛屿.

解题思路:

              
1.先将全部的岛屿,按照X的大小来排序.

              
2.从左往右放置雷达,每放置一个雷达,根据前面一个雷达的位置判断,前一个雷达覆盖的点.

              
3.放置雷达有一个原则就是: 尽量往右放.

              
4.再根据后面的点并且在雷达位置左面的点,把雷达向左移.一个雷达经过了移的过程,

              
就一定是能覆盖左面的岛.

              
5.最后是在最小的结束点完成,结果要加1.

代码:

 

#include <cstdio>

#include <iostream>

#include <cstring>

#include <cstdlib>

#include <cmath>

using namespace std;

#define MAX 1005

struct node

{

    int x ,
y;

}islands[MAX];

int n,dist;

int cmp(const void *a,const void *b)

{

    return
(*(node*)a).x - (*(node*)b).x;

}

double getX(node point)

{

    return
point.x + sqrt(dist*dist-point.y*point.y);

}

int result()

{

    int ans =
1;

    double x =
getX(islands[0]);

    for(int i =
1; i < n; ++i)

    {

  
   
 double t = getX(islands[i]);

  
   
 if(islands[i].x < x
&& x > t)

  
   
 {

  
   
   
 x = t;

  
   
   
 continue;

  
   
 }

  
   
 if( (islands[i].y*islands[i].y +
(islands[i].x-x)*(islands[i].x-x)) <= dist *dist
)

  
   
   
 continue;

  
   
 x = getX(islands[i]);

  
   
 ans++;

    }

    return
ans;

}

int main()

{

    int num =
1;

//   
freopen("input.txt","r",stdin);

  
 while(scanf("%d
%d",&n,&dist) != EOF)

    {

  
   
 if(n == 0 && dist
== 0)

  
   
   
 break;

  
   
 int flag = 0;

  
   
 for(int i = 0; i < n; ++i)

  
   
 {

  
   
   
 scanf("%d
%d",&islands[i].x,&islands[i].y);

  
   
   
 if(islands[i].y > dist)

  
   
   
   
 flag = 1;

  
   
 }

  
   
 

  
   
 if(flag == 1)

  
   
   
 printf("Case %d: -1\n",num++);

  
   
 else

  
   
 {

  
   
   
 qsort(islands,n,sizeof(islands[0]),cmp);

  
   
   
 printf("Case %d: %d\n",num++,result());

  
   
 }

  
   
 getchar();

    }

    return
0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: