CSP CCF: 201912-2 回收站选址 (C++)

题目来源

回收站选址

步骤

  1. 存储n个垃圾点的位置
    数据存储格式: set< pair< int, int> > locatiion: {( x 1 x_1 x1, y 1 y_1 y1), ( x 2 x_2 x2, y 2 y_2 y2), …, ( x n x_n xn, y n y_n yn)}
    存储0、1、2、3、4各分值的回收站选址个数
    数据存储格式:int score[5];
  2. 循环遍历每个垃圾点的位置, 首先判断正四方(正东、正西、正南、正北)是否都存在垃圾点,若都存在垃圾点,则可作为回收站点。接着再对斜四方存在的垃圾点数进行计数 s, ( 0 <= s <= 4)。
  3. 最后输出 s

代码

#include <iostream>
//#include <fstream>
#include <set>
#include <map>

using namespace std;

int main() {
    //ifstream cin("in2.txt");
    int N;
    cin>>N;

    set<pair<int, int> > location;
    while (N > 0) {
        --N;
        int x, y;
        cin>>x>>y;

        location.insert(pair<int, int>(x, y));
    }

    int score[5] = {0};
    for (pair<int, int> p: location) {
        // 正四方
        pair<int, int> p1 = {p.first - 1, p.second};
        pair<int, int> p2 = {p.first + 1, p.second};
        pair<int, int> p3 = {p.first, p.second - 1};
        pair<int, int> p4 = {p.first, p.second + 1};
        // 斜四方
        pair<int, int> p5 = {p.first + 1, p.second + 1};
        pair<int, int> p6 = {p.first - 1, p.second - 1};
        pair<int, int> p7 = {p.first + 1, p.second - 1};
        pair<int, int> p8 = {p.first - 1, p.second + 1};


        int s = 0;
        if (location.find(p1) != location.end() && location.find(p2) != location.end() && location.find(p3) != location.end() && location.find(p4) != location.end()) {
            if (location.find(p5) != location.end()) {
                ++s;
            }
            if (location.find(p6) != location.end()) {
                ++s;
            }
            if (location.find(p7) != location.end()) {
                ++s;
            }
            if (location.find(p8) != location.end()) {
                ++s;
            }
            ++score[s];
        }
    }

    for (int i = 0; i < 5; ++i) {
        cout<<score[i]<<endl;
    }


    return 0;
}


  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值