CSP 201912-2 《回收站选址》

 

 参考代码:

#include<iostream>
#include<vector>
#include<string>
#include<map>
#include<algorithm>

using namespace std;

void printmap(map<int, int>& m) {
    for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) {
        cout << it->second << endl;
    }
}

//判断是否是回收站

bool junkstation(vector<pair<int, int>>& m, pair<int, int>& p) {
    pair<int, int> p1;
    p1.first = p.first + 1, p1.second = p.second;
    pair<int, int> p2;
    p2.first = p.first - 1, p2.second = p.second;
    pair<int, int> p3;
    p3.first = p.first, p3.second = p.second + 1;
    pair<int, int> p4;
    p4.first = p.first, p4.second = p.second - 1;
    vector<pair<int, int>>::iterator pos1 = find(m.begin(), m.end(), p1);
    vector<pair<int, int>>::iterator pos2 = find(m.begin(), m.end(), p2);
    vector<pair<int, int>>::iterator pos3 = find(m.begin(), m.end(), p3);
    vector<pair<int, int>>::iterator pos4 = find(m.begin(), m.end(), p4);
    if (pos1 != m.end() && pos2 != m.end() && pos3 != m.end() && pos4 != m.end()) {
        return true;
    }
    else {
        return false;
    }
}

//获取回收站得分数

int getscore(vector<pair<int, int>>& m, pair<int, int>& p) {
    int score = 0;
    pair<int, int> p1;
    p1.first = p.first + 1, p1.second = p.second + 1;
    pair<int, int> p2;
    p2.first = p.first - 1, p2.second = p.second - 1;
    pair<int, int> p3;
    p3.first = p.first + 1, p3.second = p.second - 1;
    pair<int, int> p4;
    p4.first = p.first - 1, p4.second = p.second + 1;
    vector<pair<int, int>>::iterator pos1 = find(m.begin(), m.end(), p1);
    vector<pair<int, int>>::iterator pos2 = find(m.begin(), m.end(), p2);
    vector<pair<int, int>>::iterator pos3 = find(m.begin(), m.end(), p3);
    vector<pair<int, int>>::iterator pos4 = find(m.begin(), m.end(), p4);
    if (pos1 != m.end()) { score += 1; }
    if (pos2 != m.end()) { score += 1; }
    if (pos3 != m.end()) { score += 1; }
    if (pos4 != m.end()) { score += 1; }
    return score;
}

int main() {
    int n;
    cin >> n;
    vector<pair<int, int>> vec;
    map<int, int> m;
    for (int i = 0; i < n; i++) {
        pair<int, int> p;
        cin >> p.first >> p.second;
        vec.push_back(p);
    }
    for (int i = 0; i < 5; i++) {
        m.insert(pair<int, int>(i, 0));
    }
    for (vector<pair<int, int>>::iterator it = vec.begin(); it != vec.end(); it++) {
        if (junkstation(vec, *it)) {
            int score = getscore(vec, *it);
            for (int i = 0; i < 5; i++) {
                if (score == i) {
                    m[i] += 1;
                }
            }
        }
    }
    printmap(m);
    system("pause");
    return 0;
}
看到这题第一反应是vector套pair,然后将分数和站数量存在map里面,写下来感觉有点繁琐,懒得简化了,C++11是100分。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值