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;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值