STL
#include<map>
#include<iostream>
using namespace std;
const int maxn = 1010;
int cnt[5] = { 0 };
int main() {
int n;
cin >> n;
map<pair<int, int>, int> ps;
pair<int, int> p[maxn];
for (int i = 0; i < n; i++) {
cin >> p[i].first >> p[i].second;
ps[p[i]] = 1;
}
for (int i = 0; i < n; i++) {
int x = p[i].first;
int y = p[i].second;
if (ps[make_pair(x - 1, y)] && ps[make_pair(x + 1, y)]
&& ps[make_pair(x, y - 1)] && ps[make_pair(x, y + 1)]) {
cnt[ps[make_pair(x - 1, y - 1)] + ps[make_pair(x - 1, y + 1)] +
ps[make_pair(x + 1, y - 1)] + ps[make_pair(x + 1, y + 1)]]++;
}
}
for (int i = 0; i < 5; i++) {
if (i == 5) {
cout << cnt[5];
continue;
}
cout << cnt[i] << endl;
}
return 0;
}
结构体解法
#include<iostream>
using namespace std;
const int maxn = 1010;
int num[5] = { 0 };
struct point {
int x;
int y;
}p[maxn];
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> p[i].x >> p[i].y;
}
for (int i = 0; i < n; i++) {
int temp = 0;
int x = p[i].x, y = p[i].y;
for (int j = 0; j < n; j++) {
if (x == p[j].x && y + 1 == p[j].y)
temp++;
if (x == p[j].x && y - 1 == p[j].y)
temp++;
if (x - 1 == p[j].x && y == p[j].y)
temp++;
if (x + 1 == p[j].x && y == p[j].y)
temp++;
if (temp == 4)
break;
}
if (temp == 4) {
int count = 0;
for (int j = 0; j < n; j++)
{
if (x + 1 == p[j].x && y + 1 == p[j].y)
count++;
if (x + 1 == p[j].x && y - 1 == p[j].y)
count++;
if (x - 1 == p[j].x && y - 1 == p[j].y)
count++;
if (x - 1 == p[j].x && y + 1 == p[j].y)
count++;
}
num[count]++;
}
}
for (int i = 0; i < 5; i++) {
cout << num[i] << endl;
}
return 0;
}