POJ 3407 - Brookebond s'en va en guerre...

http://poj.org/problem?id=3407

概要

地球が半径6370kmの完全な球形だと仮定して,緯度と経度で表された2点間の球面上の距離を答える.

制約

解法

計算するだけ.

 1 #include <cstdio>
 2 #include <cmath>
 3 using namespace std;
 4 static const double PI = atan2(+0.0, -0.0);
 5 
 6 struct point
 7 {
 8   double x, y, z;
 9   double abs() const { return x*x + y*y + z*z; }
10   double operator*(const point& that) const { return x*that.x + y*that.y + z*that.z; }
11 };
12 
13 point read_point()
14 {
15   int lat, lat_min, lon, lon_min;
16   char ns, ew;
17   scanf("%d %d %c %d %d %c", &lat, &lat_min, &ns, &lon, &lon_min, &ew);
18   const double la = (ns == 'N' ? 1 : -1) * (lat + lat_min/60.0);
19   const double lo = (ew == 'E' ? 1 : -1) * (lon + lon_min/60.0);
20   const double theta = la / 180.0 * PI;
21   const double phi = lo / 180.0 * PI;
22   point p;
23   p.x = cos(theta)*cos(phi);
24   p.y = cos(theta)*sin(phi);
25   p.z = sin(theta);
26   return p;
27 }
28 
29 double surface_distance(const point& p1, const point& p2)
30 {
31   static const double R = 6370.0;
32   return R * acos((p1 * p2) / (p1.abs() * p2.abs()));
33 }
34 
35 int main()
36 {
37   const point p1 = read_point();
38   const point p2 = read_point();
39   printf("%.3f\n", surface_distance(p1, p2));
40   return 0;
41 }
poj/3407.cc