#include<iostream>
#include<map>
#include<string.h>
using namespace std;
const int N = 1000;
int count[5];
pair<int, int> p[N];
int main()
{
int n;
map<pair<int, int>, int> ps;
cin >> n;
for(int i = 0; i < n; i ++)
{
int x, y;
scanf("%d%d", &x, &y);
p[i] = make_pair(x, y);
ps[p[i]] = 1;
}
memset(count, 0, sizeof(count));
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)])
count[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 ++)
{
printf("%d\n", count[i]);
}
return 0;
}
参考了老师的博客,呜呜呜,老师的题解太棒了~
----------------------------------更新------------------------------------
#include <iostream>
#include <map>
using namespace std;
const int N = 1000 + 5;
map<pair<int, int>, bool> mp;
pair<int, int> p[N];
int a[5] = {0};
int main()
{
int n, x, y, cnt;
cin >> n;
for(int i = 0; i < n; i ++)
{
cin >> x >> y;
p[i].first = x;
p[i].second = y;
mp[p[i]] = true;
}
for(int i = 0; i < n; i ++)
{
cnt = 0;//每次判断都要清零
x = p[i].first;
y = p[i].second;
if(mp[make_pair(x,y+1)] && mp[make_pair(x,y-1)] && mp[make_pair(x-1,y)] && mp[make_pair(x+1,y)])
{
if(mp[make_pair(x+1,y+1)]) cnt ++;
if(mp[make_pair(x+1,y-1)]) cnt ++;
if(mp[make_pair(x-1,y+1)]) cnt ++;
if(mp[make_pair(x-1,y-1)]) cnt ++;
a[cnt] ++;
}
}
for(int i = 0; i < 5; i ++)
{
cout << a[i] << endl;
}
return 0;
}