?2019-12-2 回收站选址
问题描述
试题编号: | 201912-2 |
试题名称: | 回收站选址 |
时间限制: | 1.0s |
内存限制: | 512.0MB |
问题描述: |
#include <bits/stdc++.h> using namespace std; const int N = 1000; pair<int, int> p[N]; const int CN = 4; int cnt[CN + 1];//计数数组 int main() { int n; map<pair<int, int>, int> ps; scanf("%d", &n); for(int i = 0; i < n; i++) { int x, y; scanf("%d%d", &x, &y); p[i] = make_pair(x, y);//键,表示点 ps[p[i]] = 1;//值,1表示存在垃圾 } memset(cnt, 0, sizeof(cnt)); for(int i = 0; i < n; i++) { int x = p[i].first;//取出键的x int y = p[i].second;//取出键的y if(ps[make_pair(x, y -1)] && ps[make_pair(x, y + 1)] && ps[make_pair(x - 1, y)] && ps[make_pair(x + 1, y)]) cnt[ps[make_pair(x - 1, y - 1)] + ps[make_pair(x - 1, y + 1)] + ps[make_pair(x + 1, y - 1)] + ps[make_pair(x + 1, y + 1)]]++; } for(int i = 0; i <= CN; i++) printf("%d\n", cnt[i]); return 0; }
pairs,map
坐标值范围比较大,而且坐标有可能是负数,难以用矩阵来存储坐标点,所以使用稀疏矩阵来存储。用STL的map来存储坐标是最为简单的。用C语言实现的话,要复杂很多。
#include<stdio.h> #include<string.h> #include<map> using namespace std; pair<int ,int > p[1001]; int cnt[5];//哈希,散列表存储思想 int main () { map<pair<int ,int>, int> ps; p[0]=make_pair(1,2); ps[p[0]]=1; printf("%d %d",ps[p[0]], ps[make_pair(2,3)]); //表明:当一个map只有键时,它的值为0 return 0; }
|
本文来自博客园,作者:永恒&,转载请注明原文链接:https://www.cnblogs.com/Sun-yuan/p/14455975.html