题目分析:可以用两个数组一个存横坐标,一个存纵坐标;也可以用一个结构体数组;也可以用单链表存储坐标。然后单拎出来每个点,扫描整个坐标系就可以了
代码分析:fun1()函数,判断是否适合建立垃圾站;fun2()函数为垃圾站打分。但我总觉得扫描坐标系的时候,时间复杂度太高qwq
满分C++代码:
#include<bits/stdc++.h>
using namespace std;
struct XY{
long long x;
long long y;
XY *next;
};
int fun1(XY *head,int x,int y,int a,int b,int c,int d){
XY *ptr = head->next;
int sflag = 0;
for(;ptr;ptr=ptr->next){
if (ptr->x ==x&&ptr->y ==c) {
sflag++;
continue;}
else if(ptr->x ==x&&ptr->y ==d) {
sflag++;
continue;}
else if(ptr->x ==a&&ptr->y ==y) {
sflag++;
continue;}
else if(ptr->x ==b&&ptr->y ==y) {
sflag++;
continue;}
else continue;
}
return sflag;
}
int fun2(XY *head,int x,int y,int a,int b,int c,int d){
XY *ptr = head->next;
int flag = 0;
for(;ptr;ptr=ptr->next){
if(ptr->x ==a&&ptr->y ==d) {
flag++;
continue;}
else if(ptr->x ==a&&ptr->y ==c) {
flag++;
continue;}
else if(ptr->x ==b&&ptr->y ==d) {
flag++;continue;}
else if(ptr->x ==b&&ptr->y ==c) {
flag++;continue;}
else continue;
}
return flag;
}
int main(){
int n,aa=0,bb=0,cc=0,dd=0,ee=0,ff=0;
cin>>n;
XY *head = (XY*)malloc(sizeof(XY));
head->next = NULL;
XY *ptr = head;
for(int i=0;i<n;i++){
XY *p = (XY*)malloc(sizeof(XY));
scanf("%lld%lld",&p->x,&p->y);
ptr->next = p;
ptr = p;
}
ptr = head->next;
for(int j=0;j<n;j++){
int xx=ptr->x,yy=ptr->y;
int a=xx-1,b=xx+1,c=yy-1,d=yy+1;
if(fun1(head,xx,yy,a,b,c,d)==4){
int t=fun2(head,xx,yy,a,b,c,d);
if(t==0) aa++;
else if(t==1) bb++;
else if(t==2) cc++;
else if(t==3) dd++;
else if(t==4) ee++;}
ptr = ptr->next;
}
cout<<aa<<endl<<bb<<endl<<cc<<endl<<dd<<endl<<ee<<endl;
return 0;
}
本文介绍了一种用于垃圾站选址的评分算法,通过分析坐标系中的点,判断是否适合建立垃圾站,并对其进行评分。使用了单链表存储坐标,通过两次遍历实现功能。
233





