313 - Intervals
Posted: Fri Aug 09, 2002 9:01 pm
Easy Algorithm... But I recieve WA, again.
Sample test set is perfect.
Would you help me...?
[cpp]
#include<stdio.h>
#include<math.h>
typedef struct _coordinate
{
int x;
int y;
} Coordinate;
typedef struct _interval
{
double start;
double end;
} Interval;
void ComputeInterval(Coordinate Light, Coordinate Pipe, int radius, Interval& data);
void BubbleSort(Interval* data, int num);
void Swap(Interval& a, Interval& b);
int main()
{
Coordinate Pipe, Light;
Interval data[500];
int N, i, radius, count = 0;
double max;
while(scanf("%d", &N) > 0 && N != 0)
{
scanf("%d %d", &Light.x, &Light.y);
for(i=0;i<N;i++)
{
scanf("%d %d %d", &Pipe.x, &Pipe.y, &radius);
ComputeInterval(Light, Pipe, radius, data);
}
BubbleSort(data, N);
/* for(i=0;i<N;i++)
printf("start : %.2lf , end : %.2lf\n", data.start, data.end);*/
if(count != 0) printf("\n");
for(i=0;i<N;i++)
{
printf("%.2lf ", data.start);
max = data.end;
while(i < N-1 && data.end >= data[i+1].start)
{
max = data[i+1].end > max ? data[i+1].end : max;
i++;
}
printf("%.2lf\n", max);
}
count++;
}
return 0;
}
void BubbleSort(Interval* data, int num)
{
for(int i=0;i<num-1;i++)
for(int j=num-1;j>=i+1;j--)
if(data[j-1].start > data[j].start)
Swap(data[j], data[j-1]);
}
void Swap(Interval& a, Interval& b)
{
Interval temp;
temp.start = a.start;
temp.end = a.end;
a.start = b.start;
a.end = b.end;
b.start = temp.start;
b.end = temp.end;
}
void ComputeInterval(Coordinate Light, Coordinate Pipe, int radius, Interval& data)
{
if(Light.x == Pipe.x)
{
double angle = asin((double)radius / ((double)Light.y - (double)Pipe.y));
double range = Light.y * tan(angle);
data.start = (double)Light.x - range;
data.end = (double)Light.x + range;
}
else
{
double angle1 = asin((double)radius / sqrt(pow((double)Light.x - (double)Pipe.x, 2.0) + pow((double)Light.y - (double)Pipe.y, 2.0)));
double angle2 = atan2((double)Light.x > (double)Pipe.x ? (double)Light.x - (double)Pipe.x : (double)Pipe.x - (double)Light.x, (double)Light.y - (double)Pipe.y);
double range1 = tan(angle2 - angle1) * (double)Light.y;
double range2 = tan(angle2 + angle1) * (double)Light.y;
if(Light.x > Pipe.x)
{
data.start = (double)Light.x - range2;
data.end = (double)Light.x - range1;
}
else
{
data.start = (double)Light.x + range1;
data.end = (double)Light.x + range2;
}
}
}
[/cpp]
Sample test set is perfect.
Would you help me...?
[cpp]
#include<stdio.h>
#include<math.h>
typedef struct _coordinate
{
int x;
int y;
} Coordinate;
typedef struct _interval
{
double start;
double end;
} Interval;
void ComputeInterval(Coordinate Light, Coordinate Pipe, int radius, Interval& data);
void BubbleSort(Interval* data, int num);
void Swap(Interval& a, Interval& b);
int main()
{
Coordinate Pipe, Light;
Interval data[500];
int N, i, radius, count = 0;
double max;
while(scanf("%d", &N) > 0 && N != 0)
{
scanf("%d %d", &Light.x, &Light.y);
for(i=0;i<N;i++)
{
scanf("%d %d %d", &Pipe.x, &Pipe.y, &radius);
ComputeInterval(Light, Pipe, radius, data);
}
BubbleSort(data, N);
/* for(i=0;i<N;i++)
printf("start : %.2lf , end : %.2lf\n", data.start, data.end);*/
if(count != 0) printf("\n");
for(i=0;i<N;i++)
{
printf("%.2lf ", data.start);
max = data.end;
while(i < N-1 && data.end >= data[i+1].start)
{
max = data[i+1].end > max ? data[i+1].end : max;
i++;
}
printf("%.2lf\n", max);
}
count++;
}
return 0;
}
void BubbleSort(Interval* data, int num)
{
for(int i=0;i<num-1;i++)
for(int j=num-1;j>=i+1;j--)
if(data[j-1].start > data[j].start)
Swap(data[j], data[j-1]);
}
void Swap(Interval& a, Interval& b)
{
Interval temp;
temp.start = a.start;
temp.end = a.end;
a.start = b.start;
a.end = b.end;
b.start = temp.start;
b.end = temp.end;
}
void ComputeInterval(Coordinate Light, Coordinate Pipe, int radius, Interval& data)
{
if(Light.x == Pipe.x)
{
double angle = asin((double)radius / ((double)Light.y - (double)Pipe.y));
double range = Light.y * tan(angle);
data.start = (double)Light.x - range;
data.end = (double)Light.x + range;
}
else
{
double angle1 = asin((double)radius / sqrt(pow((double)Light.x - (double)Pipe.x, 2.0) + pow((double)Light.y - (double)Pipe.y, 2.0)));
double angle2 = atan2((double)Light.x > (double)Pipe.x ? (double)Light.x - (double)Pipe.x : (double)Pipe.x - (double)Light.x, (double)Light.y - (double)Pipe.y);
double range1 = tan(angle2 - angle1) * (double)Light.y;
double range2 = tan(angle2 + angle1) * (double)Light.y;
if(Light.x > Pipe.x)
{
data.start = (double)Light.x - range2;
data.end = (double)Light.x - range1;
}
else
{
data.start = (double)Light.x + range1;
data.end = (double)Light.x + range2;
}
}
}
[/cpp]