CCF CSP 201912-2 回收站選址 C++實現

在這裏插入圖片描述
在這裏插入圖片描述
樣例1輸入:

7

1 2
2 1
0 0
1 1
1 0
2 0
0 1

輸出數據:

0
0
1
0
0

樣例2輸入:

2

0 0
-100000 10

輸出數據:

0
0
0
0
0

樣例3輸入:

11

9 10
10 10
11 10
12 10
13 10
11 9
11 8
12 9
10 9
10 11
12 11

輸出數據:

0
2
1
0
0

思路

建立一個set結構,來存儲垃圾的位置(位置就是一個 pair<int,int>(x,y) )。之後,我們遍歷每一個垃圾,建立一個find函數,對垃圾上下左右位置判斷是否有垃圾,即判斷set結構中是否存儲了垃圾上下左右的位置,然後返回true或者false,如果爲true,即可以建立回收站。
之後建立add函數,我們繼續對垃圾的四個對角位置進行判斷,即set結構中輸入垃圾的四個對角位置判斷是否存在,從而來得出分數,把分數存儲到一個數組a[i] 中,最後再輸出數組,即爲答案。

#include <iostream>
#include<cstdio>
#include<set>
using namespace std;


set<pair<int, int>> s;
int find(int x,int y) {
	if (s.count(pair<int, int>(x + 1, y)) == 0) {
		return false;
	}
	else if (s.count(pair<int, int>(x - 1, y)) == 0) {
		return false;
	}
	else if (s.count(pair<int, int>(x, y + 1)) == 0) {
		return false;
	}
	else if (s.count(pair<int, int>(x, y - 1)) == 0) {
		return false;
	}
	return true;
}

int add(int x,int y) {

	int w= 0;
	if (s.count(pair<int, int> (x + 1, y+1)) != 0) {
		w++;
	}
	if (s.count(pair<int, int>(x + 1, y -1)) != 0) {
		w++;
	}
	if (s.count(pair<int, int>(x - 1, y + 1)) != 0) {
		w++;
	}
	if (s.count(pair<int, int>(x - 1, y - 1)) != 0) {
		w++;
	}
	return w;
}



int main()
{
	int n;
	cin >> n;
	int x, y;
	pair<int, int>p[1200];

	for (int i = 0; i < n; i++) {
		cin >> x >> y;

		p[i].first = x;
		p[i].second = y;
		s.insert(p[i]);
	}
	int a[5];
	for (int i = 0; i < 5; i++) {
		a[i] = 0;
	}
	for (int i = 0; i < n; i++) {
		if (find(p[i].first,p[i].second)) {
			if (add(p[i].first,p[i].second) == 0) {
				a[0]++;
			}
			else if (add(p[i].first, p[i].second) == 1) {
				a[1]++;
			}
			else if (add(p[i].first, p[i].second) == 2) {
				a[2]++;
			}
			else if (add(p[i].first, p[i].second) == 3) {
				a[3]++;
			}
			else if (add(p[i].first, p[i].second) == 4) {
				a[4]++;
			}
		}

	}
	for (int i = 0; i < 5; i++) {
		cout << a[i] << endl;
	}



	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章