CCF201912-2 回收站选址

 

 

 解题思路:这道题唬人的在于坐标有正有负哈,刚开始不知道怎么下爪,仔细思考过后,我可是会面向对象编程的啊哈哈哈哈,我可是最喜欢封装了哈哈哈哈。

1.首先可以把每个点用一个结构体来定义,包含他的x,y坐标,以及该点的得分情况。因为最大有1000个点,可以再定义一个点的数组来存放输入的点。

2.点存储了之后我们就可以来对数据进行处理了,首先遍历数组得到可以作为选址点的Node,将可以作为选址点的Node用一个容器(vector)存起来。

3.我们得到可以用作选址的点之后,就可以对每个点进行评分了,然后将评分结果输出就ok了!

#include<iostream>
#include<cmath>
#include<vector>
using namespace std;

struct Node {
    int x;
    int y;
    int score;
} node[1000];

/*
    判断节点1和节点2是否相邻
*/
bool IsAdjacent(Node n1, Node n2) {
    if (abs(n1.x - n2.x) == 1 && n1.y==n2.y || abs(n1.y - n2.y) == 1 && n1.x==n2.x) return true;
    else  return false;
}
/*
判断是否得分
*/
bool IsgetScore(vector<Node>::iterator vit, Node n) {
    if (abs(vit->x - n.x) == 1 && abs(vit->y - n.y) == 1) return true;
    else return false;
}
int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> node[i].x >> node[i].y;//输入点坐标
    }
    
    vector<Node> xz;//可作为选址的节点
    int count = 0;//记录上下左右是否均存在垃圾的值=4说明均存在,不等于说明不存在
    for (int i = 0; i < n; i++) {
        count = 0;//每个节点开始前先清零
        for (int j = 0; j < n; j++) {
            if (IsAdjacent(node[i], node[j])) count++;
        }
        if (count == 4) xz.push_back(node[i]);//当前节点可以作为选址的节点
    }

    int s[5] = {0};//最后的输出得分数组
    vector<Node>::iterator vit;
    //判断可作为选址的节点能得多少分
    for (vit = xz.begin(); vit != xz.end(); vit++) {
        for (int i = 0; i < n; i++) {
            if (IsgetScore(vit, node[i])) vit->score++;//对节点得分数++操作
        }
        s[vit->score]++;//得分为vit—>socre的元素个数加一
    }
    //输出结果数组
    for (int i = 0; i < 5; i++) {
        cout << s[i] << endl;
    }

    system("pause");
    return 0;
}
唯有热爱方能抵御岁月漫长。
原文地址:https://www.cnblogs.com/syq816/p/12392845.html