CCF CSP 201912-2 回收站选址 100分

垃圾点选址算法:基于地图的相邻点检查

 

#include<iostream>
#include<map>   //map中键值对的构造需要引入pair,因此包含utility头文件 
int score[5]; //记录不同分数的选址个数,例如score[0]表示0分选址的个数 
using namespace std;

int main(){
	int n;
	scanf("%d",&n); //输入垃圾点个数 
	pair<int,int> p[n+1]; //存放垃圾点对应的坐标
	map<pair<int,int>,bool> m;  //将垃圾点标记为true,用map绑定 
	for(int i=1;i<=n;i++){ //循环输入垃圾点 
		int x,y;
		scanf("%d%d",&x,&y); //输入垃圾点坐标 
		p[i].first = x;  
		p[i].second = y;
		m[p[i]] = true;  //将该垃圾点标记为true 
	}
	for(int i=1;i<=n;i++){//从第一个垃圾点开始检测 
		//垃圾点(x,y)的上、左、下、右处的坐标的x和y值 
		int upper_x = p[i].first;
		int upper_y = p[i].second-1;
		int down_x = p[i].first;
		int down_y = p[i].second+1;
		int left_x = p[i].first-1;
		int left_y = p[i].second;
		int right_x = p[i].first+1;
		int right_y = p[i].second;
		//垃圾点(x,y)的上、左、下、右处的坐标 
		pair<int,int> upper = make_pair(upper_x,upper_y);
		pair<int,int> down = make_pair(down_x,down_y);
		pair<int,int> left = make_pair(left_x,left_y);
		pair<int,int> right = make_pair(right_x,right_y); 
		//判断是否符合选址的条件 
		if(m[upper]&&m[down]&&m[left]&&m[right]){
			//符合选址的条件后,再判断该选址(x,y)的分数
			//成为选址的垃圾点(x,y)的四个对角线位置的坐标的x,y值 
			//lu左上,ld左下,ru右上,rd右下
			int lu_x = p[i].first -1;
			int lu_y = p[i].second -1;
			int ld_x = p[i].first -1;
			int ld_y = p[i].second +1;
			int ru_x = p[i].first +1;
			int ru_y = p[i].second -1;
			int rd_x = p[i].first+1;
			int rd_y = p[i].second+1;
			//lu左上,ld左下,ru右上,rd右下
			pair<int,int> lu = make_pair(lu_x,lu_y);
			pair<int,int> ld = make_pair(ld_x,ld_y);
			pair<int,int> ru = make_pair(ru_x,ru_y);
			pair<int,int> rd = make_pair(rd_x,rd_y);
			//开始统计垃圾点(x,y)的得分 
			int count = 0;
			if(m[lu]){  //左上位置的垃圾点是否标记为true 
				count++;
			}
			if(m[ld]){//左下位置的垃圾点是否标记为true
				count++;
			}
			if(m[ru]){//右上位置的垃圾点是否标记为true
				count++;
			}
			if(m[rd]){//右下位置的垃圾点是否标记为true
				count++;
			}
			//每比较完一个符合条件的选址,将对应得分为count的选址个数加1 
			score[count]++;
		}
	}
	//循环输出得分为0、1、2、3、4 的回收站选址的个数 
	for(int i=0;i<=4;i++)
		cout<<score[i]<<endl;
	return 0;
}

根据提供的引用内容,问题描述为201912-2回收站选址问题。该问题要求统计每种得选址个数。具体来说,给定了待清理的垃圾位置,需要判断每个位置是否适合建立回收站,条件包括:该位置必须是整数坐标且存在垃圾;上下左右四个邻居位置必须全部存在垃圾;四个对角位置中存在垃圾的个数作为该位置的得。最后需要统计每种得选址个数。 根据问题描述,我们需要读取输入数据并进行处理。输入的第一行是一个整数n,表示待清理的垃圾位置的个数。接下来的n行,每行包含两个整数xi和yi,表示每个垃圾位置的坐标。 然后,我们可以使用嵌套循环来遍历每个垃圾位置,判断其是否满足建立回收站的条件。对于每个位置,我们可以检查该位置及其上下左右四个邻居位置是否存在垃圾,并统计存在垃圾的个数。 最后,我们可以使用一个数组来记录每种得选址个数,根据每个位置的得进行计数。 请注意,这只是问题的大致解决方法,具体的实现细节可能还需要进行进一步的析和编码。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [2019-12-2回收站选址【map】【set】](https://blog.youkuaiyun.com/han_hhh/article/details/108327976)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [CCF_Java_201912-2_回收站选址](https://blog.youkuaiyun.com/weixin_43662429/article/details/104051247)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值