CCF 201912-2 回收站选址

///因为这道题的键值很大所以用数组来实现hash是不现实的,这时我们就要用map<node, int>
///来实现hash
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <map>
#include <stack>
#include <math.h>

using namespace std;

struct node{
    int x, y;
    bool operator < (const node& other) const{//map中的key一定要有大小之分,像node这种类型本身无大小
        if(x!=other.x)                        //则需要定义他们元素之间大小的规则
            return x < other.x;
        else
            return y < other.y;
    }
}s[1010];

map<node, int> h;

int dir[4][2]={{-1,0}, {0,-1}, {1,0}, {0,1}};

bool fun(node t)
{
    for(int i=0; i<4; i++)
    {
        node point;
        point.x=t.x+dir[i][0];
        point.y=t.y+dir[i][1];
        if(h.find(point)==h.end())//h.find(key)返回键为key的映射迭代器,用h.count(point)==0是同样的效果
            return false;
    }
    node point;
    point.x=t.x+1;
    point.y=t.y+1;
    if(h.count(point))//h.count(key)返回键为key的是否存在,存在返回1,不存在返回0
        h[t]++;

    point.x=t.x+1;
    point.y=t.y-1;
    if(h.count(point))
        h[t]++;

    point.x=t.x-1;
    point.y=t.y+1;
    if(h.count(point))
        h[t]++;

    point.x=t.x-1;
    point.y=t.y-1;
    if(h.count(point))
        h[t]++;

    return true;
}

int main()
{
    int n, cnt[10];
    memset(cnt, 0, sizeof(cnt));
    cin >> n;
    for(int i=0; i<n; i++)
    {
        cin >> s[i].x >> s[i].y;
        h[s[i]]=0;
    }
    map<node, int>::iterator it=h.begin();
    for(; it!=h.end(); it++)
    {
        if(fun(it->first))
            cnt[it->second]++;
    }

    for(int i=0; i<=4; i++)
        cout << cnt[i] << endl;
    return 0;
}

 

posted on 2020-03-10 22:37  南风丶丶  阅读(197)  评论(0编辑  收藏  举报