CCFCSP-201912-2-回收站選址(C++)
阿新 • • 發佈:2021-02-17
日誌:
這次一遍過哈哈,這個作為第二題真是太友好了。做的時候有一會腦子瓦特了,一直在想確定一個點是否是垃圾點需要什麼演算法,還寫了會二分查詢演算法,其實就一個for迴圈暴力比較就好啦,又不是雙重迴圈,也不會超時,執行時間才不到1ms。
#include <iostream>
using namespace std;
// 垃圾點
typedef struct Point {
int x;
int y;
}Point;
// n個垃圾點
int n;
Point points[1000];
// 該處是否有垃圾
bool have_gabbage (int x, int y) {
for (int i = 0; i < n; i++) {
if (points[i].x == x && points[i].y == y)
return true;
}
return false;
}
int main() {
// 0,1,2,3,4 五種得分的回收站選址個數
int score0 = 0;
int score1 = 0;
int score2 = 0;
int score3 = 0;
int score4 = 0;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> points[i].x >> points[i].y;
}
// 每個垃圾點都有可能成為回收站
for (int i = 0; i < n; i++) {
int a = points[i].x;
int b = points[i].y;
// 該點是回收站
if (have_gabbage(a, b + 1) && have_gabbage(a, b - 1) && have_gabbage(a + 1, b) && have_gabbage(a - 1, b) ) {
// 求其分數
int score = 0;
if (have_gabbage(a - 1, b + 1))
score++;
if (have_gabbage(a - 1, b - 1))
score++;
if (have_gabbage(a + 1, b - 1))
score++;
if (have_gabbage(a + 1, b + 1))
score++;
switch (score)
{
case(0):
score0++;
break;
case(1):
score1++;
break;
case(2):
score2++;
break;
case(3):
score3++;
break;
case(4):
score4++;
break;
default:
break;
}
}
}
cout << score0 << endl;
cout << score1 << endl;
cout << score2 << endl;
cout << score3 << endl;
cout << score4 << endl;
return 0;
}