试题编号: | 201912-2 |
试题名称: | 回收站选址 |
时间限制: | 1.0s |
内存限制: | 512.0MB |
题目:太懒....就不贴了
解题思路:简单模拟即可
解题注意点:① 由于坐标有负数,所以写一个结构体进行存储;
② 由于样例中的坐标个数最多10的三次个,所以存储后直接遍历即可,使用二维数组反而麻烦,会超内存,还不能存储负数;
③ 之前在求score的函数中,用一个if(find(x+1,y+1,site) || find(x+1,y-1,site) || find(x-1,y+1,site) || find(x-1,y-1,site)) score++; 使得得分一直为1。
正确代码如下:
#include
using namespace std;
int n = 0;
struct Site{
int x;
int y;
};
bool find(int x, int y, Site site[]){
for(int i = 0; i < n; i++){
if(site[i].x == x && site[i].y == y) return true;
}
return false;
}
bool SetTrashCan(int x,int y,Site site[]){ // 判断该位置是否可以设置垃圾桶
if(find(x,y+1,site) && find(x,y-1,site) && find(x+1,y,site) &&find(x-1,y,site)) return true;
else return false;
}
int GetScore(int x,int y,Site site[]){
int score = 0;
if(find(x+1,y+1,site)) score++;
if(find(x-1,y-1,site)) score++;
if(find(x+1,y-1,site)) score++;
if(find(x-1,y+1,site)) score++;
return score;
}
int main(){
scanf("%d",&n);
Site site[n];
int grade[5] ={0};
for(int i = 0; i < n; i++){
scanf("%d %d",&site[i].x,&site[i].y);
}
for(int i = 0; i < n; i++){
int x = site[i].x;
int y = site[i].y;
if(SetTrashCan(x,y,site)){
grade[GetScore(x,y,site)]++;
// printf("%d %d %d\n",x,y,GetScore(x,y,site));
}
}
for(int i = 0; i < 5; i++){
printf("%d\n",grade[i]);
}
}
// 测试样例(答案在题目中)
//7
//1 2
//2 1
//0 0
//1 1
//1 0
//2 0
//0 1
//11
//9 10
//10 10
//11 10
//12 10
//13 10
//11 9
//11 8
//12 9
//10 9
//10 11
//12 11