求助:该问题代码总是WA,请问错在哪里?
题目:详见:http://poj.org/problem?id=1328RadarInstallationAssumethecoastingisaninfinitestr...
题目:详见:http://poj.org/problem?id=1328
Radar Installation
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.
代码:
#include <stdio.h>
#include <iostream>
#include <math.h>
typedef struct
{
int X;
int Y;
}POINT;
typedef struct
{
double s;
double e;
}AREA;
using namespace std;
void Qsort(AREA a[], int low, int high)
{
if(low >= high)
return;
int first = low;
int last = high;
AREA key = a[first];/*用字表的第一个记录作为枢轴*/
while(first < last)
{
while(first < last && a[last].e >= key.e)
--last;
a[first] = a[last];
while(first < last && a[first].e <= key.e)
++first;
a[last] = a[first];
}
a[first] = key;/*枢轴记录到位*/
Qsort(a, low, first-1);
Qsort(a, first+1, high);
}
int main()
{
int i,j,t1,t2,n,d,n_r,count=0;
POINT p[1005];
AREA a[1005];
double f_t;
double r;
while(cin>>n>>d)
{
int flag=0;
count++;
if(n==0&&d==0)return 0;
if(d<0)goto CANNOTDOIT;
for(i=-1;++i<n;)
{
cin>>p[i].X>>p[i].Y;
if(p[i].Y>d)flag=1;
f_t=sqrt((double)d*(double)d-(double)p[i].Y*(double)p[i].Y);
a[i].s=p[i].X-f_t;
a[i].e=p[i].X+f_t;
}
if(flag)goto CANNOTDOIT;
Qsort(a,0,n-1);
n_r=1;
r=a[0].e+d;
for(i=-1;++i<n;)
{
if(r-a[i].e>1e-5)r=a[i].e;
if(a[i].s-r>1e-5)
{
n_r++;
r=a[i].e;
}
}
goto GONEXT;
CANNOTDOIT:
n_r=-1;
GONEXT:
cout<<"Case "<<count<<": "<<n_r<<endl;
}
return 0;
} 展开
Radar Installation
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.
代码:
#include <stdio.h>
#include <iostream>
#include <math.h>
typedef struct
{
int X;
int Y;
}POINT;
typedef struct
{
double s;
double e;
}AREA;
using namespace std;
void Qsort(AREA a[], int low, int high)
{
if(low >= high)
return;
int first = low;
int last = high;
AREA key = a[first];/*用字表的第一个记录作为枢轴*/
while(first < last)
{
while(first < last && a[last].e >= key.e)
--last;
a[first] = a[last];
while(first < last && a[first].e <= key.e)
++first;
a[last] = a[first];
}
a[first] = key;/*枢轴记录到位*/
Qsort(a, low, first-1);
Qsort(a, first+1, high);
}
int main()
{
int i,j,t1,t2,n,d,n_r,count=0;
POINT p[1005];
AREA a[1005];
double f_t;
double r;
while(cin>>n>>d)
{
int flag=0;
count++;
if(n==0&&d==0)return 0;
if(d<0)goto CANNOTDOIT;
for(i=-1;++i<n;)
{
cin>>p[i].X>>p[i].Y;
if(p[i].Y>d)flag=1;
f_t=sqrt((double)d*(double)d-(double)p[i].Y*(double)p[i].Y);
a[i].s=p[i].X-f_t;
a[i].e=p[i].X+f_t;
}
if(flag)goto CANNOTDOIT;
Qsort(a,0,n-1);
n_r=1;
r=a[0].e+d;
for(i=-1;++i<n;)
{
if(r-a[i].e>1e-5)r=a[i].e;
if(a[i].s-r>1e-5)
{
n_r++;
r=a[i].e;
}
}
goto GONEXT;
CANNOTDOIT:
n_r=-1;
GONEXT:
cout<<"Case "<<count<<": "<<n_r<<endl;
}
return 0;
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询