紫书第五章习题 5-12 UVa511-Do You know The Way to san jose

出处:https://blog.csdn.net/kun768/article/details/43966329

题目链接:https://vjudge.net/contest/231030#problem/L

这题不难,但细节比较多,排序的依据有很多,比较容易出错。考差C++和STL也比较全面,比如结构体、构造函数、map、pair、vector、sort、unique、copy等等,使用C++11的lambda配合泛型算法是一个很好的选择。

    #include <bits/stdc++.h>  
    #define EPS 1e-7  
    using namespace std;  
      
    struct MAP  
    {  
        string name;  
        double x1, y1, x2, y2, area, Ratio;  
        pair<double, double> center;  
        int ilevel = -1;  
        MAP(double _x1, double _y1, double _x2, double _y2, string _name) :  
            x1(_x1), y1(_y1), x2(_x2), y2(_y2), name(_name) {  
            if (x1 > x2) swap(x1, x2);  
            if (y1 > y2) swap(y1, y2);  
            area = (x2 - x1) * (y2 - y1);  
            center.first = (x1 + x2) / 2.0;  
            center.second = (y1 + y2) / 2.0;  
            Ratio = (y2 - y1) / (x2 - x1);  
        }  
        bool contain(pair<double, double> x) const {  
            return x.first >= x1 && x.first <= x2 && x.second >= y1 && x.second <= y2;  
        }  
    };  
    vector<MAP> maps;  
    map<string, pair<double, double>> site;  
    pair<double, double> Qpos;  
      
    double dist(pair<double, double> a, pair<double, double> b) {  
        return hypot(a.first - b.first, a.second - b.second);  
    }  
    bool cmp(const MAP & a, const MAP & b){  
        if (a.ilevel != b.ilevel) return a.ilevel > b.ilevel;  
        double d1 = dist(a.center, Qpos), d2 = dist(b.center, Qpos);  
        if (fabs(d1 - d2) > EPS) return d1 < d2;  
        d1 = fabs(a.Ratio - 0.75), d2 = fabs(b.Ratio - 0.75);  
        if (fabs(d1 - d2) > EPS) return d1 < d2;  
        d1 = dist(Qpos, make_pair(a.x2, a.y1)), d2 = dist(Qpos, make_pair(a.x2, a.y1));  
        if (fabs(d1 - d2) > EPS) return d1 > d2;  
        return a.x1 < b.x1;  
    }  
    int main()  
    {  
        ios::sync_with_stdio(false);  
        string name; cin >> name;  
        while (cin >> name, name != "LOCATIONS") {  
            double x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2;  
            maps.push_back(MAP(x1, y1, x2, y2, name));  
        }  
        while (cin >> name, name != "REQUESTS") {  
            double x, y; cin >> x >> y;  
            site[name] = make_pair(x, y);  
        }  
        while (cin >> name, name != "END") {  
            int level; cin >> level;  
            if (!site.count(name)) printf("%s at detail level %d unknown location\n", name.c_str(), level);  
            else {  
                printf("%s at detail level %d ", name.c_str(), level);  
                Qpos = site[name];  
                vector<double> all_area;  
                vector<MAP> C1, C2;  
                for (auto & i : maps) if (i.contain(Qpos)) {  
                    C1.push_back(i);  
                    all_area.push_back(i.area);  
                }  
                sort(all_area.begin(), all_area.end(), greater<double>());  
                unique(all_area.begin(), all_area.end(), greater<double>());  
                for (auto & i : C1)  
                    i.ilevel = find_if(all_area.begin(), all_area.end(), [i](const double area){return fabs(area - i.area) <= EPS; }) - all_area.begin() + 1;  
                sort(C1.begin(), C1.end(), cmp);  
                copy_if(C1.begin(), C1.end(), back_inserter(C2), [level](const MAP & x) {return x.ilevel == level; });  
                if (C1.size() == 0) puts("no map contains that location");  
                else if (C2.size() == 0) printf("no map at that detail level; using %s\n", C1[0].name.c_str());  
                else printf("using %s\n", C2[0].name.c_str());  
            }  
        }  
        return 0;  
    }  

猜你喜欢

转载自blog.csdn.net/JXUFE_ACMer/article/details/80465283