思路:模拟题
1.由于一个垃圾点有坐标的属性,可以建一个结构体,用map匹配垃圾点与标记,由于map中的key值是有序的,需要排序函数,所以在定义结构体的时候要重载运算符<。
2.然后根据题目要求进行模拟
首先选址,建立回收站的条件是该点,且上下左右都要有垃圾,这个可以将包含各个点坐标的结构体数组作为map的key值,将对应的value的值都设为1,这样当一个点的value为1且上下左右的value都为1的时候,就可以确定该点可以作为回收站
3.计算每种选址的得分,就是看对角线上有没有垃圾,对角线的value为1的话计数器加1。
代码
#include
using namespace std;
#include
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;
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;//标记该点为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]++;//得分为cnt的回收站的个数加1
}
}
for(int i=0;i<5;i++)
{
printf("%d\n",c[i]);
}
return 0;
}
这道题的难点在于结构体的使用和stl中map的应用(当然能用数组死刚出来也是可以的)
参考:https://blog.csdn.net/weixin_44778155/article/details/103987010#comments