CCF CSP 竞赛试题——回收站选址(201912-2)

#include <bits/stdc++.h>
using namespace std;

unordered_map<int, unordered_set<int>> points;

bool isPotentialSite(int x, int y) {
    static const int dir[] = {0, 1, 0, -1, 0};
    for (int i = 0; i < 4; ++i) {
        int nx = x + dir[i];
        int ny = y + dir[i + 1];
        if (!points.count(nx) || !points[nx].count(ny))
            return false;
    }
    return true;
}

int calcScore(int x, int y) {
    int res = 0;
    static const int dir[] = {-1, 1, 1, -1, -1};
    for (int i = 0; i < 4; ++i) {
        int nx = x + dir[i];
        int ny = y + dir[i + 1];
        if (points.count(nx) && points[nx].count(ny)) ++res;
    }
    return res;
}

int main() {
    int n;
    cin >> n;
    for (int _ = 0; _ < n; ++_) {
        int x, y;
        cin >> x >> y;
        points[x].insert(y);
    }
    vector<int> res(5);
    for (const auto& x_axis: points) {
        for (const auto& y: x_axis.second) {
            int x = x_axis.first;
            if (isPotentialSite(x, y))
                ++res[calcScore(x, y)];
        }
    }
    for (int x: res) cout << x << endl;
    return 0;
}

附:

1.in

7
1 2
2 1
0 0
1 1
1 0
2 0
0 1

1.out

0
0
1
0
0

2.in

2
0 0
-100000 10

2.out

0
0
0
0
0

3.in

11
9 10
10 10
11 10
12 10
13 10
11 9
11 8
12 9
10 9
10 11
12 11

3.out

0
2
1
0
0
发布了33 篇原创文章 · 获赞 4 · 访问量 8728

猜你喜欢

转载自blog.csdn.net/Touchig/article/details/104050206