样例1输入:
7
1 2
2 1
0 0
1 1
1 0
2 0
0 1
输出数据:
0
0
1
0
0
样例2输入:
2
0 0
-100000 10
输出数据:
0
0
0
0
0
样例3输入:
11
9 10
10 10
11 10
12 10
13 10
11 9
11 8
12 9
10 9
10 11
12 11
输出数据:
0
2
1
0
0
思路
建立一个set结构,来存储垃圾的位置(位置就是一个 pair<int,int>(x,y) )。之后,我们遍历每一个垃圾,建立一个find函数,对垃圾上下左右位置判断是否有垃圾,即判断set结构中是否存储了垃圾上下左右的位置,然后返回true或者false,如果为true,即可以建立回收站。
之后建立add函数,我们继续对垃圾的四个对角位置进行判断,即set结构中输入垃圾的四个对角位置判断是否存在,从而来得出分数,把分数存储到一个数组a[i] 中,最后再输出数组,即为答案。
#include <iostream>
#include<cstdio>
#include<set>
using namespace std;
set<pair<int, int>> s;
int find(int x,int y) {
if (s.count(pair<int, int>(x + 1, y)) == 0) {
return false;
}
else if (s.count(pair<int, int>(x - 1, y)) == 0) {
return false;
}
else if (s.count(pair<int, int>(x, y + 1)) == 0) {
return false;
}
else if (s.count(pair<int, int>(x, y - 1)) == 0) {
return false;
}
return true;
}
int add(int x,int y) {
int w= 0;
if (s.count(pair<int, int> (x + 1, y+1)) != 0) {
w++;
}
if (s.count(pair<int, int>(x + 1, y -1)) != 0) {
w++;
}
if (s.count(pair<int, int>(x - 1, y + 1)) != 0) {
w++;
}
if (s.count(pair<int, int>(x - 1, y - 1)) != 0) {
w++;
}
return w;
}
int main()
{
int n;
cin >> n;
int x, y;
pair<int, int>p[1200];
for (int i = 0; i < n; i++) {
cin >> x >> y;
p[i].first = x;
p[i].second = y;
s.insert(p[i]);
}
int a[5];
for (int i = 0; i < 5; i++) {
a[i] = 0;
}
for (int i = 0; i < n; i++) {
if (find(p[i].first,p[i].second)) {
if (add(p[i].first,p[i].second) == 0) {
a[0]++;
}
else if (add(p[i].first, p[i].second) == 1) {
a[1]++;
}
else if (add(p[i].first, p[i].second) == 2) {
a[2]++;
}
else if (add(p[i].first, p[i].second) == 3) {
a[3]++;
}
else if (add(p[i].first, p[i].second) == 4) {
a[4]++;
}
}
}
for (int i = 0; i < 5; i++) {
cout << a[i] << endl;
}
return 0;
}