虽然归属于search problems,但是用简单的方法就可以做了。所有的长度都是double,用int就错了。scanf读取double应该用%lf,用%f g++会读取错误。这种浮点数计算,尽量不要用除法、开方,改成乘法。用atan2不知道算不算错,反正改成了叉乘了。最后输出的时候错误地+1,导致浪费了很多时间。
//ZOJ1041Transmitters
#include <iostream>
#include <stdio.h>
#include <math.h>
//#define DEBUG
using namespace std;
int main(int argc, char *argv[])
{
double tr[2], radius, pt[155][2], pt_tmp[2], dist_tmp[2];
int pt_sum, pt_r_sum, same_count[155], max_count;
double base_angle, angle, angle_diff;
while (scanf("%lf%lf%lf", &tr[0], &tr[1], &radius) && radius >= 0)
{
scanf("%d", &pt_sum);
#ifdef DEBUG
//cout << tr[0] << " " << tr[1] << " " << radius << " " << pt_sum << endl;
#endif
if (pt_sum == 0)
{
cout << 0 << endl;
continue;
}
pt_r_sum = 0;
for (int i = 0; i < pt_sum; i ++)
{
cin >> pt_tmp[0] >> pt_tmp[1];
dist_tmp[0] = (pt_tmp[0] - tr[0]);
dist_tmp[1] = (pt_tmp[1] - tr[1]);
if (dist_tmp[0] * dist_tmp[0] + dist_tmp[1] * dist_tmp[1]
<= radius * radius)
{
same_count[pt_r_sum] = 0;
pt[pt_r_sum][0] = pt_tmp[0];
pt[pt_r_sum][1] = pt_tmp[1];
pt_r_sum ++;
}
}
for (int i = 0; i < pt_r_sum; i ++)
{
//base_angle = atan2(pt[i][1] - tr[1], pt[i][0] - tr[0]);
#ifdef DEBUG
cout << "pt " << i << " baseangle " << base_angle << endl;
#endif
for (int j = 0; j < pt_r_sum; j ++)
{
//if (j == i)
// continue;
//angle = atan2(pt[j][1] - tr[1], pt[j][0] - tr[0]);
//angle_diff = angle - base_angle;
//if (angle_diff <= -M_PI)
// angle_diff += 2.0 * M_PI;
//else if (angle_diff >= M_PI)
// angle_diff -= 2.0 * M_PI;
#ifdef DEBUG
cout << "anglediff " << j << " " << angle_diff << endl;
#endif
//if (angle_diff >= 0)
if ((pt[i][0] - tr[0]) * (pt[j][1] - tr[1])
- (pt[j][0] - tr[0]) * (pt[i][1] - tr[1]) >= 0)
{
same_count[i] ++;
#ifdef DEBUG
cout << "pt " << i << " same "
<< pt[j][0] << " " << pt[j][1] << endl;
#endif
}
}
}
max_count = 0;
for (int i = 0; i < pt_r_sum; i ++)
{
#ifdef DEBUG
cout << "pt " << i << " cnt " << same_count[i] << endl;
#endif
if (same_count[i] > max_count)
max_count = same_count[i];
}
printf("%d\n", max_count);
}
return 0;
}