参考代码:
#include<iostream>
#include<vector>
#include<string>
#include<map>
#include<algorithm>
using namespace std;
void printmap(map<int, int>& m) {
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) {
cout << it->second << endl;
}
}
//判断是否是回收站
bool junkstation(vector<pair<int, int>>& m, pair<int, int>& p) {
pair<int, int> p1;
p1.first = p.first + 1, p1.second = p.second;
pair<int, int> p2;
p2.first = p.first - 1, p2.second = p.second;
pair<int, int> p3;
p3.first = p.first, p3.second = p.second + 1;
pair<int, int> p4;
p4.first = p.first, p4.second = p.second - 1;
vector<pair<int, int>>::iterator pos1 = find(m.begin(), m.end(), p1);
vector<pair<int, int>>::iterator pos2 = find(m.begin(), m.end(), p2);
vector<pair<int, int>>::iterator pos3 = find(m.begin(), m.end(), p3);
vector<pair<int, int>>::iterator pos4 = find(m.begin(), m.end(), p4);
if (pos1 != m.end() && pos2 != m.end() && pos3 != m.end() && pos4 != m.end()) {
return true;
}
else {
return false;
}
}
//获取回收站得分数
int getscore(vector<pair<int, int>>& m, pair<int, int>& p) {
int score = 0;
pair<int, int> p1;
p1.first = p.first + 1, p1.second = p.second + 1;
pair<int, int> p2;
p2.first = p.first - 1, p2.second = p.second - 1;
pair<int, int> p3;
p3.first = p.first + 1, p3.second = p.second - 1;
pair<int, int> p4;
p4.first = p.first - 1, p4.second = p.second + 1;
vector<pair<int, int>>::iterator pos1 = find(m.begin(), m.end(), p1);
vector<pair<int, int>>::iterator pos2 = find(m.begin(), m.end(), p2);
vector<pair<int, int>>::iterator pos3 = find(m.begin(), m.end(), p3);
vector<pair<int, int>>::iterator pos4 = find(m.begin(), m.end(), p4);
if (pos1 != m.end()) { score += 1; }
if (pos2 != m.end()) { score += 1; }
if (pos3 != m.end()) { score += 1; }
if (pos4 != m.end()) { score += 1; }
return score;
}
int main() {
int n;
cin >> n;
vector<pair<int, int>> vec;
map<int, int> m;
for (int i = 0; i < n; i++) {
pair<int, int> p;
cin >> p.first >> p.second;
vec.push_back(p);
}
for (int i = 0; i < 5; i++) {
m.insert(pair<int, int>(i, 0));
}
for (vector<pair<int, int>>::iterator it = vec.begin(); it != vec.end(); it++) {
if (junkstation(vec, *it)) {
int score = getscore(vec, *it);
for (int i = 0; i < 5; i++) {
if (score == i) {
m[i] += 1;
}
}
}
}
printmap(m);
system("pause");
return 0;
}
看到这题第一反应是vector套pair,然后将分数和站数量存在map里面,写下来感觉有点繁琐,懒得简化了,C++11是100分。