511 - Do You Know the Way to San Jose? (UVA)

题目链接如下:

Online Judge

写完后看到一个很厉害的写法:UVa 511 - Do You Know the Way to San Jose? [STL应用]-CSDN博客

学习学习。

我的代码如下:

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cmath>
#include <map>
#include <utility>
// #define debug

struct mmap{
    std::string name;
    double area;
    double centerX, centerY;
    double aspectRatio;
    double lowerX, lowerY;
    double minX, maxY;
    mmap(std::string _name): name(_name){}
};

std::string op;
double a, b, c, d, x, y;
std::vector<mmap> vec;
std::map<std::string, std::pair<double, double>> loc;
int level;

bool cmp (const mmap &a, const mmap &b){
    if (a.area != b.area){
        return a.area > b.area;
    }
    double disA = (x - a.centerX) * (x - a.centerX) + (y - a.centerY) * (y - a.centerY);
    double disB = (x - b.centerX) * (x - b.centerX) + (y - b.centerY) * (y - b.centerY);
    if (disA != disB){
        return disA < disB;
    }
    if (a.aspectRatio != b.aspectRatio){
        return a.aspectRatio < b.aspectRatio;
    }
    disA = (x - a.lowerX) * (x - a.lowerX) + (y - a.lowerY) * (y - a.lowerY);
    disB = (x - b.lowerX) * (x - b.lowerX) + (y - b.lowerY) * (y - b.lowerY);
    if (disA != disB){
        return disA > disB;
    }
    return a.minX < b.minX;
}

int main(){
    #ifdef debug
    freopen("0.txt", "r", stdin);
    freopen("1.txt", "w", stdout);
    #endif
    std::cin >> op;
    while (std::cin >> op && op != "LOCATIONS"){
        vec.push_back(mmap(op));
        scanf("%lf %lf %lf %lf", &a, &b, &c, &d);
        vec.back().area = std::fabs(a - c) * std::fabs(b - d);
        vec.back().centerX = (a + c) / 2;
        vec.back().centerY = (b + d) / 2;
        vec.back().aspectRatio = std::fabs(std::fabs(b - d) / std::fabs(a - c) - 0.75);
        vec.back().lowerX = std::max(a, c);
        vec.back().lowerY = std::min(b, d);
        vec.back().minX = std::min(a, c);
        vec.back().maxY = std::max(b, d);
    }
    while (std::cin >> op && op != "REQUESTS"){
        scanf("%lf %lf", &a, &b);
        loc[op].first = a;
        loc[op].second = b;
    }
    while (std::cin >> op && op != "END"){
        scanf("%d", &level);
        printf("%s at detail level %d ", op.c_str(), level);
        if (!loc.count(op)){
            printf("unknown location\n");
            continue;
        }
        x = loc[op].first;
        y = loc[op].second;
        std::vector<mmap> ans;
        for (int i = 0; i < vec.size(); ++i){
            if (vec[i].minX <= x && vec[i].lowerX >= x && vec[i].lowerY <= y && vec[i].maxY >= y){
                ans.push_back(vec[i]);
            }
        }
        if (ans.empty()){
            printf("no map contains that location\n");
            continue;
        }
        sort(ans.begin(), ans.end(), cmp);
        std::vector<std::string> maps;
        int curr = 1;
        maps.push_back(ans[0].name);
        int i;
        for (i = 1; i < ans.size(); ++i){
            if (ans[i].area != ans[i - 1].area){
                ++curr;
                maps.push_back(ans[i].name);
                if (curr == level){
                    printf("using %s\n", ans[i].name.c_str());
                    break;
                }
            }
        }
        if (i == ans.size()){
            printf("no map at that detail level; using %s\n", maps.back().c_str());
        }
    }
    #ifdef debug
    fclose(stdin);
    fclose(stdout);
    #endif
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值