关闭

uva 10553 Treasure Map (指南针偏差航海寻宝)

331人阅读 评论(0) 收藏 举报

题意:给了一张地图,按照正常的路线,可以恰好到达那个有宝藏的小岛,由于地理北极和地磁北极方向有别,导致每次转向都有角度偏差,求按照地图航行过程中,距离小岛的最短路径

分析:先求出小岛位置,然后按照偏差计算,每条直线距离小岛距离,取最小即可

#include <bits/stdc++.h>
#include <iostream>

using namespace std;
const double eps = acos(-1.0);
int  n;
double num;
string str[32] = {"N", "NbE", "NNE", "NEbN", "NE", "NEbE", "ENE", "EbN", "E", "EbS", "ESE", "SEbE", "SE", "SEbS", "SSE",
"SbE", "S", "SbW", "SSW", "SWbS", "SW", "SWbW", "WSW", "WbS", "W", "WbN", "WNW", "NWbW", "NW", "NWbN", "NNW", "NbW"
};
struct Point
{
    double x;double y;
    Point(){};   //不加这一句,有的编译器会报错,no matching function for call to ...
    Point(const double xx,const double yy)
    {
        x = xx; y = yy;
    }
};
struct Road
{
    double x;
    double y;
    Road(const double xx,const double yy)
    {
        x = xx; y = yy;
    }
};
map<string,double> direction;
vector<Road> road;
void init()
{
    for(int i = 0; i < 32; i++)
    {
        direction[str[i]] = eps*i/16;
    }
}
void read()
{
    road.clear();
    string str0;
    double num0;
    for(int i = 0; i< n; i++)
    {
        cin >> str0 >> num0;
        road.push_back(Road(direction[str0],num0));

    }
    cin >> num;
    num = num *eps /180;
}
double dis(Point p1,Point p2)
{
    return sqrt((p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y));
}
double dot(Point p0,Point p1,Point p2){  //p0p1点乘 p0p2;
	return (p1.x-p0.x)*(p2.x-p0.x)+(p1.y-p0.y)*(p2.y-p0.y);
}

struct line{
	Point a,b;
	line(const Point a0,const Point b0){a=a0;b=b0;}
	double getdis(Point p){//点到线段的最小距离
		if(dot(a,p,b)<0 || dot(b,p,a)<0) return min(dis(p,a),dis(p,b));
		else{
			double a0,b0,c0;
			a0=a.y-b.y;
			b0=b.x-a.x;
			c0=a.x*b.y-b.x*a.y;
			return fabs(a0*p.x+b0*p.y+c0)/sqrt(a0*a0+b0*b0);
		}
	}
};
void computing()
{
    Point d(0,0),t(0,0),pre;
	for(int i=0;i<n;i++){
		d.x+=cos(road[i].x)*road[i].y;
		d.y+=sin(road[i].x)*road[i].y;
	}
	double ans=dis(d,t);
	for(int i=0;i<n;i++){
		pre=t;
		t.x+=cos(road[i].x-num)*road[i].y;
		t.y+=sin(road[i].x-num)*road[i].y;
		line l(pre,t);
		ans=min(ans,l.getdis(d));
	}
	printf("%.2lf\n",ans);
}
int main()
{

    init();
    while(scanf("%d",&n)&&n)
    {
        read();
        computing();
    }
    return 0;
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:56396次
    • 积分:2278
    • 等级:
    • 排名:第17238名
    • 原创:175篇
    • 转载:31篇
    • 译文:0篇
    • 评论:4条
    文章分类
    最新评论