题目描述:
开学了,可是校园里堆积了不少垃圾杂物。
热心的同学们纷纷自发前来清理,为学校注入正能量~
通过无人机航拍我们已经知晓了n处尚待清理的垃圾位置,其中第i(1<=i<=n)处的坐标为(xi,yi),保证所有的坐标均为整数。
我们希望在垃圾集中的地方建立些回收站。若垃圾A处的上下左右四处也存在垃圾,则A处可以被选为垃圾回收站,每个垃圾回收站还有四个位置:左上,左下,右上,右下,这四处每有一处存在垃圾,则回收站A的评分+1,回收站的评分可能为0、1、2、3、4。
问每个评分的回收站数量。
输入描述:
第一行一个正整数n。
接下来n行,每行一个二维坐标(xi,yi),表示此处有垃圾。坐标范围在1e9以内。
输出描述:
5行,5个评分的回收站数量。
输入样例:
11
1 2
1 3
1 4
2 1
2 2
2 3
2 4
2 5
3 2
3 3
3 4
输出样例:
0
0
2
0
1
核心思想:
map实现二分查找某点是否存在。
结构体内部需要重载小于号才能封装到map中。
代码如下:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<map>
using namespace std;
typedef long long ll;
const int N=1e3+50;
struct node{
int x,y;
node(){
}
node(int a,int b)
{
x=a;
y=b;
}
bool operator <(const node &oth)const
{
if(x!=oth.x)
return x<oth.x;
return y<oth.y;
}
}h[N];
map<node,bool>mp;//查询某个点是否存在,存在=1,否则=0
int c[6];
int main()
{
int n,x,y;
cin>>n;
for(int i=1;i<=n;i++)
{
scanf("%d%d",&x,&y);
h[i]=node(x,y);
mp[h[i]]=1;
}
for(int i=1;i<=n;i++)
{
int cnt=0;
x=h[i].x;
y=h[i].y;
if(mp[node(x+1,y)]&&mp[node(x-1,y)]&&mp[node(x,y+1)]&&mp[node(x,y-1)])
{
cnt=mp[node(x+1,y+1)]+mp[node(x+1,y-1)]+mp[node(x-1,y+1)]+mp[node(x-1,y-1)];
c[cnt]++;
}
}
for(int i=0;i<5;i++)
printf("%d\n",c[i]);
return 0;
}