使用C++的CCF-CSP满分解决方案 202206-2 寻宝!大冒险!点集

在这里插入图片描述

解题思路

这题如果用二维矩阵肯定会超标,因为样例保证 n < = 1 0 9 n <= 10^9 n<=109,所以考虑用点集来处理。

  1. 读入所有的树的位置,每个位置用一个pair来记录
  2. 记录每个可能的左下角——其实也就是树的位置——对应的藏宝图范围覆盖了多少棵树
  3. 读入藏宝图中树的位置和个数
  4. 对于每颗绿化图中的树,检查树的个数与藏宝图是否一致,检查树的位置与藏宝图是否一致,二者均符合则答案加一
  5. 输出答案

满分代码实现

#include <iostream>
#include <set>
#include <algorithm>
#include <cmath>
#include <map>
using namespace std;

int main() {
	set<pair<int, int> > forests;
	set<pair<int, int> > treasures;
	map<pair<int, int>, int> counts;
	int n = 0;
	int forest, treasure;
	cin >> n >> forest >> treasure;
	int x = 0, y = 0;
	for (int i = 0; i < n; i++) {
		cin >> x >> y;
		forests.insert(make_pair(x, y));
	}
	for (set<pair<int, int> >::iterator it = forests.begin(); it != forests.end(); it++) {
		x = it->first, y = it->second;
		for (int i = x; i >= max(x - treasure, 0); i--) {
			for (int j = y; j >= max(y - treasure, 0); j--) {
				if (!forests.count(make_pair(i, j))) continue;
				if (!counts.count(make_pair(i, j))) {
					counts[make_pair(i, j)] = 1;
				}
				else counts[make_pair(i, j)]++;
			}
		}
	}
	int isTree = 0;
	int trees = 0;
	for (int i = treasure; i >= 0; i--) {
		for (int j = 0; j <= treasure; j++) {
			cin >> isTree;
			if (!isTree) continue;
			treasures.insert(make_pair(i, j));
			trees++;
		}
	}
	int ans = 0;
	bool flag = true;
	for (set<pair<int, int> >::iterator it = forests.begin(); it != forests.end(); it++) {
		flag = true;
		x = it->first, y = it->second;
		if (counts[make_pair(x, y)] != trees) continue;
		if (x + treasure > forest || y + treasure > forest) continue;
		for (set<pair<int, int> >::iterator t = treasures.begin(); t != treasures.end(); t++) {
			if (!forests.count(make_pair(x + t->first, y + t->second))) {
				flag = false;
				break;
			}
		}
		
		if (flag) {
			// cout << x << " " << y << endl;
			ans++;
		}
	}
	cout << ans;
	return 0;
} 
  • 5
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值