题目大意:只有上下左右全部有坐标存在的时候,对应的该坐标才可以被选为垃圾站,同时该坐标的四个对角代表分数(0,1,2,3,4),求分数为0,1,2,3,4的垃圾站个数
收获点:
- 安全性较高的文件输入输出流
- map和array的组合使用(出发点是尽可能为了整合信息)
#include <iostream>
#include <stdio.h>
#include <map>
#include <array>
#include <cmath>
#include <algorithm>
using namespace std;
using ll = long long;
//输入所有垃圾站的个数以及坐标,输出得分为0,1,2,3,4垃圾站的个数。
//为了尽可能的缩小时间复杂度,可以使用哈希表(map),争取循环一遍过。一边扫描一边处理。
int main()
{
FILE *stream1;
FILE *stream2;
freopen_s(&stream1,"input.txt", "r" ,stdin);
freopen_s(&stream2, "output.txt", "w", stdout);
map< array<ll, 2>, array<ll, 2> > hash;
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
array<ll, 2> temp;
cin >> temp[0];
cin >> temp[1];
int cornum = 0;
int neinum = 0;
//遍历已有的表项,修正已有的对角的个数和上下左右相邻的个数
for (map< array<ll, 2>, array<ll, 2> >::iterator it = hash.begin(); it != hash.end(); it++)
{
array<ll, 2> temp1 = (*it).first;
if (abs(temp[0] - temp1[0]) == 1 && abs(temp[1] - temp1[1]) == 1)//对角位置
{
cornum++;
hash[temp1][0]++;
}
else if (abs(temp[0] - temp1[0]) == 1&& abs(temp[1] - temp1[1]) ==0 || abs(temp[1] - temp1[1]) == 1&& abs(temp[0] - temp1[0]) == 0)
{
neinum++;
hash[temp1][1]++;
}
}
hash.insert({ temp,{cornum,neinum} });
}
int ans[10] = { 0 };
for (map< array<ll, 2>, array<ll, 2> >::iterator it = hash.begin(); it != hash.end(); it++)
{
array<ll, 2> temp2 = (*it).second;
if (temp2[1] == 4)
{
ans[temp2[0]]++;
}
}
for (int i = 0; i < 5; i++)
{
cout << ans[i] << endl;
}
fclose(stdin);
fclose(stdout);
}